forked from nzbr/pnpm2nix-nzbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathderivation.nix
164 lines (136 loc) · 4.63 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
{ lib
, stdenv
, nodejs
, pkg-config
, callPackage
, writeText
, runCommand
, ...
}:
with builtins; with lib; with callPackage ./lockfile.nix { };
let
nodePkg = nodejs;
pkgConfigPkg = pkg-config;
in
{
mkPnpmPackage =
{ src
, packageJSON ? src + "/package.json"
, pnpmLockYaml ? src + "/pnpm-lock.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"
, installInPlace ? false
, installEnv ? { }
, noDevDependencies ? false
, extraNodeModuleSources ? [ ]
, copyPnpmStore ? true
, copyNodeModules ? false
, extraBuildInputs ? [ ]
, nodejs ? nodePkg
, pnpm ? nodejs.pkgs.pnpm
, pkg-config ? pkgConfigPkg
, ...
}@attrs:
let
nativeBuildInputs = [
nodejs
pnpm
pkg-config
] ++ extraBuildInputs;
in
stdenv.mkDerivation (
recursiveUpdate
(rec {
inherit src name nativeBuildInputs;
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 ''
${if !copyNodeModules
then "ln -s"
else "cp -r"
} ${passthru.nodeModules}/node_modules node_modules
''
}
runHook postConfigure
'';
buildPhase = ''
runHook preBuild
pnpm run ${script}
runHook postBuild
'';
installPhase = ''
runHook preInstall
${if distDir == "." then "cp -r" else "mv"} ${distDir} $out
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"
(
map
(v:
let
nv = if isAttrs v then v else { name = "."; value = v; };
in
"cp -vr ${nv.value} ${nv.name}"
)
([
{ name = "package.json"; value = packageJSON; }
{ name = "pnpm-lock.yaml"; value = passthru.patchedLockfileYaml; }
] ++ extraNodeModuleSources)
);
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)
${lib.optionalString copyPnpmStore "chmod -R +w $(pnpm store path)"}
${concatStringsSep "\n" (
mapAttrsToList
(n: v: ''export ${n}="${v}"'')
installEnv
)}
pnpm install ${optionalString noDevDependencies "--prod "}--frozen-lockfile --offline
'';
installPhase = ''
mkdir -p $out
cp -r node_modules/. $out/node_modules
'';
};
};
})
(attrs // { extraNodeModuleSources = null; installEnv = null; })
);
}