summaryrefslogtreecommitdiffstats
path: root/pkgs/development/beam-modules/fetch-mix-deps.nix
blob: 96cdd4b3284815da1382c09a997bbcf2504a83ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
{
  stdenvNoCC,
  lib,
  elixir,
  hex,
  rebar,
  rebar3,
  cacert,
  git,
}@inputs:

{
  pname,
  version,
  hash ? "",
  sha256 ? "",
  src,
  mixEnv ? "prod",
  mixTarget ? "host",
  debug ? false,
  meta ? { },
  patches ? [ ],
  ...
}@attrs:

assert lib.assertMsg (!(attrs ? elixir || attrs ? hex))
  "fetchMixDeps no longer takes `elixir` or `hex`. Build with the package set that has the versions you need, e.g. `beam27Packages.fetchMixDeps`, or derive one with `beamPackages.overrideScope (final: prev: { elixir = prev.elixir_1_18; })`.";

let
  hash_ =
    if hash != "" then
      {
        outputHashAlgo = null;
        outputHash = hash;
      }
    else if sha256 != "" then
      {
        outputHashAlgo = "sha256";
        outputHash = sha256;
      }
    else
      {
        outputHashAlgo = "sha256";
        outputHash = lib.fakeSha256;
      };
in
stdenvNoCC.mkDerivation (
  attrs
  // {
    nativeBuildInputs = [
      elixir
      hex
      cacert
      git
    ];

    env = {
      MIX_ENV = mixEnv;
      MIX_TARGET = mixTarget;
      MIX_DEBUG = if debug then 1 else 0;
      DEBUG = if debug then 1 else 0; # for rebar3
      # the api with `mix local.rebar rebar path` makes a copy of the binary
      MIX_REBAR = "${rebar}/bin/rebar";
      MIX_REBAR3 = "${rebar3}/bin/rebar3";
      # there is a persistent download failure with absinthe 1.6.3
      # those defaults reduce the failure rate
      HEX_HTTP_CONCURRENCY = 1;
      HEX_HTTP_TIMEOUT = 120;
    }
    // (attrs.env or { });

    configurePhase =
      attrs.configurePhase or ''
        runHook preConfigure
        export HEX_HOME="$TEMPDIR/.hex";
        export MIX_HOME="$TEMPDIR/.mix";
        export MIX_DEPS_PATH="$TEMPDIR/deps";

        # Rebar
        export REBAR_GLOBAL_CONFIG_DIR="$TMPDIR/rebar3"
        export REBAR_CACHE_DIR="$TMPDIR/rebar3.cache"
        runHook postConfigure
      '';

    inherit patches;

    dontBuild = true;

    installPhase =
      attrs.installPhase or ''
        runHook preInstall
        mix deps.get ''${MIX_ENV:+--only $MIX_ENV}
        find "$TEMPDIR/deps" -path '*/.git/*' -a ! -name HEAD -exec rm -rf {} +
        cp -r --no-preserve=mode,ownership,timestamps $TEMPDIR/deps $out
        runHook postInstall
      '';

    outputHashMode = "recursive";

    impureEnvVars = lib.fetchers.proxyImpureEnvVars;
    inherit meta;
  }
  // hash_
)