pretender: 1.2.0 -> 1.3.0 (#378434)
[NixPkgs.git] / pkgs / development / beam-modules / build-mix.nix
blobed286c4c2bd1e01d3c331d8bffa79489df785e3c
2   stdenv,
3   writeText,
4   elixir,
5   erlang,
6   hex,
7   lib,
8 }:
11   name,
12   version,
13   src,
14   buildInputs ? [ ],
15   nativeBuildInputs ? [ ],
16   erlangCompilerOptions ? [ ],
17   # Deterministic Erlang builds remove full system paths from debug information
18   # among other things to keep builds more reproducible. See their docs for more:
19   # https://www.erlang.org/doc/man/compile
20   erlangDeterministicBuilds ? true,
21   beamDeps ? [ ],
22   propagatedBuildInputs ? [ ],
23   postPatch ? "",
24   compilePorts ? false,
25   meta ? { },
26   enableDebugInfo ? false,
27   mixEnv ? "prod",
28   # A config directory that is considered for all the dependencies of an app, typically in $src/config/
29   # This was initially added, as some of Mobilizon's dependencies need to access the config at build time.
30   appConfigPath ? null,
31   ...
32 }@attrs:
34 let
35   shell =
36     drv:
37     stdenv.mkDerivation {
38       name = "interactive-shell-${drv.name}";
39       buildInputs = [ drv ];
40     };
42   pkg =
43     self:
44     stdenv.mkDerivation (
45       attrs
46       // {
47         name = "${name}-${version}";
48         inherit version src;
50         MIX_ENV = mixEnv;
51         MIX_DEBUG = if enableDebugInfo then 1 else 0;
52         HEX_OFFLINE = 1;
54         ERL_COMPILER_OPTIONS =
55           let
56             options = erlangCompilerOptions ++ lib.optionals erlangDeterministicBuilds [ "deterministic" ];
57           in
58           "[${lib.concatStringsSep "," options}]";
60         LC_ALL = "C.UTF-8";
62         # add to ERL_LIBS so other modules can find at runtime.
63         # http://erlang.org/doc/man/code.html#code-path
64         # Mix also searches the code path when compiling with the --no-deps-check flag
65         setupHook = attrs.setupHook or writeText "setupHook.sh" ''
66           addToSearchPath ERL_LIBS "$1/lib/erlang/lib"
67         '';
69         buildInputs = buildInputs ++ [ ];
70         nativeBuildInputs = nativeBuildInputs ++ [
71           elixir
72           hex
73         ];
74         propagatedBuildInputs = propagatedBuildInputs ++ beamDeps;
76         configurePhase =
77           attrs.configurePhase or ''
78             runHook preConfigure
80             ${./mix-configure-hook.sh}
81             ${lib.optionalString (!isNull appConfigPath)
82               # Due to https://hexdocs.pm/elixir/main/Config.html the config directory
83               # of a library seems to be not considered, as config is always
84               # application specific. So we can safely delete it.
85               ''
86                 rm -rf config
87                 cp -r ${appConfigPath} config
88               ''
89             }
91             runHook postConfigure
92           '';
94         buildPhase =
95           attrs.buildPhase or ''
96             runHook preBuild
97             export HEX_HOME="$TEMPDIR/hex"
98             export MIX_HOME="$TEMPDIR/mix"
99             mix compile --no-deps-check
100             runHook postBuild
101           '';
103         installPhase =
104           attrs.installPhase or ''
105             runHook preInstall
107             # This uses the install path convention established by nixpkgs maintainers
108             # for all beam packages. Changing this will break compatibility with other
109             # builder functions like buildRebar3 and buildErlangMk.
110             mkdir -p "$out/lib/erlang/lib/${name}-${version}"
112             # Some packages like db_connection will use _build/shared instead of
113             # honoring the $MIX_ENV variable.
114             for reldir in _build/{$MIX_ENV,shared}/lib/${name}/{src,ebin,priv,include} ; do
115               if test -d $reldir ; then
116                 # Some builds produce symlinks (eg: phoenix priv dircetory). They must
117                 # be followed with -H flag.
118                 cp  -Hrt "$out/lib/erlang/lib/${name}-${version}" "$reldir"
119               fi
120             done
122             # Copy the source so it can be used by dependent packages. For example,
123             # phoenix applications need the source of phoenix and phoenix_html to
124             # build javascript and css assets.
125             mkdir -p $out/src
126             cp -r $src/* "$out/src"
128             runHook postInstall
129           '';
131         # stripping does not have any effect on beam files
132         # it is however needed for dependencies with NIFs like bcrypt for example
133         dontStrip = false;
135         passthru = {
136           packageName = name;
137           env = shell self;
138           inherit beamDeps;
139         };
140       }
141     );
143 lib.fix pkg