nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 stdenv,
3 writeText,
4 erlang,
5 rebar3WithPlugins,
6 openssl,
7 libyaml,
8 lib,
9}:
10
11{
12 name,
13 version,
14 src,
15 setupHook ? null,
16 buildInputs ? [ ],
17 beamDeps ? [ ],
18 buildPlugins ? [ ],
19 postPatch ? "",
20 installPhase ? null,
21 buildPhase ? null,
22 configurePhase ? null,
23 meta ? { },
24 erlangCompilerOptions ? [ ],
25 # Deterministic Erlang builds remove full system paths from debug information
26 # among other things to keep builds more reproducible. See their docs for more:
27 # https://www.erlang.org/doc/man/compile
28 erlangDeterministicBuilds ? true,
29 ...
30}@attrs:
31
32let
33 rebar3 = rebar3WithPlugins {
34 plugins = buildPlugins;
35 };
36
37 shell =
38 drv:
39 stdenv.mkDerivation {
40 name = "interactive-shell-${drv.name}";
41 buildInputs = [ drv ];
42 };
43
44 customPhases = lib.filterAttrs (_: v: v != null) {
45 inherit
46 setupHook
47 configurePhase
48 buildPhase
49 installPhase
50 ;
51 };
52
53 pkg =
54 self:
55 stdenv.mkDerivation (
56 attrs
57 // {
58
59 name = "${name}-${version}";
60 inherit version;
61
62 buildInputs = buildInputs ++ [
63 erlang
64 rebar3
65 openssl
66 libyaml
67 ];
68 propagatedBuildInputs = lib.unique beamDeps;
69
70 inherit src;
71
72 ERL_COMPILER_OPTIONS =
73 let
74 options = erlangCompilerOptions ++ lib.optionals erlangDeterministicBuilds [ "deterministic" ];
75 in
76 "[${lib.concatStringsSep "," options}]";
77
78 # stripping does not have any effect on beam files
79 # it is however needed for dependencies with NIFs
80 # false is the default but we keep this for readability
81 dontStrip = false;
82
83 setupHook = writeText "setupHook.sh" ''
84 addToSearchPath ERL_LIBS "$1/lib/erlang/lib/"
85 '';
86
87 postPatch = ''
88 rm -f rebar rebar3
89 ''
90 + postPatch;
91
92 preConfigure = ''
93 # Copy the source so it can be used by mix projects
94 # do this before building to avoid build artifacts but after patching
95 # to include any modifications
96 mkdir -p $out/src
97 cp -r "." "$out/src"
98 '';
99
100 buildPhase = ''
101 runHook preBuild
102 HOME=. rebar3 bare compile --paths "."
103 runHook postBuild
104 '';
105
106 installPhase = ''
107 runHook preInstall
108 mkdir -p "$out/lib/erlang/lib/${name}-${version}"
109 for reldir in src ebin priv include; do
110 [ -d "$reldir" ] || continue
111 # $out/lib/erlang/lib is a convention used in nixpkgs for compiled BEAM packages
112 cp -Hrt "$out/lib/erlang/lib/${name}-${version}" "$reldir"
113 done
114 runHook postInstall
115 '';
116
117 meta = {
118 inherit (erlang.meta) platforms;
119 }
120 // meta;
121
122 passthru = {
123 packageName = name;
124 env = shell self;
125 inherit beamDeps;
126 };
127 }
128 // customPhases
129 );
130in
131lib.fix pkg