forked from axelf4/nix-rebar3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.nix
159 lines (144 loc) · 5.26 KB
/
default.nix
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{ lib, stdenv, callPackage, writeText, fetchHex, erlang, rebar3 }:
let
inherit (builtins) head elem elemAt listToAttrs getAttr hasAttr concatStringsSep;
inherit (callPackage ./lib.nix {}) readErl;
supportedConfigVsns = [ "1.2.0" ];
in {
buildRebar3 = {
root,
pname,
version,
releaseType ? "app",
profile ? "default",
checkouts ? {},
singleStep ? false,
...
}@attrs: let
deps = let
terms = readErl (root + "/rebar.lock");
vsn = head (head terms);
locks = elemAt (head terms) 1;
attrs = elemAt terms 1;
hashes = listToAttrs (map
(x: { name = head x; value = elemAt x 1; })
(elemAt (lib.findFirst
(x: head x == "pkg_hash_ext")
(throw "pkg_hash_ext not found")
attrs) 1));
in lib.trivial.warnIfNot (elem vsn supportedConfigVsns)
"Unsupported lock file. Proceeding anyway..."
(listToAttrs (map (x: let
name = head x;
src = elemAt x 1;
tbl = {
pkg = fetchHex {
pkg = elemAt src 1;
version = elemAt src 2;
sha256 = getAttr name hashes;
};
git = fetchGit {
url = elemAt src 1;
rev = elemAt (elemAt src 2) 1;
};
path = root + "/${elemAt src 1}";
};
type = head src;
in if hasAttr type tbl
then { inherit name; value = getAttr type tbl; }
else throw "Unsupported dependency type ${type} for ${name}")
locks) // checkouts);
depsDrv = stdenv.mkDerivation {
pname = "${pname}-deps";
inherit version;
src = builtins.path {
path = root;
name = "source";
filter = path: type: let
base = baseNameOf path;
in if type == "directory" then base == "_checkouts"
else base == "rebar.config" || base == "rebar.config.script" || base == "rebar.lock";
};
# OTP 25.1/26.0 will make this obsolete with erlang/otp#5965
# being merged, which makes Parsetools respect +deterministic.
postUnpack = ''
# Canonicalize path since e.g. Parsetools will output absolute paths
mv --no-clobber --no-target-directory "$sourceRoot" source
sourceRoot=source
'';
configurePhase = ''
mkdir -p _checkouts
${concatStringsSep "\n" (lib.mapAttrsToList
(name: value: ''cp --no-preserve=mode -r "${value}" _checkouts/${name}'')
deps)}
'';
buildPhase = ''
export ERL_COMPILER_OPTIONS=[deterministic]
REBAR_CACHE_DIR=.rebar-cache REBAR_OFFLINE=1 DEBUG=1 ${rebar3}/bin/rebar3 as ${profile} compile --deps_only
'';
installPhase = ''mv _build $out'';
};
specialParams = [ "releaseType" "profile" "checkouts" "singleStep" ];
in stdenv.mkDerivation (removeAttrs attrs specialParams // {
src = root;
buildInputs = [ erlang rebar3 ] ++ attrs.buildInputs or [];
setupHook = attrs.setupHook or
(if releaseType == "app"
then writeText "setupHook.sh" ''addToSearchPath ERL_LIBS $1/lib/erlang/lib''
else null);
postUnpack = attrs.postUnpack or ''
mv --no-clobber --no-target-directory "$sourceRoot" source
sourceRoot=source
'';
configurePhase = attrs.configurePhase or ''
runHook preConfigure
mkdir -p _checkouts
${concatStringsSep "\n" (lib.mapAttrsToList
(name: value: ''cp --no-preserve=mode -r "${value}" _checkouts/${name}'')
deps)}
${lib.optionalString (!singleStep)
''cp --no-preserve=mode -r --no-target-directory ${depsDrv} _build''}
runHook postConfigure
'';
buildPhase = attrs.buildPhase or ''
runHook preBuild
export ERL_COMPILER_OPTIONS=[deterministic]
REBAR_CACHE_DIR=.rebar-cache REBAR_OFFLINE=1 DEBUG=1 rebar3 as ${profile} ${if releaseType == "app" then "compile" else releaseType}
runHook postBuild
'';
installPhase = attrs.installPhase or ''
runHook preInstall
path="$(TERM=dumb rebar3 as ${profile} path --separator=: \
--${if releaseType == "app" then "ebin"
else if releaseType == "release" then "rel"
else if releaseType == "escriptize" then "bin"
else throw "The argument 'releaseType' has to be one of 'app', 'release' or 'escriptize'"})"
path="''${path##===> *
}"
${if releaseType == "app"
then ''
mkdir -p $out/lib/erlang/lib
IFS=: read -ra ebins <<<"$path"
for ebin in "''${ebins[@]}"; do
appdir="$(dirname "$ebin")"
find "$appdir" -xtype l -delete # Remove broken symlinks
cp --dereference -r "$appdir" $out/lib/erlang/lib
done
'' else ''
mkdir -p $out/bin
cp -r $path $out
${lib.optionalString (releaseType == "release")
''
find $out/rel/*/bin -type f -executable -exec ln -st $out/bin {} +
# Remove references to erlang to reduce closure size
for f in $out/rel/*/erts-*/bin/{erl,start}; do
substituteInPlace "$f" --replace ${erlang}/lib/erlang "''${f%/erts-*/bin/*}"
done
''}
''}
runHook postInstall
'';
meta = {
inherit (erlang.meta) platforms;
} // attrs.meta or {};
});
}