This repository has been archived by the owner on May 20, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathdefault.nix
186 lines (152 loc) · 4.99 KB
/
default.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
{ pkgs ? import <nixpkgs> {}
, nodejs ? pkgs.nodejs
, yarn ? pkgs.yarn
}:
pkgs.lib.fix (self: rec {
inherit (pkgs) stdenv lib fetchurl linkFarm;
inherit yarn;
unlessNull = item: alt:
if item == null then alt else item;
# Generates the yarn.nix from the yarn.lock file
mkYarnNix = yarnLock:
pkgs.runCommand "yarn.nix" {}
"${yarn2nix}/bin/yarn2nix ${yarnLock} > $out";
# Loads the generated offline cache. This will be used by yarn as
# the package source.
importOfflineCache = yarnNix:
let
pkg = import yarnNix { inherit fetchurl linkFarm; };
in
pkg.offline_cache;
defaultYarnFlags = [
"--offline"
"--frozen-lockfile"
"--ignore-engines"
"--ignore-scripts"
];
mkYarnModules = {
name,
packageJSON,
yarnLock,
yarnNix ? mkYarnNix yarnLock,
yarnFlags ? defaultYarnFlags,
pkgConfig ? {},
}:
let
offlineCache = importOfflineCache yarnNix;
extraBuildInputs = (lib.flatten (builtins.map (key:
pkgConfig.${key} . buildInputs or []
) (builtins.attrNames pkgConfig)));
postInstall = (builtins.map (key:
if (pkgConfig.${key} ? postInstall) then
''
for f in $(find -L -path '*/node_modules/${key}' -type d); do
(cd "$f" && (${pkgConfig.${key}.postInstall}))
done
''
else
""
) (builtins.attrNames pkgConfig));
in
stdenv.mkDerivation {
inherit name;
phases = ["buildPhase"];
buildInputs = [ yarn nodejs ] ++ extraBuildInputs;
buildPhase = ''
runHook preBuild
# Yarn writes cache directories etc to $HOME.
export HOME=`pwd`/yarn_home
cp ${packageJSON} ./package.json
cp ${yarnLock} ./yarn.lock
chmod +w ./yarn.lock
yarn config --offline set yarn-offline-mirror ${offlineCache}
# Do not look up in the registry, but in the offline cache.
# TODO: Ask upstream to fix this mess.
sed -i -E 's|^(\s*resolved\s*")https?://.*/|\1|' yarn.lock
yarn install ${lib.escapeShellArgs yarnFlags}
${lib.concatStringsSep "\n" postInstall}
mkdir $out
mv node_modules $out/
patchShebangs $out
runHook postBuild
'';
};
# This can be used as a shellHook in mkYarnPackage. It brings the built node_modules into
# the shell-hook environment.
linkNodeModulesHook = ''
if [[ -d node_modules || -L node_modules ]]; then
echo "./node_modules is present. Replacing."
rm -rf node_modules
fi
ln -s "$node_modules" node_modules
'';
mkYarnPackage = {
name ? null,
src,
packageJSON ? src + "/package.json",
yarnLock ? src + "/yarn.lock",
yarnNix ? mkYarnNix yarnLock,
yarnFlags ? defaultYarnFlags,
pkgConfig ? {},
extraBuildInputs ? [],
publishBinsFor ? null,
...
}@attrs:
let
package = lib.importJSON packageJSON;
pname = package.name;
version = package.version;
deps = mkYarnModules {
name = "${pname}-modules-${version}";
inherit packageJSON yarnLock yarnNix yarnFlags pkgConfig;
};
publishBinsFor_ = unlessNull publishBinsFor [pname];
in stdenv.mkDerivation (builtins.removeAttrs attrs ["pkgConfig"] // {
inherit src;
name = unlessNull name "${pname}-${version}";
buildInputs = [ yarn nodejs ] ++ extraBuildInputs;
node_modules = deps + "/node_modules";
configurePhase = attrs.configurePhase or ''
runHook preConfigure
if [ -d npm-packages-offline-cache ]; then
echo "npm-pacakges-offline-cache dir present. Removing."
rm -rf npm-packages-offline-cache
fi
if [[ -d node_modules || -L node_modules ]]; then
echo "./node_modules is present. Removing."
rm -rf node_modules
fi
mkdir -p node_modules
ln -s $node_modules/* node_modules/
ln -s $node_modules/.bin node_modules/
if [ -d node_modules/${pname} ]; then
echo "Error! There is already an ${pname} package in the top level node_modules dir!"
exit 1
fi
runHook postConfigure
'';
# Replace this phase on frontend packages where only the generated
# files are an interesting output.
installPhase = attrs.installPhase or ''
runHook preInstall
mkdir -p $out
cp -r node_modules $out/node_modules
cp -r . $out/node_modules/${pname}
rm -rf $out/node_modules/${pname}/node_modules
mkdir $out/bin
node ${./nix/fixup_bin.js} $out ${lib.concatStringsSep " " publishBinsFor_}
runHook postInstall
'';
passthru = {
inherit package deps;
} // (attrs.passthru or {});
# TODO: populate meta automatically
});
yarn2nix = mkYarnPackage {
src = ./.;
# yarn2nix is the only package that requires the yarnNix option.
# All the other projects can auto-generate that file.
yarnNix = ./yarn.nix;
};
tests = import ./tests { yarn2nix = self; };
})