summary refs log tree commit diff
path: root/pkgs/development/beam-modules/build-mix.nix
blob: 728d249c97d8c046e363c2bcde3f0217002ad11e (plain) (blame)
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
{ stdenv, writeText, elixir, erlang, hex, lib }:

{ name
, version
, src
, buildInputs ? [ ]
, nativeBuildInputs ? [ ]
, beamDeps ? [ ]
, propagatedBuildInputs ? [ ]
, postPatch ? ""
, compilePorts ? false
, meta ? { }
, enableDebugInfo ? false
, mixEnv ? "prod"
, ...
}@attrs:

with lib;
let
  shell = drv: stdenv.mkDerivation {
    name = "interactive-shell-${drv.name}";
    buildInputs = [ drv ];
  };

  pkg = self: stdenv.mkDerivation (attrs // {
    name = "${name}-${version}";
    inherit version src;

    MIX_ENV = mixEnv;
    MIX_DEBUG = if enableDebugInfo then 1 else 0;
    HEX_OFFLINE = 1;

    # add to ERL_LIBS so other modules can find at runtime.
    # http://erlang.org/doc/man/code.html#code-path
    # Mix also searches the code path when compiling with the --no-deps-check flag
    setupHook = attrs.setupHook or
      writeText "setupHook.sh" ''
      addToSearchPath ERL_LIBS "$1/lib/erlang/lib"
    '';

    buildInputs = buildInputs ++ [ ];
    nativeBuildInputs = nativeBuildInputs ++ [ elixir hex ];
    propagatedBuildInputs = propagatedBuildInputs ++ beamDeps;

    buildPhase = attrs.buildPhase or ''
      runHook preBuild
      export HEX_HOME="$TEMPDIR/hex"
      export MIX_HOME="$TEMPDIR/mix"
      mix compile --no-deps-check
      runHook postBuild
    '';

    installPhase = attrs.installPhase or ''
      runHook preInstall

      # This uses the install path convention established by nixpkgs maintainers
      # for all beam packages. Changing this will break compatibility with other
      # builder functions like buildRebar3 and buildErlangMk.
      mkdir -p "$out/lib/erlang/lib/${name}-${version}"

      # Some packages like db_connection will use _build/shared instead of
      # honoring the $MIX_ENV variable.
      for reldir in _build/{$MIX_ENV,shared}/lib/${name}/{src,ebin,priv,include} ; do
        if test -d $reldir ; then
          # Some builds produce symlinks (eg: phoenix priv dircetory). They must
          # be followed with -H flag.
          cp  -Hrt "$out/lib/erlang/lib/${name}-${version}" "$reldir"
        fi
      done

      runHook postInstall
    '';

    # stripping does not have any effect on beam files
    # it is however needed for dependencies with NIFs like bcrypt for example
    dontStrip = false;

    passthru = {
      packageName = name;
      env = shell self;
      inherit beamDeps;
    };
  });
in
fix pkg