-
-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: patchShebangs in nonexecutable bin files (#106) #107
base: master
Are you sure you want to change the base?
Changes from 8 commits
98c359b
a306d45
a75e8bc
48b2eaf
8c9b89b
40e09df
ff6d44a
60b003c
de6d2c6
99370e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
|
@@ -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 } | ||
|
@@ -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 | ||
|
@@ -283,6 +285,22 @@ rec { | |
else | ||
throw "sourceHashFunc: spec.type '${spec.type}' is not supported. Supported types: 'github'"; | ||
|
||
runInstallScriptsForRootPackage = '' | ||
# 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 "$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 = | ||
{ src | ||
, packageJson ? src + "/package.json" | ||
|
@@ -331,6 +349,16 @@ rec { | |
|
||
${preInstallLinkCommands} | ||
|
||
# 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 | ||
Comment on lines
+356
to
+364
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @milahu At least this part of this PR relies on hook scripts, see the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aah yes, thanks which brings me back to #110 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think being able to patch sources is useful on its own, and it's a relatively easy and reasonable workaround for the hook problem. Patching sources is also very well-known by Nix people already. Implementing an entirely separate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yes, of course. like for example, in nix, we can say {
pname = "puppeteer";
patchPhase = ''
<package.json jq '.scripts.install = ""' | sponge package.json
'';
postInstall = ''
d=$out/node_modules/puppeteer/.local-chromium/linux-970485
mkdir -p $d
ln -s ${puppeteer-chromium} $d/chrome-linux
''; related: patching json files in place
a working prototype is implemented in my pnpm-install-only the complexity is limited by
another bonus: this custom also that project is a working prototype. its just not popular, because of ... reasons. more human reasons than technical reasons |
||
|
||
if grep -I -q -r '/bin/' .; then | ||
source $TMP/preinstall-env | ||
patchShebangs . | ||
|
@@ -348,6 +376,7 @@ rec { | |
|
||
nativeBuildInputs = nativeBuildInputs ++ [ | ||
nodejs | ||
jq | ||
]; | ||
|
||
propagatedBuildInputs = [ | ||
|
@@ -416,6 +445,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} | ||
'' + shellHook; | ||
passthru = passthru // { | ||
node_modules = nm; | ||
|
@@ -446,6 +476,7 @@ rec { | |
|
||
buildPhase = '' | ||
runHook preBuild | ||
${runInstallScriptsForRootPackage} | ||
${lib.concatStringsSep "\n" buildCommands} | ||
runHook postBuild | ||
''; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could move this to a bash function, similar to
buildPhase
instdenv.mkDerivation