forked from nzbr/pnpm2nix-nzbr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
derivation.nix
253 lines (223 loc) · 8.18 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
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
{ lib
, stdenv
, nodejs
, rsync
, pkg-config
, callPackage
, writeText
, runCommand
, ...
}:
with builtins; with lib; with callPackage ./lockfile.nix { };
let
nodePkg = nodejs;
pkgConfigPkg = pkg-config;
in
{
mkPnpmPackage =
{ workspace ? null
, components ? []
, src ? if (workspace != null && components != []) then workspace else null
, packageJSON ? src + "/package.json"
, componentPackageJSONs ? map (c: {
name = "${c}/package.json";
value = src + "/${c}/package.json";
}) components
, pnpmLockYaml ? src + "/pnpm-lock.yaml"
, pnpmWorkspaceYaml ? (if workspace == null then null else workspace + "/pnpm-workspace.yaml")
, pname ? (fromJSON (readFile packageJSON)).name
, version ? (fromJSON (readFile packageJSON)).version or null
, name ? if version != null then "${pname}-${version}" else pname
, registry ? "https://registry.npmjs.org"
, script ? "build"
, distDir ? "dist"
, distDirs ? (if workspace == null then [distDir] else (map (c: "${c}/dist") components))
, distDirIsOut ? true
, installNodeModules ? false
, installPackageFiles ? false
, installInPlace ? false
, installEnv ? { }
, buildEnv ? { }
, noDevDependencies ? false
, extraNodeModuleSources ? [ ]
, copyPnpmStore ? true
, copyNodeModules ? false
, extraBuildInputs ? [ ]
, nodejs ? nodePkg
, pnpm ? nodejs.pkgs.pnpm
, pkg-config ? pkgConfigPkg
, ...
}@attrs:
let
# Flag that can be computed from arguments, indicating a workspace was
# supplied. Only used in these let bindings.
isWorkspace = workspace != null && components != [];
# Utility functions
forEachConcat = f: xs: concatStringsSep "\n" (map f xs);
forEachComponent = f: forEachConcat f components;
# Computed values used below that don't loop
nativeBuildInputs = [
nodejs
pnpm
pkg-config
] ++ extraBuildInputs ++ (optional copyNodeModules rsync);
copyLink =
if copyNodeModules
then "rsync -a --chmod=u+w"
else "ln -s";
rsyncSlash = optionalString copyNodeModules "/";
packageFilesWithoutLockfile =
[
{ name = "package.json"; value = packageJSON; }
] ++ componentPackageJSONs ++ computedNodeModuleSources;
computedNodeModuleSources =
(if pnpmWorkspaceYaml == null
then []
else [
{name = "pnpm-workspace.yaml"; value = pnpmWorkspaceYaml;}
]
) ++ extraNodeModuleSources;
# Computed values that loop over something
computedDistFiles =
let
packageFileNames = ["pnpm-lock.yaml"] ++
map ({ name, ... }: name) packageFilesWithoutLockfile;
in
distDirs ++
optionals installNodeModules nodeModulesDirs ++
optionals installPackageFiles packageFileNames;
nodeModulesDirs =
if isWorkspace then
["node_modules"] ++ (map (c: "${c}/node_modules") components)
else ["node_modules"];
filterString = concatStringsSep " " (
["--recursive" "--stream"] ++
map (c: "--filter ./${c}") components
) + " ";
buildScripts = ''
pnpm run ${optionalString isWorkspace filterString}${script}
'';
# Flag derived from value computed above, indicating the single dist
# should be copied as $out directly, rather than $out/${distDir}
computedDistDirIsOut =
length computedDistFiles == 1 && distDirIsOut && !isWorkspace;
in
stdenv.mkDerivation (
recursiveUpdate
(rec {
inherit src name nativeBuildInputs;
postUnpack = ''
${optionalString (pnpmWorkspaceYaml != null) ''
cp ${pnpmWorkspaceYaml} pnpm-workspace.yaml
''}
${forEachComponent (component:
''mkdir -p "${component}"'')
}
'';
configurePhase = ''
export HOME=$NIX_BUILD_TOP # Some packages need a writable HOME
export npm_config_nodedir=${nodejs}
runHook preConfigure
${if installInPlace
then passthru.nodeModules.buildPhase
else
forEachConcat (
nodeModulesDir: ''
${copyLink} ${passthru.nodeModules}/${nodeModulesDir}${rsyncSlash} ${nodeModulesDir}
'') nodeModulesDirs
}
runHook postConfigure
'';
buildPhase = ''
${concatStringsSep "\n" (
mapAttrsToList
(n: v: ''export ${n}="${v}"'')
buildEnv
)}
runHook preBuild
${buildScripts}
runHook postBuild
'';
installPhase = ''
runHook preInstall
${if computedDistDirIsOut then ''
${if distDir == "." then "cp -r" else "mv"} ${distDir} $out
''
else ''
mkdir -p $out
${forEachConcat (dDir: ''
cp -r --parents ${dDir} $out
'') computedDistFiles
}
''
}
runHook postInstall
'';
passthru =
let
processResult = processLockfile { inherit registry noDevDependencies; lockfile = pnpmLockYaml; };
in
{
inherit attrs;
patchedLockfile = processResult.patchedLockfile;
patchedLockfileYaml = writeText "pnpm-lock.yaml" (toJSON passthru.patchedLockfile);
pnpmStore = runCommand "${name}-pnpm-store"
{
nativeBuildInputs = [ nodejs pnpm ];
} ''
mkdir -p $out
store=$(pnpm store path)
mkdir -p $(dirname $store)
ln -s $out $(pnpm store path)
pnpm store add ${concatStringsSep " " (unique processResult.dependencyTarballs)}
'';
nodeModules = stdenv.mkDerivation {
name = "${name}-node-modules";
inherit nativeBuildInputs;
unpackPhase = concatStringsSep "\n"
( [ # components is an empty list for non workspace builds
(forEachComponent (component: ''
mkdir -p "${component}"
'')) ] ++
map
(v:
let
nv = if isAttrs v then v else { name = "."; value = v; };
in
"cp -vr \"${nv.value}\" \"${nv.name}\""
)
([{ name = "pnpm-lock.yaml"; value = passthru.patchedLockfileYaml; }]
++ packageFilesWithoutLockfile)
);
buildPhase = ''
export HOME=$NIX_BUILD_TOP # Some packages need a writable HOME
store=$(pnpm store path)
mkdir -p $(dirname $store)
cp -f ${passthru.patchedLockfileYaml} pnpm-lock.yaml
# solve pnpm: EACCES: permission denied, copyfile '/build/.pnpm-store
${if !copyPnpmStore
then "ln -s"
else "cp -RL"
} ${passthru.pnpmStore} $(pnpm store path)
${optionalString copyPnpmStore "chmod -R +w $(pnpm store path)"}
${concatStringsSep "\n" (
mapAttrsToList
(n: v: ''export ${n}="${v}"'')
installEnv
)}
pnpm install --stream ${optionalString noDevDependencies "--prod "}--frozen-lockfile --offline
'';
installPhase = ''
mkdir -p $out
cp -r node_modules/. $out/node_modules
${forEachComponent (component: ''
mkdir -p $out/"${component}"
cp -r "${component}/node_modules" $out/"${component}/node_modules"
'')}
'';
};
};
})
(attrs // { extraNodeModuleSources = null; installEnv = null; buildEnv = null;})
);
}