From 98c359b38281f7659945042b8f31ac6162336c12 Mon Sep 17 00:00:00 2001 From: milahu Date: Tue, 14 Sep 2021 15:31:47 +0200 Subject: [PATCH 01/10] fix: patchShebangs in nonexecutable bin files (#106) --- internal.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal.nix b/internal.nix index 0d3754f..436f153 100644 --- a/internal.nix +++ b/internal.nix @@ -1,4 +1,4 @@ -{ nodejs, stdenv, mkShell, lib, fetchurl, writeText, writeTextFile, runCommand, fetchFromGitHub }: +{ nodejs, stdenv, mkShell, lib, fetchurl, writeText, writeTextFile, runCommand, fetchFromGitHub, jq }: rec { default_nodejs = nodejs; @@ -331,6 +331,9 @@ rec { ${preInstallLinkCommands} + cat package.json | jq -r '. | select(.bin != null) | .bin | values[]' | while read binTarget; do + chmod +x "$binTarget" # patchShebangs will only patch executable files + done if grep -I -q -r '/bin/' .; then source $TMP/preinstall-env patchShebangs . @@ -348,6 +351,7 @@ rec { nativeBuildInputs = nativeBuildInputs ++ [ nodejs + jq ]; propagatedBuildInputs = [ From a306d4510945cf66a5aa702b4aa959d79a857f7a Mon Sep 17 00:00:00 2001 From: milahu Date: Tue, 14 Sep 2021 17:01:37 +0200 Subject: [PATCH 02/10] remove cat --- internal.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal.nix b/internal.nix index 436f153..e694fc4 100644 --- a/internal.nix +++ b/internal.nix @@ -331,7 +331,7 @@ rec { ${preInstallLinkCommands} - cat package.json | jq -r '. | select(.bin != null) | .bin | values[]' | while read binTarget; do + jq -r '. | select(.bin != null) | .bin | values[]' package.json | while read binTarget; do chmod +x "$binTarget" # patchShebangs will only patch executable files done if grep -I -q -r '/bin/' .; then From a75e8bca0bc7901b7e5d5bcab2f7e0ecb959937d Mon Sep 17 00:00:00 2001 From: milahu Date: Tue, 14 Sep 2021 18:15:48 +0200 Subject: [PATCH 03/10] fix jq filter --- internal.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal.nix b/internal.nix index e694fc4..a3dd7a7 100644 --- a/internal.nix +++ b/internal.nix @@ -331,7 +331,7 @@ rec { ${preInstallLinkCommands} - jq -r '. | select(.bin != null) | .bin | values[]' package.json | while read binTarget; do + jq -r 'select(.bin != null) | .bin | if type == "string" then . else values[] end' package.json | while read binTarget; do chmod +x "$binTarget" # patchShebangs will only patch executable files done if grep -I -q -r '/bin/' .; then From 48b2eaf329cf5245a1a65161a05ece55f1a5fd96 Mon Sep 17 00:00:00 2001 From: milahu Date: Tue, 14 Sep 2021 18:52:31 +0200 Subject: [PATCH 04/10] ignore root package, throw on error --- internal.nix | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/internal.nix b/internal.nix index a3dd7a7..bb968a3 100644 --- a/internal.nix +++ b/internal.nix @@ -331,9 +331,16 @@ rec { ${preInstallLinkCommands} - jq -r 'select(.bin != null) | .bin | if type == "string" then . else values[] end' package.json | while read binTarget; do - chmod +x "$binTarget" # patchShebangs will only patch executable files - done + # patchShebangs will only patch executable files + if [[ "$(pwd)" != "/build" ]]; then # ignore the root package. bin entries only make sense for dependencies + jq -r 'select(.bin != null) | .bin | if type == "string" then . else values[] end' package.json | while read binTarget; do + if [[ ! -x "$binTarget" ]]; then + echo "make binary executable: $(pwd)/$binTarget" + chmod +x "$binTarget" || exit 1 # on error, throw ELIFECYCLE + fi + done + fi + if grep -I -q -r '/bin/' .; then source $TMP/preinstall-env patchShebangs . From 8c9b89bb405655af2ebc8bcdcbbc96cd8efa1e8a Mon Sep 17 00:00:00 2001 From: milahu Date: Tue, 14 Sep 2021 19:58:40 +0200 Subject: [PATCH 05/10] in function node_modules, ignore the install script for the root package --- internal.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal.nix b/internal.nix index bb968a3..c1af36e 100644 --- a/internal.nix +++ b/internal.nix @@ -214,7 +214,9 @@ rec { dependencies = if (content ? dependencies) then lib.mapAttrs patchDep content.dependencies else { }; devDependencies = if (content ? devDependencies) then lib.mapAttrs patchDep content.devDependencies else { }; in - content // { inherit devDependencies dependencies; }; + content // + { inherit devDependencies dependencies; } // + { scripts = {}; }; # in function node_modules, ignore the install script for the root package # Description: Takes a Path to a package file and returns the patched version as file in the Nix store # Type: Fn -> Path -> Derivation @@ -427,6 +429,7 @@ rec { shellHook = '' # FIXME: we should somehow register a GC root here in case of a symlink? ${add_node_modules_to_cwd nm node_modules_mode} + npm run install # run install script for the root package '' + shellHook; passthru = passthru // { node_modules = nm; @@ -457,6 +460,7 @@ rec { buildPhase = '' runHook preBuild + npm run install # run install script for the root package ${lib.concatStringsSep "\n" buildCommands} runHook postBuild ''; From 40e09dfc4dc687816e8556bb1c83fab859314031 Mon Sep 17 00:00:00 2001 From: milahu Date: Tue, 14 Sep 2021 20:04:17 +0200 Subject: [PATCH 06/10] remove questionmarks from derivation names for mkdir $out --- internal.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal.nix b/internal.nix index c1af36e..e7e502f 100644 --- a/internal.nix +++ b/internal.nix @@ -26,12 +26,12 @@ rec { "0" <= c && c <= "9" || "a" <= c && c <= "z" || "A" <= c && c <= "Z" || - c == "+" || c == "-" || c == "." || c == "_" || c == "?" || c == "="; + c == "+" || c == "-" || c == "." || c == "_" || c == "="; # Description: Converts a npm package name to something that is compatible with nix # Type: String -> String makeValidDrvName = str: - lib.stringAsChars (c: if isValidDrvNameChar c then c else "?") str; + lib.stringAsChars (c: if isValidDrvNameChar c then c else "-") str; # Description: Takes a string of the format "github:org/repo#revision" and returns # an attribute set { org, repo, rev } From ff6d44a58db1124edc30da26f13db4a851318075 Mon Sep 17 00:00:00 2001 From: milahu Date: Tue, 14 Sep 2021 20:37:13 +0200 Subject: [PATCH 07/10] fix: run install script for the root package --- internal.nix | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal.nix b/internal.nix index e7e502f..b00b2c7 100644 --- a/internal.nix +++ b/internal.nix @@ -285,6 +285,16 @@ rec { else throw "sourceHashFunc: spec.type '${spec.type}' is not supported. Supported types: 'github'"; + runInstallScriptsForRootPackage = '' + scriptList=$(jq -r '.scripts | keys[]' package.json) + # https://docs.npmjs.com/cli/v7/using-npm/scripts#npm-install + for script in preinstall install postinstall prepublish preprepare prepare postprepare; do + if ( echo "$scriptList" | grep "^$script$" >/dev/null ); then + npm run $script + fi + done + ''; + node_modules = { src , packageJson ? src + "/package.json" @@ -429,7 +439,7 @@ rec { shellHook = '' # FIXME: we should somehow register a GC root here in case of a symlink? ${add_node_modules_to_cwd nm node_modules_mode} - npm run install # run install script for the root package + ${runInstallScriptsForRootPackage} '' + shellHook; passthru = passthru // { node_modules = nm; @@ -460,7 +470,7 @@ rec { buildPhase = '' runHook preBuild - npm run install # run install script for the root package + ${runInstallScriptsForRootPackage} ${lib.concatStringsSep "\n" buildCommands} runHook postBuild ''; From 60b003cadaa90673f3ab43ff6aca5877f421b021 Mon Sep 17 00:00:00 2001 From: milahu Date: Tue, 14 Sep 2021 20:51:28 +0200 Subject: [PATCH 08/10] print install commands before running them --- internal.nix | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/internal.nix b/internal.nix index b00b2c7..c8d48d6 100644 --- a/internal.nix +++ b/internal.nix @@ -286,13 +286,19 @@ rec { throw "sourceHashFunc: spec.type '${spec.type}' is not supported. Supported types: 'github'"; runInstallScriptsForRootPackage = '' - scriptList=$(jq -r '.scripts | keys[]' package.json) # https://docs.npmjs.com/cli/v7/using-npm/scripts#npm-install + allScripts=$(jq -r '.scripts | keys[]' package.json) + runScripts="" for script in preinstall install postinstall prepublish preprepare prepare postprepare; do - if ( echo "$scriptList" | grep "^$script$" >/dev/null ); then - npm run $script + if ( echo "$allScripts" | grep "^$script$" >/dev/null ); then + runScripts+=" $script" fi done + if [ ! -z "$runScripts" ]; then + echo "run install scripts for root package:" + for script in $runScripts; do echo " npm run $script"; done # make easier to copy-paste + for script in $runScripts; do npm run $script || break; done + fi ''; node_modules = From de6d2c63828574e423e20a8ebe226b345645b41e Mon Sep 17 00:00:00 2001 From: milahu Date: Wed, 15 Sep 2021 07:54:11 +0200 Subject: [PATCH 09/10] fix jq filter: handle missing scripts field --- internal.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal.nix b/internal.nix index c8d48d6..4a0e1ba 100644 --- a/internal.nix +++ b/internal.nix @@ -287,7 +287,7 @@ rec { runInstallScriptsForRootPackage = '' # https://docs.npmjs.com/cli/v7/using-npm/scripts#npm-install - allScripts=$(jq -r '.scripts | keys[]' package.json) + allScripts=$(jq -r 'select(.scripts != null) | .scripts | keys[]' package.json) runScripts="" for script in preinstall install postinstall prepublish preprepare prepare postprepare; do if ( echo "$allScripts" | grep "^$script$" >/dev/null ); then From 99370e3067adc3037e46a224ee787c7e3d29ba50 Mon Sep 17 00:00:00 2001 From: milahu Date: Wed, 15 Sep 2021 09:34:18 +0200 Subject: [PATCH 10/10] in mkShell, dont run install scripts for the root package avoid unnecessary or unwanted rebuilds --- internal.nix | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/internal.nix b/internal.nix index 4a0e1ba..1c46e1c 100644 --- a/internal.nix +++ b/internal.nix @@ -285,7 +285,7 @@ rec { else throw "sourceHashFunc: spec.type '${spec.type}' is not supported. Supported types: 'github'"; - runInstallScriptsForRootPackage = '' + runInstallScriptsForRootPackage = { dontRun ? false }: '' # https://docs.npmjs.com/cli/v7/using-npm/scripts#npm-install allScripts=$(jq -r 'select(.scripts != null) | .scripts | keys[]' package.json) runScripts="" @@ -295,9 +295,13 @@ rec { fi done if [ ! -z "$runScripts" ]; then - echo "run install scripts for root package:" + echo "install scripts for the root package:" for script in $runScripts; do echo " npm run $script"; done # make easier to copy-paste - for script in $runScripts; do npm run $script || break; done + if [[ "${toString dontRun}" == "0" ]]; then + for script in $runScripts; do npm run $script || break; done + else + echo "please run these scripts manually" + fi fi ''; @@ -445,7 +449,7 @@ rec { shellHook = '' # FIXME: we should somehow register a GC root here in case of a symlink? ${add_node_modules_to_cwd nm node_modules_mode} - ${runInstallScriptsForRootPackage} + ${runInstallScriptsForRootPackage { dontRun = true; }} '' + shellHook; passthru = passthru // { node_modules = nm; @@ -476,7 +480,7 @@ rec { buildPhase = '' runHook preBuild - ${runInstallScriptsForRootPackage} + ${runInstallScriptsForRootPackage {}} ${lib.concatStringsSep "\n" buildCommands} runHook postBuild '';