1 # This file originates from node2nix
3 {stdenv, nodejs, python2, utillinux, libtool, runCommand, writeTextFile}:
6 python = if nodejs ? python then nodejs.python else python2;
8 # Create a tar wrapper that filters all the 'Ignoring unknown extended header keyword' noise
9 tarWrapper = runCommand "tarWrapper" {} ''
12 cat > $out/bin/tar <<EOF
14 $(type -p tar) "\$@" --warning=no-unknown-keyword
20 # Function that generates a TGZ file from a NPM project
22 { name, version, src, ... }:
25 name = "node-tarball-${name}-${version}";
27 buildInputs = [ nodejs ];
30 tgzFile=$(npm pack | tail -n 1) # Hooks to the pack command will add output (https://docs.npmjs.com/misc/scripts)
33 mkdir -p $out/tarballs
34 mv $tgzFile $out/tarballs
35 mkdir -p $out/nix-support
36 echo "file source-dist $out/tarballs/$tgzFile" >> $out/nix-support/hydra-build-products
40 includeDependencies = {dependencies}:
41 stdenv.lib.optionalString (dependencies != [])
42 (stdenv.lib.concatMapStrings (dependency:
44 # Bundle the dependencies of the package
48 # Only include dependencies if they don't exist. They may also be bundled in the package.
49 if [ ! -e "${dependency.name}" ]
51 ${composePackage dependency}
58 # Recursively composes the dependencies of a package
59 composePackage = { name, packageName, src, dependencies ? [], ... }@args:
66 # Make the base dir in which the target dependency resides first
67 mkdir -p "$(dirname "$DIR/${packageName}")"
71 # Figure out what directory has been unpacked
72 packageDir="$(find . -maxdepth 1 -type d | tail -1)"
74 # Restore write permissions to make building work
75 find "$packageDir" -type d -print0 | xargs -0 chmod u+x
76 chmod -R u+w "$packageDir"
78 # Move the extracted tarball into the output folder
79 mv "$packageDir" "$DIR/${packageName}"
82 # Get a stripped name (without hash) of the source directory.
83 # On old nixpkgs it's already set internally.
84 if [ -z "$strippedName" ]
86 strippedName="$(stripHash ${src})"
89 # Restore write permissions to make building work
90 chmod -R u+w "$strippedName"
92 # Move the extracted directory into the output folder
93 mv "$strippedName" "$DIR/${packageName}"
96 # Unset the stripped name to not confuse the next unpack step
99 # Include the dependencies of the package
100 cd "$DIR/${packageName}"
101 ${includeDependencies { inherit dependencies; }}
103 ${stdenv.lib.optionalString (builtins.substring 0 1 packageName == "@") "cd .."}
106 pinpointDependencies = {dependencies, production}:
108 pinpointDependenciesFromPackageJSON = writeTextFile {
109 name = "pinpointDependencies.js";
111 var fs = require('fs');
112 var path = require('path');
114 function resolveDependencyVersion(location, name) {
115 if(location == process.env['NIX_STORE']) {
118 var dependencyPackageJSON = path.join(location, "node_modules", name, "package.json");
120 if(fs.existsSync(dependencyPackageJSON)) {
121 var dependencyPackageObj = JSON.parse(fs.readFileSync(dependencyPackageJSON));
123 if(dependencyPackageObj.name == name) {
124 return dependencyPackageObj.version;
127 return resolveDependencyVersion(path.resolve(location, ".."), name);
132 function replaceDependencies(dependencies) {
133 if(typeof dependencies == "object" && dependencies !== null) {
134 for(var dependency in dependencies) {
135 var resolvedVersion = resolveDependencyVersion(process.cwd(), dependency);
137 if(resolvedVersion === null) {
138 process.stderr.write("WARNING: cannot pinpoint dependency: "+dependency+", context: "+process.cwd()+"\n");
140 dependencies[dependency] = resolvedVersion;
146 /* Read the package.json configuration */
147 var packageObj = JSON.parse(fs.readFileSync('./package.json'));
149 /* Pinpoint all dependencies */
150 replaceDependencies(packageObj.dependencies);
151 if(process.argv[2] == "development") {
152 replaceDependencies(packageObj.devDependencies);
154 replaceDependencies(packageObj.optionalDependencies);
156 /* Write the fixed package.json file */
157 fs.writeFileSync("package.json", JSON.stringify(packageObj, null, 2));
162 node ${pinpointDependenciesFromPackageJSON} ${if production then "production" else "development"}
164 ${stdenv.lib.optionalString (dependencies != [])
166 if [ -d node_modules ]
169 ${stdenv.lib.concatMapStrings (dependency: pinpointDependenciesOfPackage dependency) dependencies}
175 # Recursively traverses all dependencies of a package and pinpoints all
176 # dependencies in the package.json file to the versions that are actually
179 pinpointDependenciesOfPackage = { packageName, dependencies ? [], production ? true, ... }@args:
181 if [ -d "${packageName}" ]
184 ${pinpointDependencies { inherit dependencies production; }}
186 ${stdenv.lib.optionalString (builtins.substring 0 1 packageName == "@") "cd .."}
190 # Extract the Node.js source code which is used to compile packages with
192 nodeSources = runCommand "node-sources" {} ''
193 tar --no-same-owner --no-same-permissions -xf ${nodejs.src}
197 # Script that adds _integrity fields to all package.json files to prevent NPM from consulting the cache (that is empty)
198 addIntegrityFieldsScript = writeTextFile {
199 name = "addintegrityfields.js";
201 var fs = require('fs');
202 var path = require('path');
204 function augmentDependencies(baseDir, dependencies) {
205 for(var dependencyName in dependencies) {
206 var dependency = dependencies[dependencyName];
208 // Open package.json and augment metadata fields
209 var packageJSONDir = path.join(baseDir, "node_modules", dependencyName);
210 var packageJSONPath = path.join(packageJSONDir, "package.json");
212 if(fs.existsSync(packageJSONPath)) { // Only augment packages that exist. Sometimes we may have production installs in which development dependencies can be ignored
213 console.log("Adding metadata fields to: "+packageJSONPath);
214 var packageObj = JSON.parse(fs.readFileSync(packageJSONPath));
216 if(dependency.integrity) {
217 packageObj["_integrity"] = dependency.integrity;
219 packageObj["_integrity"] = "sha1-000000000000000000000000000="; // When no _integrity string has been provided (e.g. by Git dependencies), add a dummy one. It does not seem to harm and it bypasses downloads.
222 packageObj["_resolved"] = dependency.version; // Set the resolved version to the version identifier. This prevents NPM from cloning Git repositories.
223 fs.writeFileSync(packageJSONPath, JSON.stringify(packageObj, null, 2));
226 // Augment transitive dependencies
227 if(dependency.dependencies !== undefined) {
228 augmentDependencies(packageJSONDir, dependency.dependencies);
233 if(fs.existsSync("./package-lock.json")) {
234 var packageLock = JSON.parse(fs.readFileSync("./package-lock.json"));
236 if(packageLock.lockfileVersion !== 1) {
237 process.stderr.write("Sorry, I only understand lock file version 1!\n");
241 if(packageLock.dependencies !== undefined) {
242 augmentDependencies(".", packageLock.dependencies);
248 # Reconstructs a package-lock file from the node_modules/ folder structure and package.json files with dummy sha1 hashes
249 reconstructPackageLock = writeTextFile {
250 name = "addintegrityfields.js";
252 var fs = require('fs');
253 var path = require('path');
255 var packageObj = JSON.parse(fs.readFileSync("package.json"));
258 name: packageObj.name,
259 version: packageObj.version,
265 function augmentPackageJSON(filePath, dependencies) {
266 var packageJSON = path.join(filePath, "package.json");
267 if(fs.existsSync(packageJSON)) {
268 var packageObj = JSON.parse(fs.readFileSync(packageJSON));
269 dependencies[packageObj.name] = {
270 version: packageObj.version,
271 integrity: "sha1-000000000000000000000000000=",
274 processDependencies(path.join(filePath, "node_modules"), dependencies[packageObj.name].dependencies);
278 function processDependencies(dir, dependencies) {
279 if(fs.existsSync(dir)) {
280 var files = fs.readdirSync(dir);
282 files.forEach(function(entry) {
283 var filePath = path.join(dir, entry);
284 var stats = fs.statSync(filePath);
286 if(stats.isDirectory()) {
287 if(entry.substr(0, 1) == "@") {
288 // When we encounter a namespace folder, augment all packages belonging to the scope
289 var pkgFiles = fs.readdirSync(filePath);
291 pkgFiles.forEach(function(entry) {
292 if(stats.isDirectory()) {
293 var pkgFilePath = path.join(filePath, entry);
294 augmentPackageJSON(pkgFilePath, dependencies);
298 augmentPackageJSON(filePath, dependencies);
305 processDependencies("node_modules", lockObj.dependencies);
307 fs.writeFileSync("package-lock.json", JSON.stringify(lockObj, null, 2));
311 # Builds and composes an NPM package including all its dependencies
320 , dontNpmInstall ? false
321 , bypassCache ? false
324 , unpackPhase ? "true"
325 , buildPhase ? "true"
329 forceOfflineFlag = if bypassCache then "--offline" else "--registry http://www.example.com";
330 extraArgs = removeAttrs args [ "name" "dependencies" "buildInputs" "dontStrip" "dontNpmInstall" "preRebuild" "unpackPhase" "buildPhase" ];
332 stdenv.mkDerivation ({
333 name = "node-${name}-${version}";
334 buildInputs = [ tarWrapper python nodejs ]
335 ++ stdenv.lib.optional (stdenv.isLinux) utillinux
336 ++ stdenv.lib.optional (stdenv.isDarwin) libtool
339 inherit dontStrip; # Stripping may fail a build for some package deployments
340 inherit dontNpmInstall preRebuild unpackPhase buildPhase;
342 compositionScript = composePackage args;
343 pinpointDependenciesScript = pinpointDependenciesOfPackage args;
345 passAsFile = [ "compositionScript" "pinpointDependenciesScript" ];
348 # Create and enter a root node_modules/ folder
349 mkdir -p $out/lib/node_modules
350 cd $out/lib/node_modules
352 # Compose the package and all its dependencies
353 source $compositionScriptPath
355 # Pinpoint the versions of all dependencies to the ones that are actually being used
356 echo "pinpointing versions of dependencies..."
357 source $pinpointDependenciesScriptPath
359 # Patch the shebangs of the bundled modules to prevent them from
360 # calling executables outside the Nix store as much as possible
363 # Deploy the Node.js package by running npm install. Since the
364 # dependencies have been provided already by ourselves, it should not
365 # attempt to install them again, which is good, because we want to make
366 # it Nix's responsibility. If it needs to install any dependencies
367 # anyway (e.g. because the dependency parameters are
368 # incomplete/incorrect), it fails.
370 # The other responsibilities of NPM are kept -- version checks, build
371 # steps, postprocessing etc.
377 ${stdenv.lib.optionalString bypassCache ''
378 if [ ! -f package-lock.json ]
380 echo "No package-lock.json file found, reconstructing..."
381 node ${reconstructPackageLock}
384 node ${addIntegrityFieldsScript}
387 npm ${forceOfflineFlag} --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} rebuild
389 if [ "$dontNpmInstall" != "1" ]
391 # NPM tries to download packages even when they already exist if npm-shrinkwrap is used.
392 rm -f npm-shrinkwrap.json
394 npm ${forceOfflineFlag} --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} install
397 # Create symlink to the deployed executable folder, if applicable
398 if [ -d "$out/lib/node_modules/.bin" ]
400 ln -s $out/lib/node_modules/.bin $out/bin
403 # Create symlinks to the deployed manual page folders, if applicable
404 if [ -d "$out/lib/node_modules/${packageName}/man" ]
407 for dir in "$out/lib/node_modules/${packageName}/man/"*
409 mkdir -p $out/share/man/$(basename "$dir")
412 ln -s $page $out/share/man/$(basename "$dir")
417 # Run post install hook, if provided
422 # Builds a development shell
432 , dontNpmInstall ? false
433 , bypassCache ? false
435 , unpackPhase ? "true"
436 , buildPhase ? "true"
440 forceOfflineFlag = if bypassCache then "--offline" else "--registry http://www.example.com";
442 extraArgs = removeAttrs args [ "name" "dependencies" "buildInputs" ];
444 nodeDependencies = stdenv.mkDerivation ({
445 name = "node-dependencies-${name}-${version}";
447 buildInputs = [ tarWrapper python nodejs ]
448 ++ stdenv.lib.optional (stdenv.isLinux) utillinux
449 ++ stdenv.lib.optional (stdenv.isDarwin) libtool
452 inherit dontStrip; # Stripping may fail a build for some package deployments
453 inherit dontNpmInstall unpackPhase buildPhase;
455 includeScript = includeDependencies { inherit dependencies; };
456 pinpointDependenciesScript = pinpointDependenciesOfPackage args;
458 passAsFile = [ "includeScript" "pinpointDependenciesScript" ];
461 mkdir -p $out/${packageName}
462 cd $out/${packageName}
464 source $includeScriptPath
466 # Create fake package.json to make the npm commands work properly
467 cp ${src}/package.json .
468 chmod 644 package.json
469 ${stdenv.lib.optionalString bypassCache ''
470 if [ -f ${src}/package-lock.json ]
472 cp ${src}/package-lock.json .
476 # Pinpoint the versions of all dependencies to the ones that are actually being used
477 echo "pinpointing versions of dependencies..."
479 ${stdenv.lib.optionalString (builtins.substring 0 1 packageName == "@") "cd .."}
481 source $pinpointDependenciesScriptPath
484 # Patch the shebangs of the bundled modules to prevent them from
485 # calling executables outside the Nix store as much as possible
490 ${stdenv.lib.optionalString bypassCache ''
491 if [ ! -f package-lock.json ]
493 echo "No package-lock.json file found, reconstructing..."
494 node ${reconstructPackageLock}
497 node ${addIntegrityFieldsScript}
500 npm ${forceOfflineFlag} --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} rebuild
502 ${stdenv.lib.optionalString (!dontNpmInstall) ''
503 # NPM tries to download packages even when they already exist if npm-shrinkwrap is used.
504 rm -f npm-shrinkwrap.json
506 npm ${forceOfflineFlag} --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} install
510 ${stdenv.lib.optionalString (builtins.substring 0 1 packageName == "@") "cd .."}
512 mv ${packageName} lib
513 ln -s $out/lib/node_modules/.bin $out/bin
517 stdenv.mkDerivation {
518 name = "node-shell-${name}-${version}";
520 buildInputs = [ python nodejs ] ++ stdenv.lib.optional (stdenv.isLinux) utillinux ++ buildInputs;
523 cat > $out/bin/shell <<EOF
524 #! ${stdenv.shell} -e
528 chmod +x $out/bin/shell
531 # Provide the dependencies in a development shell through the NODE_PATH environment variable
532 inherit nodeDependencies;
533 shellHook = stdenv.lib.optionalString (dependencies != []) ''
534 export NODE_PATH=$nodeDependencies/lib/node_modules
539 buildNodeSourceDist = stdenv.lib.makeOverridable buildNodeSourceDist;
540 buildNodePackage = stdenv.lib.makeOverridable buildNodePackage;
541 buildNodeShell = stdenv.lib.makeOverridable buildNodeShell;