-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathyarn-project.nix.in
159 lines (130 loc) · 4.78 KB
/
yarn-project.nix.in
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
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!
{ lib, nodejs, stdenv, fetchurl, writeText }:
{ src, overrideAttrs ? null, ... } @ args:
let
yarnPath = ./@@YARN_PATH@@;
lockfile = ./@@LOCKFILE@@;
cacheFolder = @@CACHE_FOLDER@@;
# Call overrideAttrs on a derivation if a function is provided.
optionalOverride = fn: drv:
if fn == null then drv else drv.overrideAttrs fn;
# Common attributes between Yarn derivations.
drvCommon = {
# Make sure the build uses the right Node.js version everywhere.
buildInputs = [ nodejs ];
# Tell node-gyp to use the provided Node.js headers for native code builds.
npm_config_nodedir = nodejs;
# Tell node-pre-gyp to never fetch binaries / always build from source.
npm_config_build_from_source = "true";
# Defines the shell alias to run Yarn.
postHook = ''
yarn() {
CI=1 node "${yarnPath}" "$@"
}
'';
};
# Create derivations for fetching dependencies.
cacheDrvs = let
builder = builtins.toFile "builder.sh" ''
source $stdenv/setup
cd "$src"
HOME="$TMP" yarn_cache_folder="$TMP" CI=1 \
node '${yarnPath}' nixify fetch-one $locator
# Because we change the cache dir, Yarn may generate a different name.
mv "$TMP/$(sed 's/-[^-]*\.[^-]*$//' <<< "$outputFilename")"-* $out
'';
in lib.mapAttrs (locator: { filename, sha512 }: stdenv.mkDerivation {
inherit src builder locator;
name = lib.strings.sanitizeDerivationName locator;
buildInputs = [ nodejs ];
outputFilename = filename;
outputHashMode = "flat";
outputHashAlgo = "sha512";
outputHash = sha512;
}) cacheEntries;
# Create a shell snippet to copy dependencies from a list of derivations.
mkCacheBuilderForDrvs = drvs:
writeText "collect-cache.sh" (lib.concatMapStrings (drv: ''
cp ${drv} '${drv.outputFilename}'
'') drvs);
#@@ IF NEED_ISOLATED_BUILD_SUPPRORT
# Create a shell snippet to copy dependencies from a list of locators.
mkCacheBuilderForLocators = let
pickCacheDrvs = map (locator: cacheDrvs.${locator});
in locators:
mkCacheBuilderForDrvs (pickCacheDrvs locators);
# Create a derivation that builds a node-pre-gyp module in isolation.
mkIsolatedBuild = { pname, version, locators }: stdenv.mkDerivation (drvCommon // {
inherit pname version;
phases = [ "buildPhase" "installPhase" ];
buildPhase = ''
mkdir -p .yarn/cache
pushd .yarn/cache > /dev/null
source ${mkCacheBuilderForLocators locators}
popd > /dev/null
echo '{ "dependencies": { "${pname}": "${version}" } }' > package.json
install -m 0600 ${lockfile} ./yarn.lock
export yarn_global_folder="$TMP"
export YARN_ENABLE_IMMUTABLE_INSTALLS=false
yarn --immutable-cache
'';
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
'';
});
#@@ ENDIF NEED_ISOLATED_BUILD_SUPPRORT
# Main project derivation.
project = stdenv.mkDerivation (drvCommon // {
inherit src;
name = @@PROJECT_NAME@@;
# Disable Nixify plugin to save on some unnecessary processing.
yarn_enable_nixify = "false";
configurePhase = ''
# Copy over the Yarn cache.
rm -fr '${cacheFolder}'
mkdir -p '${cacheFolder}'
pushd '${cacheFolder}' > /dev/null
source ${mkCacheBuilderForDrvs (lib.attrValues cacheDrvs)}
popd > /dev/null
# Yarn may need a writable home directory.
export yarn_global_folder="$TMP"
# 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
@@ISOLATED_INTEGRATION@@
# Run normal Yarn install to complete dependency installation.
yarn install --immutable --immutable-cache
runHook postConfigure
'';
buildPhase = ''
runHook preBuild
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p $out/libexec $out/bin
# Move the entire project to the output directory.
mv $PWD "$out/libexec/$sourceRoot"
cd "$out/libexec/$sourceRoot"
# Invoke a plugin internal command to setup binaries.
yarn nixify install-bin $out/bin
runHook postInstall
'';
passthru = {
inherit nodejs;
};
});
@@CACHE_ENTRIES@@
@@ISOLATED@@
in optionalOverride overrideAttrs project