forked from nix-community/pnpm2nix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
derivation.nix
159 lines (125 loc) · 4.3 KB
/
derivation.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
{ pkgs ? import <nixpkgs> {}
, nodejs ? pkgs.nodejs
, nodePackages ? pkgs.nodePackages
, node-gyp ? nodePackages.node-gyp
}:
let
inherit (pkgs) stdenv lib fetchurl;
hasScript = scriptName: "test \"$(${pkgs.jq}/bin/jq -e -r '.scripts | .${scriptName} | length' < package.json)\" -gt 0";
linkBinOutputsScript = ./link-bin-outputs.py;
in {
mkPnpmDerivation = lib.makeOverridable ({
attrs ? {},
devDependencies ? [],
deps ? [],
linkDevDependencies,
passthru ? {},
}: stdenv.mkDerivation (attrs // {
outputs = attrs.outputs or [ "out" "lib" ];
# Only bin outputs specified in package.json should be patched
# Trying to reduce some closure size
dontPatchShebangs = true;
nativeBuildInputs = with pkgs; [ pkgconfig ];
propagatedBuildInputs = [];
buildInputs = [ nodejs nodejs.passthru.python node-gyp ]
++ lib.optionals (lib.hasAttr "buildInputs" attrs) attrs.buildInputs
++ lib.optionals linkDevDependencies devDependencies
++ deps;
checkInputs = devDependencies;
passthru = passthru // {
inherit nodejs;
};
checkPhase = let
runTestScript = scriptName: ''
if ${hasScript scriptName}; then
PATH="${lib.makeBinPath devDependencies}:$PATH" npm run-script ${scriptName}
fi
'';
in attrs.checkPhase or ''
runHook preCheck
for constituent in ''${constituents}; do
cd "''${constituent}"
${runTestScript "pretest"}
${runTestScript "test"}
${runTestScript "posttest"}
cd "''${build_dir}"
done
runHook postCheck
'';
sourceRoot = ".";
postUnpack = ''
mkdir -p node_modules
cp -R pnpm2nix-source-*/* node_modules/
rm -r pnpm2nix-source-*
'';
configurePhase = let
linkDeps = deps ++ lib.optionals linkDevDependencies devDependencies;
linkDep = dep: ''
ls -d ${lib.getLib dep}/node_modules/* ${lib.getLib dep}/node_modules/@*/* | grep -Pv '(@[^/]+)$' | while read module; do
if test ! -L "$module"; then
# Check for nested directories (npm calls this scopes)
if test "$(echo "$module" | grep -o '@')" = '@'; then
scope=$(echo "${dep.pname}" | cut -d/ -f 1)
outdir=node_modules/$scope
mkdir -p "$outdir"
ln -sf "$module" "$outdir"
else
ln -sf "$module" node_modules/ || :
fi
fi
done
'';
in attrs.configurePhase or ''
runHook preConfigure
export constituents=$(ls -d node_modules/* node_modules/@*/* | grep -Pv '(@[^/]+)$')
export build_dir=$(pwd)
# Prevent gyp from going online (no matter if invoked by us or by package.json)
export npm_config_nodedir="${nodejs}"
# node-gyp writes to $HOME
export HOME="$TEMPDIR"
# Link dependencies into node_modules
${lib.concatStringsSep "\n" (map linkDep linkDeps)}
for constituent in ''${constituents}; do
cd "''${constituent}"
if ${hasScript "preinstall"}; then
npm run-script preinstall
fi
cd "''${build_dir}"
done
runHook postConfigure
'';
buildPhase = attrs.buildPhase or ''
runHook preBuild
for constituent in ''${constituents}; do
cd "''${constituent}"
# If there is a binding.gyp file and no "install" or "preinstall" script in package.json "install" defaults to "node-gyp rebuild"
if ${hasScript "install"}; then
npm run-script install
elif ${hasScript "preinstall"}; then
true
elif [ -f ./binding.gyp ]; then
${nodePackages.node-gyp}/bin/node-gyp rebuild
fi
if ${hasScript "postinstall"}; then
npm run-script postinstall
fi
cd "''${build_dir}"
done
runHook postBuild
'';
installPhase = let
linkBinOutputs = "${nodejs.passthru.python}/bin/python ${linkBinOutputsScript}";
in attrs.installPhase or ''
runHook preInstall
mkdir -p "$out/bin" "$lib"
mv node_modules $lib/
# Create bin outputs
for constituent in ''${constituents}; do
${linkBinOutputs} "$out/bin/" "$lib/$constituent" | while read bin_in; do
patchShebangs "$bin_in"
done
done
runHook postInstall
'';
}));
}