-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
internal.nix
436 lines (397 loc) · 17.1 KB
/
internal.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
{ nodejs, stdenv, mkShell, lib, fetchurl, writeText, writeTextFile, runCommand, fetchFromGitHub }:
rec {
default_nodejs = nodejs;
# Description: Turns an npm lockfile dependency into an attribute set as needed by fetchurl
# Type: String -> Set -> Set
makeSourceAttrs = name: dependency:
assert !(dependency ? resolved) -> throw "[npmlock2nix] Missing `resolved` attribute for dependency `${name}`.";
assert !(dependency ? integrity) -> throw "[npmlock2nix] Missing `integrity` attribute for dependency `${name}`.";
{
url = dependency.resolved;
# FIXME: for backwards compatibility we should probably set the
# `sha1`, `sha256`, `sha512` … attributes depending on the string
# content.
hash = dependency.integrity;
};
# Description: Returns true if the given input character is allowed within a derviation name.
# Type: String -> Boolean
# Source: https://github.com/nixos/nix/blob/706777a4a8b13771221f9b0ef3e984a50562e82b/src/libstore/path.cc#L5-L17
isValidDrvNameChar = c:
"0" <= c && c <= "9" ||
"a" <= c && c <= "z" ||
"A" <= c && c <= "Z" ||
c == "+" || c == "-" || c == "." || c == "_" || c == "?" || c == "=";
# Description: Converts a npm package name to something that is compatible with nix
# Type: String -> String
makeValidDrvName = str:
lib.stringAsChars (c: if isValidDrvNameChar c then c else "?") str;
# Description: Takes a string of the format "github:org/repo#revision" and returns
# an attribute set { org, repo, rev }
# Type: String -> Set
parseGitHubRef = str:
let
parts = builtins.split "[:#/]" str;
in
assert !(builtins.length parts == 7) ->
builtins.throw "[npmlock2nix] failed to parse GitHub reference `${str}`. Expected a string of format `github:org/repo#revision`";
rec {
inherit parts;
org = builtins.elemAt parts 2;
repo = builtins.elemAt parts 4;
rev = builtins.elemAt parts 6;
};
# Description: Takes an attribute set describing a git dependency and returns
# a .tgz of the repository as store path. If the attribute hash contains a
# hash attribute it will provide the value to `fetchFromGitHub` which will
# also work in restricted evaluation.
# Type: Set -> Path
buildTgzFromGitHub = { name, org, repo, rev, ref, hash ? null }:
let
src =
if hash != null then
fetchFromGitHub
{
owner = org;
inherit repo;
inherit rev;
sha256 = hash; # FIXME: what if sha3?
} else
builtins.fetchGit {
url = "https://github.com/${org}/${repo}";
inherit rev ref;
};
in
runCommand
name
{ } ''
set +x
tar -C ${src} -czf $out ./
'';
# Description: Turns a dependency with a from field of the format
# `github:org/repo#revision` into a git fetcher. The fetcher can
# receive a hash value by calling 'sourceHashFunc' if a source hash
# map has been provided. Otherwise the function yields `null`.
# Type: Fn -> String -> Set -> Path
makeGithubSource = sourceHashFunc: name: dependency:
assert !(dependency ? version) ->
builtins.throw "Missing `version` attribute missing from `${name}`";
assert (lib.hasPrefix "github: " dependency.version) -> builtins.throw "invalid prefix for `version` field of `${name}` expected `github:`, got: `${dependency.version}`.";
let
v = parseGitHubRef dependency.version;
f = parseGitHubRef dependency.from;
in
assert v.org != f.org -> throw "[npmlock2nix] version and from of `${name}` disagree on the GitHub org to fetch from: `${v.org}` vs `${f.org}`";
assert v.repo != f.repo -> throw "[npmlock2nix] version and from of `${name}` disagree on the GitHub repo to fetch from: `${v.repo}` vs `${f.repo}`";
let
src = buildTgzFromGitHub {
name = "${name}.tgz";
ref = v.rev;
inherit (v) org repo rev;
hash = sourceHashFunc { type = "github"; value = v; };
};
in
(builtins.removeAttrs dependency [ "from" ]) // {
version = "file://" + (toString src);
};
# Description: Turns an npm lockfile dependency into a fetchurl derivation
# Type: Fn -> String -> Set -> Derivation
makeSource = sourceHashFunc: name: dependency:
assert (builtins.typeOf name != "string") ->
throw "[npmlock2nix] Name of dependency ${toString name} must be a string";
assert (builtins.typeOf dependency != "set") ->
throw "[npmlock2nix] Specification of dependency ${toString name} must be a set";
if dependency ? resolved && dependency ? integrity then
dependency // { resolved = "file://" + (toString (fetchurl (makeSourceAttrs name dependency))); }
else if dependency ? from && dependency ? version then
makeGithubSource sourceHashFunc name dependency
else throw "[npmlock2nix] A valid dependency consists of at least the resolved and integrity field. Missing one or both of them for `${name}`. The object I got looks like this: ${builtins.toJSON dependency}";
# Description: Parses the lock file as json and returns an attribute set
# Type: Path -> Set
readLockfile = file:
let
content = builtins.readFile file;
json = builtins.fromJSON content;
in
assert
builtins.typeOf json != "set" ->
throw "[npmlock2nix] The NPM lockfile must be a valid JSON object";
# if a lockfile doesn't declare dependencies ensure that we have an empty
# set. This makes the consuming code eaiser.
if json ? dependencies then json else json // { dependencies = { }; };
# Description: Turns a github string reference into a store path with a tgz of the reference
# Type: Fn -> String -> String -> Path
stringToTgzPath = sourceHashFunc: name: str:
let
gitAttrs = parseGitHubRef str;
in
buildTgzFromGitHub {
name = "${name}.tgz";
ref = gitAttrs.rev;
inherit (gitAttrs) org repo rev;
hash = sourceHashFunc { type = "github"; value = gitAttrs; };
};
# Description: Patch the `requires` attributes of a dependency spec to refer to paths in the store
# Type: Fn -> String -> Set -> Set
patchRequires = sourceHashFunc: name: requires:
let
patchReq = name: version: if lib.hasPrefix "github:" version then stringToTgzPath sourceHashFunc name version else version;
in
lib.mapAttrs patchReq requires;
# Description: Patches a single lockfile dependency (recursively) by replacing the resolved URL with a store path
# Type: Fn -> String -> Set -> Set
patchDependency = sourceHashFunc: name: spec:
assert (builtins.typeOf name != "string") ->
throw "[npmlock2nix] Name of dependency ${toString name} must be a string";
assert (builtins.typeOf spec != "set") ->
throw "[npmlock2nix] pec of dependency ${toString name} must be a set";
let
isBundled = spec ? bundled && spec.bundled == true;
hasGitHubRequires = spec: (spec ? requires) && (lib.any (x: lib.hasPrefix "github:" x) (lib.attrValues spec.requires));
patchSource = lib.optionalAttrs (!isBundled) (makeSource sourceHashFunc name spec);
patchRequiresSources = lib.optionalAttrs (hasGitHubRequires spec) { requires = (patchRequires sourceHashFunc name spec.requires); };
patchDependenciesSources = lib.optionalAttrs (spec ? dependencies) { dependencies = lib.mapAttrs (patchDependency sourceHashFunc) spec.dependencies; };
in
# For our purposes we need a dependency with
# - `resolved` set to a path in the nix store (`patchSource`)
# - All `requires` entries of this dependency that are set to github URLs set to a path in the nix store (`patchRequiresSources`)
# - This needs to be done recursively for all `dependencies` in the lockfile (`patchDependenciesSources`)
(spec // patchSource // patchRequiresSources // patchDependenciesSources);
# Description: Takes a Path to a lockfile and returns the patched version as attribute set
# Type: Fn -> Path -> Set
patchLockfile = sourceHashFunc: file:
assert (builtins.typeOf file != "path" && builtins.typeOf file != "string") ->
throw "[npmlock2nix] file ${toString file} must be a path or string";
let content = readLockfile file; in
content // {
dependencies = lib.mapAttrs (patchDependency sourceHashFunc) content.dependencies;
};
# Description: Rewrite all the `github:` references to store paths
# Type: Fn -> Path -> Set
patchPackagefile = sourceHashFunc: file:
assert (builtins.typeOf file != "path" && builtins.typeOf file != "string") ->
throw "[npmlock2nix] file ${toString file} must be a path or string";
let
# Read the file but also add empty `devDependencies` and `dependencies`
# if either are missing
content = builtins.fromJSON (builtins.readFile file);
patchDep = (name: version:
if lib.hasPrefix "github:" version then
"file://${stringToTgzPath sourceHashFunc name version}"
else version);
dependencies = if (content ? dependencies) then lib.mapAttrs patchDep content.dependencies else { };
devDependencies = if (content ? devDependencies) then lib.mapAttrs patchDep content.devDependencies else { };
in
content // { inherit devDependencies dependencies; };
# Description: Takes a Path to a package file and returns the patched version as file in the Nix store
# Type: Fn -> Path -> Derivation
patchedPackagefile = sourceHashFunc: file: writeText "package.json"
(
builtins.toJSON (patchPackagefile sourceHashFunc file)
);
# Description: Takes a Path to a lockfile and returns the patched version as file in the Nix store
# Type: Fn -> Path -> Derivation
patchedLockfile = sourceHashFunc: file: writeText "packages-lock.json"
(builtins.toJSON (patchLockfile sourceHashFunc file));
# Description: Turn a derivation (with name & src attribute) into a directory containing the unpacked sources
# Type: Derivation -> Derivation
nodeSource = nodejs: runCommand "node-sources-${nodejs.version}"
{ } ''
tar --no-same-owner --no-same-permissions -xf ${nodejs.src}
mv node-* $out
'';
# Description: Creates shell scripts to provide node_modules to the environment supporting
# two different modes: "symlink" and "copy"
# Type: Derivation -> String -> String
add_node_modules_to_cwd = node_modules: mode:
''
# If node_modules is a managed symlink we can safely remove it and install a new one
${lib.optionalString (mode == "symlink") ''
if [[ "$(readlink -f node_modules)" == ${builtins.storeDir}* ]]; then
rm -f node_modules
fi
''}
if test -e node_modules; then
echo '[npmlock2nix] There is already a `node_modules` directory. Not replacing it.' >&2
exit 1
fi
'' +
(
if mode == "copy" then ''
cp --no-preserve=mode -r ${node_modules}/node_modules node_modules
chmod -R u+rw node_modules
'' else if mode == "symlink" then ''
ln -s ${node_modules}/node_modules node_modules
'' else throw "[npmlock2nix] node_modules_mode must be either `copy` or `symlink`"
) + ''
export NODE_PATH="$(pwd)/node_modules:$NODE_PATH"
'';
# Description: Extract the attributes that are relevant for building node_modules and use
# them as defaults in case the node_modules_attrs attribute doesn't have
# them.
# Type: Set -> Set
get_node_modules_attrs = { node_modules_attrs ? { }, ... }@attrs:
let
getAttr = name: from: lib.optionalAttrs (builtins.hasAttr name from) { "${name}" = from.${name}; };
getAttrs = names: from: lib.foldl (a: b: a // (getAttr b from)) { } names;
in
(getAttrs [ "src" "nodejs" ] attrs // node_modules_attrs);
# Description: Takes a dependency spec and a map of github sources/hashes and returns either the map or 'null'
# Type: Set -> Set -> Set | null
sourceHashFunc = githubSourceHashMap: spec:
if spec.type == "github" then
lib.attrByPath [ spec.value.org spec.value.repo spec.value.rev ] null githubSourceHashMap
else
throw "[npmlock2nix] sourceHashFunc: spec.type '${spec.type}' is not supported. Supported types: 'github'";
node_modules =
{ src
, packageJson ? src + "/package.json"
, packageLockJson ? src + "/package-lock.json"
, buildInputs ? [ ]
, nativeBuildInputs ? [ ]
, nodejs ? default_nodejs
, preBuild ? ""
, postBuild ? ""
, preInstallLinks ? { } # set that describes which files should be linked in a specific packages folder
, githubSourceHashMap ? { }
, passthru ? { }
, ...
}@args:
assert (builtins.typeOf preInstallLinks != "set") ->
throw "[npmlock2nix] `preInstallLinks` must be an attributeset of attributesets";
let
cleanArgs = builtins.removeAttrs args [ "src" "packageJson" "packageLockJson" "buildInputs" "nativeBuildInputs" "nodejs" "preBuild" "postBuild" "preInstallLinks" "githubSourceHashMap" ];
lockfile = readLockfile packageLockJson;
preinstall_node_modules = writeTextFile {
name = "prepare";
destination = "/node_modules/.hooks/prepare";
text =
let
preInstallLinkCommands = lib.concatStringsSep "\n" (
lib.mapAttrsToList
(name: mappings: ''
if [ "$npm_package_name" == "${name}" ]; then
${lib.concatStringsSep "\n"
(lib.mapAttrsToList
(to: from: ''
dirname=$(dirname ${to})
mkdir -p $dirname
ln -s ${from} ${to}
'')
mappings
)}
fi
'')
preInstallLinks
);
in
''
#! ${stdenv.shell}
${preInstallLinkCommands}
if grep -I -q -r '/bin/' .; then
source $TMP/preinstall-env
patchShebangs .
fi
'';
executable = true;
};
in
stdenv.mkDerivation ({
inherit (lockfile) version;
pname = makeValidDrvName lockfile.name;
inherit buildInputs preBuild postBuild;
dontUnpack = true;
nativeBuildInputs = nativeBuildInputs ++ [
nodejs
];
propagatedBuildInputs = [
nodejs
];
setupHooks = [
./set-node-path.sh
];
preConfigure = ''
export HOME=$(mktemp -d)
'';
postPatch = ''
ln -sf ${patchedLockfile (sourceHashFunc githubSourceHashMap) packageLockJson} package-lock.json
ln -sf ${patchedPackagefile (sourceHashFunc githubSourceHashMap) packageJson} package.json
'';
buildPhase = ''
runHook preBuild
mkdir -p node_modules/.hooks
declare -pf > $TMP/preinstall-env
ln -s ${preinstall_node_modules}/node_modules/.hooks/prepare node_modules/.hooks/preinstall
export HOME=.
npm install --offline --nodedir=${nodeSource nodejs}
test -d node_modules/.bin && patchShebangs node_modules/.bin
rm -rf node_modules/.hooks
runHook postBuild
'';
installPhase = ''
mkdir $out
if test -d node_modules; then
if [ $(ls -1 node_modules | wc -l) -gt 0 ] || [ -e node_modules/.bin ]; then
mv node_modules $out/
if test -d $out/node_modules/.bin; then
ln -s $out/node_modules/.bin $out/bin
fi
fi
fi
'';
passthru = passthru // {
inherit nodejs;
lockfile = patchedLockfile packageLockJson;
packagesfile = patchedPackagefile packageJson;
};
} // cleanArgs);
shell =
{ src
, node_modules_mode ? "symlink"
, node_modules_attrs ? { }
, buildInputs ? [ ]
, passthru ? { }
, shellHook ? ""
, ...
}@attrs:
let
nm = node_modules (get_node_modules_attrs attrs);
extraAttrs = builtins.removeAttrs attrs [ "node_modules_attrs" "passthru" "shellHook" "buildInputs" ];
in
mkShell ({
buildInputs = buildInputs ++ [ nm.nodejs nm ];
shellHook = ''
# FIXME: we should somehow register a GC root here in case of a symlink?
${add_node_modules_to_cwd nm node_modules_mode}
'' + shellHook;
passthru = passthru // {
node_modules = nm;
};
} // extraAttrs);
build =
{ src
, buildCommands ? [ "npm run build" ]
, installPhase
, node_modules_attrs ? { }
, node_modules_mode ? "symlink"
, buildInputs ? [ ]
, passthru ? { }
, ...
}@attrs:
let
nm = node_modules (get_node_modules_attrs attrs);
extraAttrs = builtins.removeAttrs attrs [ "node_modules_attrs" "passthru" "buildInputs" ];
in
stdenv.mkDerivation ({
pname = nm.pname;
version = nm.version;
buildInputs = [ nm ] ++ buildInputs;
inherit src installPhase;
preConfigure = add_node_modules_to_cwd nm node_modules_mode;
buildPhase = ''
runHook preBuild
${lib.concatStringsSep "\n" buildCommands}
runHook postBuild
'';
passthru = passthru // { node_modules = nm; };
} // extraAttrs);
}