-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathyarn-project.nix
189 lines (153 loc) · 6.13 KB
/
yarn-project.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
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!
{ lib, stdenv, nodejs, git, cacert, fetchurl, writeShellScript, writeShellScriptBin }:
{ src, overrideAttrs ? null, ... } @ args:
let
yarnBin = fetchurl {
url = "https://repo.yarnpkg.com/4.5.3/packages/yarnpkg-cli/bin/yarn.js";
hash = "sha512-MAOhQBLimHBy0kTHIFBlScGqtz7nKCCPGyWAqf1nuS1humsI/pP23OaP13Hjrx5ZoK+ijdJC3QlA1zuV/t1OkA==";
};
cacheFolder = ".yarn/cache";
lockfile = ./yarn.lock;
# Call overrideAttrs on a derivation if a function is provided.
optionalOverride = fn: drv:
if fn == null then drv else drv.overrideAttrs fn;
# Simple stub that provides the global yarn command.
yarn = writeShellScriptBin "yarn" ''
exec '${nodejs}/bin/node' '${yarnBin}' "$@"
'';
# Common attributes between Yarn derivations.
drvCommon = {
# Make sure the build uses the right Node.js version everywhere.
buildInputs = [ nodejs yarn ];
# All dependencies should already be cached.
yarn_enable_network = "0";
# Tell node-gyp to use the provided Node.js headers for native code builds.
npm_config_nodedir = nodejs;
};
# Comman variables that we set in a Nix build, but not in a Nix shell.
buildVars = ''
# Make Yarn produce friendlier logging for automated builds.
export CI=1
# Tell node-pre-gyp to never fetch binaries / always build from source.
export npm_config_build_from_source=true
# Disable Nixify plugin to save on some unnecessary processing.
export yarn_enable_nixify=false
'';
cacheDrv = stdenv.mkDerivation {
name = "yarn-cache";
buildInputs = [ yarn git cacert ];
buildCommand = ''
cp --reflink=auto --recursive '${src}' ./src
cd ./src/
${buildVars}
HOME="$TMP" yarn_enable_global_cache=false yarn_cache_folder="$out" \
yarn nixify fetch
rm $out/.gitignore
'';
outputHashMode = "recursive";
outputHash = "sha512-xjpfv8ahHf6t2Yu+Xp5jZNIhorOtajIJbJLtI+TbA5PEwkefoZcGS9HLB78cVAGgHKoX2lA/cFc17YdNyQN22w==";
};
# Create a derivation that builds a module in isolation.
mkIsolatedBuild = { pname, version, reference, locators ? [] }: stdenv.mkDerivation (drvCommon // {
inherit pname version;
dontUnpack = true;
configurePhase = ''
${buildVars}
unset yarn_enable_nixify # plugin is not present
'';
buildPhase = ''
mkdir -p .yarn/cache
cp --reflink=auto --recursive ${cacheDrv}/* .yarn/cache/
echo '{ "dependencies": { "${pname}": "${reference}" } }' > package.json
install -m 0600 ${lockfile} ./yarn.lock
export yarn_global_folder="$TMP"
export yarn_enable_global_cache=false
export yarn_enable_immutable_installs=false
yarn
'';
installPhase = ''
unplugged=( .yarn/unplugged/${pname}-*/node_modules/* )
if [[ ! -e "''${unplugged[@]}" ]]; then
echo >&2 "Could not find the unplugged path for ${pname}"
exit 1
fi
mv "$unplugged" $out
'';
});
# Main project derivation.
project = stdenv.mkDerivation (drvCommon // {
inherit src;
name = "castlingclub";
configurePhase = ''
${buildVars}
# Copy over the Yarn cache.
rm -fr '${cacheFolder}'
mkdir -p '${cacheFolder}'
cp --reflink=auto --recursive ${cacheDrv}/* '${cacheFolder}/'
# Yarn may need a writable home directory.
export yarn_global_folder="$TMP"
# Ensure global cache is disabled. Cache must be part of our output.
touch .yarnrc.yml
sed -i -e '/^enableGlobalCache/d' .yarnrc.yml
echo 'enableGlobalCache: false' >> .yarnrc.yml
# Some node-gyp calls may call out to npm, which could fail due to an
# read-only home dir.
export HOME="$TMP"
# running preConfigure after the cache is populated allows for
# preConfigure to contain substituteInPlace for dependencies as well as the
# main project. This is necessary for native bindings that maybe have
# hardcoded values.
runHook preConfigure
# Copy in isolated builds.
echo 'injecting build for canvas'
yarn nixify inject-build \
"canvas@npm:2.11.2" \
${isolated."canvas@npm:2.11.2"} \
".yarn/unplugged/canvas-npm-2.11.2-824d893a31/node_modules/canvas"
echo 'running yarn install'
# Run normal Yarn install to complete dependency installation.
yarn install --immutable --immutable-cache
runHook postConfigure
'';
buildPhase = ''
runHook preBuild
runHook postBuild
'';
installPhase = ''
runHook preInstall
# Move the package contents to the output directory.
if grep -q '"workspaces"' package.json; then
# We can't use `yarn pack` in a workspace setup, because it only
# packages the outer workspace.
mkdir -p "$out/libexec"
mv $PWD "$out/libexec/$name"
else
# - If the package.json has a `files` field, only files matching those patterns are copied
# - Otherwise all files are copied.
yarn pack --out package.tgz
mkdir -p "$out/libexec/$name"
tar xzf package.tgz --directory "$out/libexec/$name" --strip-components=1
cp --reflink=auto .yarnrc* "$out/libexec/$name"
cp --reflink=auto ${lockfile} "$out/libexec/$name/yarn.lock"
cp --reflink=auto --recursive .yarn "$out/libexec/$name"
# Copy the Yarn linker output into the package.
cp --reflink=auto .pnp.* "$out/libexec/$name"
fi
cd "$out/libexec/$name"
# Invoke a plugin internal command to setup binaries.
mkdir -p "$out/bin"
yarn nixify install-bin $out/bin
runHook postInstall
'';
passthru = {
inherit nodejs;
yarn-freestanding = yarn;
yarn = writeShellScriptBin "yarn" ''
exec '${yarn}/bin/yarn' --cwd '${overriddenProject}/libexec/${overriddenProject.name}' "$@"
'';
};
});
overriddenProject = optionalOverride overrideAttrs project;
isolated."canvas@npm:2.11.2" = optionalOverride (args.overrideCanvasAttrs or null) (mkIsolatedBuild { pname = "canvas"; version = "2.11.2"; reference = "npm:2.11.2"; });
in overriddenProject