-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild-npm-package.nix
58 lines (56 loc) · 1.3 KB
/
build-npm-package.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
{
lib,
fetchurl,
writeTextFile,
stdenv,
nodejs,
runCommandLocal,
}:
with lib;
{
src,
extraEnv ? {},
}: let
lockfile = importJSON "${src}/package-lock.json";
nodeModules = let
deps = builtins.attrValues (removeAttrs lockfile.packages [""]);
tarballs = map (p:
fetchurl {
url = p.resolved;
hash = p.integrity;
})
deps;
tarballsFile = writeTextFile {
name = "tarballs";
text = builtins.concatStringsSep "\n" tarballs + "\n";
};
npmCache = runCommandLocal "npm-cache" {buildInputs = [nodejs];} ''
mkdir -p $out
export HOME=$PWD/.home
export npm_config_cache=$out
while read package
do
echo "caching $package"
npm cache add "$package"
done < ${tarballsFile}
'';
in
stdenv.mkDerivation {
inherit (lockfile) name version;
inherit src;
env = extraEnv;
nativeBuildInputs = [nodejs];
buildPhase = ''
export HOME=$PWD/.home
export npm_config_cache="$PWD/.npm"
ln -s ${npmCache} $PWD/.npm
npm ci --offline
patchShebangs .
npm run build
'';
installPhase = ''
mv dist $out
'';
};
in
nodeModules