@@ -6,19 +6,19 @@ let
66 # Create a tar wrapper that filters all the 'Ignoring unknown extended header keyword' noise
77 tarWrapper = runCommand "tarWrapper" {} ''
88 mkdir -p $out/bin
9−
9+
1010 cat > $out/bin/tar <<EOF
1111 #! ${stdenv.shell} -e
1212 $(type -p tar) "\$@" --warning=no-unknown-keyword
1313 EOF
14−
14+
1515 chmod +x $out/bin/tar
1616 '';
17−
17+
1818 # Function that generates a TGZ file from a NPM project
1919 buildNodeSourceDist =
2020 { name, version, src, ... }:
21−
21+
2222 stdenv.mkDerivation {
2323 name = "node-tarball-${name}-${version}";
2424 inherit src;
@@ -42,13 +42,13 @@ let
4242 # Bundle the dependencies of the package
4343 mkdir -p node_modules
4444 cd node_modules
45−
45+
4646 # Only include dependencies if they don't exist. They may also be bundled in the package.
4747 if [ ! -e "${dependency.name}" ]
4848 then
4949 ${composePackage dependency}
5050 fi
51−
51+
5252 cd ..
5353 ''
5454 ) dependencies);
@@ -61,13 +61,13 @@ let
6161 text = ''
6262 var fs = require('fs');
6363 var url = require('url');
64−
64+
6565 /*
6666 * Replaces an impure version specification by *
6767 */
6868 function replaceImpureVersionSpec(versionSpec) {
6969 var parsedUrl = url.parse(versionSpec);
70−
70+
7171 if(versionSpec == "latest" || versionSpec == "unstable" ||
7272 versionSpec.substr(0, 2) == ".." || dependency.substr(0, 2) == "./" || dependency.substr(0, 2) == "~/" || dependency.substr(0, 1) == '/')
7373 return '*';
@@ -77,9 +77,9 @@ let
7777 else
7878 return versionSpec;
7979 }
80−
80+
8181 var packageObj = JSON.parse(fs.readFileSync('./package.json'));
82−
82+
8383 /* Replace dependencies */
8484 if(packageObj.dependencies !== undefined) {
8585 for(var dependency in packageObj.dependencies) {
@@ -87,7 +87,7 @@ let
8787 packageObj.dependencies[dependency] = replaceImpureVersionSpec(versionSpec);
8888 }
8989 }
90−
90+
9191 /* Replace development dependencies */
9292 if(packageObj.devDependencies !== undefined) {
9393 for(var dependency in packageObj.devDependencies) {
@@ -95,7 +95,7 @@ let
9595 packageObj.devDependencies[dependency] = replaceImpureVersionSpec(versionSpec);
9696 }
9797 }
98−
98+
9999 /* Replace optional dependencies */
100100 if(packageObj.optionalDependencies !== undefined) {
101101 for(var dependency in packageObj.optionalDependencies) {
@@ -103,7 +103,7 @@ let
103103 packageObj.optionalDependencies[dependency] = replaceImpureVersionSpec(versionSpec);
104104 }
105105 }
106−
106+
107107 /* Write the fixed JSON file */
108108 fs.writeFileSync("package.json", JSON.stringify(packageObj));
109109 '';
@@ -112,9 +112,9 @@ let
112112 ''
113113 DIR=$(pwd)
114114 cd $TMPDIR
115−
115+
116116 unpackFile ${src}
117−
117+
118118 # Make the base dir in which the target dependency resides first
119119 mkdir -p "$(dirname "$DIR/${packageName}")"
120120
@@ -122,11 +122,11 @@ let
122122 then
123123 # Figure out what directory has been unpacked
124124 packageDir="$(find . -type d -maxdepth 1 | tail -1)"
125−
125+
126126 # Restore write permissions to make building work
127127 find "$packageDir" -type d -print0 | xargs -0 chmod u+x
128128 chmod -R u+w "$packageDir"
129−
129+
130130 # Move the extracted tarball into the output folder
131131 mv "$packageDir" "$DIR/${packageName}"
132132 elif [ -d "${src}" ]
@@ -135,7 +135,7 @@ let
135135
136136 # Restore write permissions to make building work
137137 chmod -R u+w $strippedName
138−
138+
139139 # Move the extracted directory into the output folder
140140 mv $strippedName "$DIR/${packageName}"
141141 fi
@@ -144,7 +144,7 @@ let
144144 # The following JavaScript replaces these by * to prevent that
145145 cd "$DIR/${packageName}"
146146 node ${fixImpureDependencies}
147−
147+
148148 # Include the dependencies of the package
149149 ${includeDependencies { inherit dependencies; }}
150150 cd ..
@@ -157,36 +157,36 @@ let
157157 tar --no-same-owner --no-same-permissions -xf ${nodejs.src}
158158 mv node-* $out
159159 '';
160−
160+
161161 # Builds and composes an NPM package including all its dependencies
162162 buildNodePackage = { name, packageName, version, dependencies ? [], production ? true, npmFlags ? "", dontNpmInstall ? false, preRebuild ? "", ... }@args:
163−
163+
164164 stdenv.lib.makeOverridable stdenv.mkDerivation (builtins.removeAttrs args [ "dependencies" ] // {
165165 name = "node-${name}-${version}";
166166 buildInputs = [ tarWrapper python nodejs ] ++ stdenv.lib.optional (stdenv.isLinux) utillinux ++ args.buildInputs or [];
167167 dontStrip = args.dontStrip or true; # Striping may fail a build for some package deployments
168−
168+
169169 inherit dontNpmInstall preRebuild;
170−
170+
171171 unpackPhase = args.unpackPhase or "true";
172−
172+
173173 buildPhase = args.buildPhase or "true";
174−
174+
175175 compositionScript = composePackage args;
176176 passAsFile = [ "compositionScript" ];
177−
177+
178178 installPhase = args.installPhase or ''
179179 # Create and enter a root node_modules/ folder
180180 mkdir -p $out/lib/node_modules
181181 cd $out/lib/node_modules
182−
182+
183183 # Compose the package and all its dependencies
184184 source $compositionScriptPath
185−
185+
186186 # Patch the shebangs of the bundled modules to prevent them from
187187 # calling executables outside the Nix store as much as possible
188188 patchShebangs .
189−
189+
190190 # Deploy the Node.js package by running npm install. Since the
191191 # dependencies have been provided already by ourselves, it should not
192192 # attempt to install them again, which is good, because we want to make
@@ -196,23 +196,23 @@ let
196196 #
197197 # The other responsibilities of NPM are kept -- version checks, build
198198 # steps, postprocessing etc.
199−
199+
200200 export HOME=$TMPDIR
201201 cd "${packageName}"
202202 runHook preRebuild
203203 npm --registry http://www.example.com --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} rebuild
204−
204+
205205 if [ "$dontNpmInstall" != "1" ]
206206 then
207207 npm --registry http://www.example.com --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} install
208208 fi
209−
209+
210210 # Create symlink to the deployed executable folder, if applicable
211211 if [ -d "$out/lib/node_modules/.bin" ]
212212 then
213213 ln -s $out/lib/node_modules/.bin $out/bin
214214 fi
215−
215+
216216 # Create symlinks to the deployed manual page folders, if applicable
217217 if [ -d "$out/lib/node_modules/${packageName}/man" ]
218218 then
@@ -226,7 +226,7 @@ let
226226 done
227227 done
228228 fi
229−
229+
230230 # Run post install hook, if provided
231231 runHook postInstall
232232 '';
@@ -237,17 +237,17 @@ let
237237 let
238238 nodeDependencies = stdenv.mkDerivation {
239239 name = "node-dependencies-${name}-${version}";
240−
240+
241241 buildInputs = [ tarWrapper python nodejs ] ++ stdenv.lib.optional (stdenv.isLinux) utillinux ++ args.buildInputs or [];
242−
242+
243243 includeScript = includeDependencies { inherit dependencies; };
244244 passAsFile = [ "includeScript" ];
245−
245+
246246 buildCommand = ''
247247 mkdir -p $out/lib
248248 cd $out/lib
249249 source $includeScriptPath
250−
250+
251251 # Create fake package.json to make the npm commands work properly
252252 cat > package.json <<EOF
253253 {
@@ -255,14 +255,14 @@ let
255255 "version": "${version}"
256256 }
257257 EOF
258−
258+
259259 # Patch the shebangs of the bundled modules to prevent them from
260260 # calling executables outside the Nix store as much as possible
261261 patchShebangs .
262−
262+
263263 export HOME=$TMPDIR
264264 npm --registry http://www.example.com --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} rebuild
265−
265+
266266 ${stdenv.lib.optionalString (!dontNpmInstall) ''
267267 npm --registry http://www.example.com --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} install
268268 ''}
@@ -273,7 +273,7 @@ let
273273 in
274274 stdenv.lib.makeOverridable stdenv.mkDerivation {
275275 name = "node-shell-${name}-${version}";
276−
276+
277277 buildInputs = [ python nodejs ] ++ stdenv.lib.optional (stdenv.isLinux) utillinux ++ args.buildInputs or [];
278278 buildCommand = ''
279279 mkdir -p $out/bin
@@ -284,7 +284,7 @@ let
284284 EOF
285285 chmod +x $out/bin/shell
286286 '';
287−
287+
288288 # Provide the dependencies in a development shell through the NODE_PATH environment variable
289289 inherit nodeDependencies;
290290 shellHook = stdenv.lib.optionalString (dependencies != []) ''