diff --git a/default.nix b/default.nix index 01572f1..1039eb5 100644 --- a/default.nix +++ b/default.nix @@ -1,13 +1,14 @@ -# This file has been generated by node2nix 1.2.0. Do not edit! +# This file has been generated by node2nix 1.6.0. Do not edit! {pkgs ? import { inherit system; - }, system ? builtins.currentSystem, nodejs ? pkgs."nodejs-6_x"}: + }, system ? builtins.currentSystem, nodejs ? pkgs."nodejs-10_x"}: let nodeEnv = import ./node-env.nix { inherit (pkgs) stdenv python2 utillinux runCommand writeTextFile; inherit nodejs; + libtool = if pkgs.stdenv.isDarwin then pkgs.darwin.cctools else null; }; in import ./node-packages.nix { diff --git a/node-env.nix b/node-env.nix index 356e78f..720e0cc 100644 --- a/node-env.nix +++ b/node-env.nix @@ -1,6 +1,6 @@ # This file originates from node2nix -{stdenv, nodejs, python2, utillinux, runCommand, writeTextFile}: +{stdenv, nodejs, python2, utillinux, libtool, runCommand, writeTextFile}: let python = if nodejs ? python then nodejs.python else python2; @@ -27,7 +27,7 @@ let buildInputs = [ nodejs ]; buildPhase = '' export HOME=$TMPDIR - tgzFile=$(npm pack) + tgzFile=$(npm pack | tail -n 1) # Hooks to the pack command will add output (https://docs.npmjs.com/misc/scripts) ''; installPhase = '' mkdir -p $out/tarballs @@ -110,16 +110,16 @@ let text = '' var fs = require('fs'); var path = require('path'); - + function resolveDependencyVersion(location, name) { if(location == process.env['NIX_STORE']) { return null; } else { var dependencyPackageJSON = path.join(location, "node_modules", name, "package.json"); - + if(fs.existsSync(dependencyPackageJSON)) { var dependencyPackageObj = JSON.parse(fs.readFileSync(dependencyPackageJSON)); - + if(dependencyPackageObj.name == name) { return dependencyPackageObj.version; } @@ -128,12 +128,12 @@ let } } } - + function replaceDependencies(dependencies) { if(typeof dependencies == "object" && dependencies !== null) { for(var dependency in dependencies) { var resolvedVersion = resolveDependencyVersion(process.cwd(), dependency); - + if(resolvedVersion === null) { process.stderr.write("WARNING: cannot pinpoint dependency: "+dependency+", context: "+process.cwd()+"\n"); } else { @@ -142,17 +142,17 @@ let } } } - + /* Read the package.json configuration */ var packageObj = JSON.parse(fs.readFileSync('./package.json')); - + /* Pinpoint all dependencies */ replaceDependencies(packageObj.dependencies); if(process.argv[2] == "development") { replaceDependencies(packageObj.devDependencies); } replaceDependencies(packageObj.optionalDependencies); - + /* Write the fixed package.json file */ fs.writeFileSync("package.json", JSON.stringify(packageObj, null, 2)); ''; @@ -160,7 +160,7 @@ let in '' node ${pinpointDependenciesFromPackageJSON} ${if production then "production" else "development"} - + ${stdenv.lib.optionalString (dependencies != []) '' if [ -d node_modules ] @@ -171,11 +171,11 @@ let fi ''} ''; - + # Recursively traverses all dependencies of a package and pinpoints all # dependencies in the package.json file to the versions that are actually # being used. - + pinpointDependenciesOfPackage = { packageName, dependencies ? [], production ? true, ... }@args: '' if [ -d "${packageName}" ] @@ -194,33 +194,164 @@ let mv node-* $out ''; - # Builds and composes an NPM package including all its dependencies - buildNodePackage = { name, packageName, version, dependencies ? [], production ? true, npmFlags ? "", dontNpmInstall ? false, preRebuild ? "", ... }@args: + # Script that adds _integrity fields to all package.json files to prevent NPM from consulting the cache (that is empty) + addIntegrityFieldsScript = writeTextFile { + name = "addintegrityfields.js"; + text = '' + var fs = require('fs'); + var path = require('path'); - stdenv.lib.makeOverridable stdenv.mkDerivation (builtins.removeAttrs args [ "dependencies" ] // { - name = "node-${name}-${version}"; - buildInputs = [ tarWrapper python nodejs ] ++ stdenv.lib.optional (stdenv.isLinux) utillinux ++ args.buildInputs or []; - dontStrip = args.dontStrip or true; # Striping may fail a build for some package deployments + function augmentDependencies(baseDir, dependencies) { + for(var dependencyName in dependencies) { + var dependency = dependencies[dependencyName]; + + // Open package.json and augment metadata fields + var packageJSONDir = path.join(baseDir, "node_modules", dependencyName); + var packageJSONPath = path.join(packageJSONDir, "package.json"); + + if(fs.existsSync(packageJSONPath)) { // Only augment packages that exist. Sometimes we may have production installs in which development dependencies can be ignored + console.log("Adding metadata fields to: "+packageJSONPath); + var packageObj = JSON.parse(fs.readFileSync(packageJSONPath)); - inherit dontNpmInstall preRebuild; + if(dependency.integrity) { + packageObj["_integrity"] = dependency.integrity; + } else { + 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. + } - unpackPhase = args.unpackPhase or "true"; + packageObj["_resolved"] = dependency.version; // Set the resolved version to the version identifier. This prevents NPM from cloning Git repositories. + fs.writeFileSync(packageJSONPath, JSON.stringify(packageObj, null, 2)); + } - buildPhase = args.buildPhase or "true"; + // Augment transitive dependencies + if(dependency.dependencies !== undefined) { + augmentDependencies(packageJSONDir, dependency.dependencies); + } + } + } + + if(fs.existsSync("./package-lock.json")) { + var packageLock = JSON.parse(fs.readFileSync("./package-lock.json")); + + if(packageLock.lockfileVersion !== 1) { + process.stderr.write("Sorry, I only understand lock file version 1!\n"); + process.exit(1); + } + + if(packageLock.dependencies !== undefined) { + augmentDependencies(".", packageLock.dependencies); + } + } + ''; + }; + + # Reconstructs a package-lock file from the node_modules/ folder structure and package.json files with dummy sha1 hashes + reconstructPackageLock = writeTextFile { + name = "addintegrityfields.js"; + text = '' + var fs = require('fs'); + var path = require('path'); + + var packageObj = JSON.parse(fs.readFileSync("package.json")); + + var lockObj = { + name: packageObj.name, + version: packageObj.version, + lockfileVersion: 1, + requires: true, + dependencies: {} + }; + + function augmentPackageJSON(filePath, dependencies) { + var packageJSON = path.join(filePath, "package.json"); + if(fs.existsSync(packageJSON)) { + var packageObj = JSON.parse(fs.readFileSync(packageJSON)); + dependencies[packageObj.name] = { + version: packageObj.version, + integrity: "sha1-000000000000000000000000000=", + dependencies: {} + }; + processDependencies(path.join(filePath, "node_modules"), dependencies[packageObj.name].dependencies); + } + } + + function processDependencies(dir, dependencies) { + if(fs.existsSync(dir)) { + var files = fs.readdirSync(dir); + + files.forEach(function(entry) { + var filePath = path.join(dir, entry); + var stats = fs.statSync(filePath); + + if(stats.isDirectory()) { + if(entry.substr(0, 1) == "@") { + // When we encounter a namespace folder, augment all packages belonging to the scope + var pkgFiles = fs.readdirSync(filePath); + + pkgFiles.forEach(function(entry) { + if(stats.isDirectory()) { + var pkgFilePath = path.join(filePath, entry); + augmentPackageJSON(pkgFilePath, dependencies); + } + }); + } else { + augmentPackageJSON(filePath, dependencies); + } + } + }); + } + } + + processDependencies("node_modules", lockObj.dependencies); + + fs.writeFileSync("package-lock.json", JSON.stringify(lockObj, null, 2)); + ''; + }; + + # Builds and composes an NPM package including all its dependencies + buildNodePackage = + { name + , packageName + , version + , dependencies ? [] + , buildInputs ? [] + , production ? true + , npmFlags ? "" + , dontNpmInstall ? false + , bypassCache ? false + , preRebuild ? "" + , dontStrip ? true + , unpackPhase ? "true" + , buildPhase ? "true" + , ... }@args: + + let + forceOfflineFlag = if bypassCache then "--offline" else "--registry http://www.example.com"; + extraArgs = removeAttrs args [ "name" "dependencies" "buildInputs" "dontStrip" "dontNpmInstall" "preRebuild" "unpackPhase" "buildPhase" ]; + in + stdenv.mkDerivation ({ + name = "node-${name}-${version}"; + buildInputs = [ tarWrapper python nodejs ] + ++ stdenv.lib.optional (stdenv.isLinux) utillinux + ++ stdenv.lib.optional (stdenv.isDarwin) libtool + ++ buildInputs; + + inherit dontStrip; # Stripping may fail a build for some package deployments + inherit dontNpmInstall preRebuild unpackPhase buildPhase; compositionScript = composePackage args; pinpointDependenciesScript = pinpointDependenciesOfPackage args; - + passAsFile = [ "compositionScript" "pinpointDependenciesScript" ]; - installPhase = args.installPhase or '' + installPhase = '' # Create and enter a root node_modules/ folder mkdir -p $out/lib/node_modules cd $out/lib/node_modules # Compose the package and all its dependencies source $compositionScriptPath - + # Pinpoint the versions of all dependencies to the ones that are actually being used echo "pinpointing versions of dependencies..." source $pinpointDependenciesScriptPath @@ -242,14 +373,25 @@ let export HOME=$TMPDIR cd "${packageName}" runHook preRebuild - npm --registry http://www.example.com --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} rebuild + + ${stdenv.lib.optionalString bypassCache '' + if [ ! -f package-lock.json ] + then + echo "No package-lock.json file found, reconstructing..." + node ${reconstructPackageLock} + fi + + node ${addIntegrityFieldsScript} + ''} + + npm ${forceOfflineFlag} --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} rebuild if [ "$dontNpmInstall" != "1" ] then # NPM tries to download packages even when they already exist if npm-shrinkwrap is used. rm -f npm-shrinkwrap.json - npm --registry http://www.example.com --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} install + npm ${forceOfflineFlag} --nodedir=${nodeSources} ${npmFlags} ${stdenv.lib.optionalString production "--production"} install fi # Create symlink to the deployed executable folder, if applicable @@ -275,60 +417,107 @@ let # Run post install hook, if provided runHook postInstall ''; - }); + } // extraArgs); # Builds a development shell - buildNodeShell = { name, packageName, version, src, dependencies ? [], production ? true, npmFlags ? "", dontNpmInstall ? false, ... }@args: + buildNodeShell = + { name + , packageName + , version + , src + , dependencies ? [] + , buildInputs ? [] + , production ? true + , npmFlags ? "" + , dontNpmInstall ? false + , bypassCache ? false + , dontStrip ? true + , unpackPhase ? "true" + , buildPhase ? "true" + , ... }@args: + let - nodeDependencies = stdenv.mkDerivation { + forceOfflineFlag = if bypassCache then "--offline" else "--registry http://www.example.com"; + + extraArgs = removeAttrs args [ "name" "dependencies" "buildInputs" ]; + + nodeDependencies = stdenv.mkDerivation ({ name = "node-dependencies-${name}-${version}"; - buildInputs = [ tarWrapper python nodejs ] ++ stdenv.lib.optional (stdenv.isLinux) utillinux ++ args.buildInputs or []; + buildInputs = [ tarWrapper python nodejs ] + ++ stdenv.lib.optional (stdenv.isLinux) utillinux + ++ stdenv.lib.optional (stdenv.isDarwin) libtool + ++ buildInputs; + + inherit dontStrip; # Stripping may fail a build for some package deployments + inherit dontNpmInstall unpackPhase buildPhase; includeScript = includeDependencies { inherit dependencies; }; pinpointDependenciesScript = pinpointDependenciesOfPackage args; - + passAsFile = [ "includeScript" "pinpointDependenciesScript" ]; - buildCommand = '' - mkdir -p $out/lib - cd $out/lib + installPhase = '' + mkdir -p $out/${packageName} + cd $out/${packageName} + source $includeScriptPath - + + # Create fake package.json to make the npm commands work properly + cp ${src}/package.json . + chmod 644 package.json + ${stdenv.lib.optionalString bypassCache '' + if [ -f ${src}/package-lock.json ] + then + cp ${src}/package-lock.json . + fi + ''} + # Pinpoint the versions of all dependencies to the ones that are actually being used echo "pinpointing versions of dependencies..." - source $pinpointDependenciesScriptPath + cd .. + ${stdenv.lib.optionalString (builtins.substring 0 1 packageName == "@") "cd .."} - # Create fake package.json to make the npm commands work properly - cat > package.json < $out/bin/shell <