patch-shebangs: don't patch shebangs with bash builtins

'command -v builtin' returns 'builtin', which doesn't suit us since
we're looking for program in the given PATH. This could give us shebangs
like this:

    #!builtin

which is surprising.

Switch to 'type -P command' which always returns a path, even if command
is both a builtin and an executable (for example 'test'), or fail is
'command' is just a builtin.
This commit is contained in:
Minijackson 2024-07-26 16:20:14 +02:00
parent df41014acf
commit 24655f7804
No known key found for this signature in database
GPG Key ID: FEA888C9F5D64F62
2 changed files with 18 additions and 4 deletions

View File

@ -90,8 +90,8 @@ patchShebangs() {
if [[ $arg0 == "-S" ]]; then if [[ $arg0 == "-S" ]]; then
arg0=${args%% *} arg0=${args%% *}
args=${args#* } args=${args#* }
newPath="$(PATH="${!pathName}" command -v "env" || true)" newPath="$(PATH="${!pathName}" type -P "env" || true)"
args="-S $(PATH="${!pathName}" command -v "$arg0" || true) $args" args="-S $(PATH="${!pathName}" type -P "$arg0" || true) $args"
# Check for unsupported 'env' functionality: # Check for unsupported 'env' functionality:
# - options: something starting with a '-' besides '-S' # - options: something starting with a '-' besides '-S'
@ -100,7 +100,7 @@ patchShebangs() {
echo "$f: unsupported interpreter directive \"$oldInterpreterLine\" (set dontPatchShebangs=1 and handle shebang patching yourself)" >&2 echo "$f: unsupported interpreter directive \"$oldInterpreterLine\" (set dontPatchShebangs=1 and handle shebang patching yourself)" >&2
exit 1 exit 1
else else
newPath="$(PATH="${!pathName}" command -v "$arg0" || true)" newPath="$(PATH="${!pathName}" type -P "$arg0" || true)"
fi fi
else else
if [[ -z $oldPath ]]; then if [[ -z $oldPath ]]; then
@ -109,7 +109,7 @@ patchShebangs() {
oldPath="/bin/sh" oldPath="/bin/sh"
fi fi
newPath="$(PATH="${!pathName}" command -v "$(basename "$oldPath")" || true)" newPath="$(PATH="${!pathName}" type -P "$(basename "$oldPath")" || true)"
args="$arg0 $args" args="$arg0 $args"
fi fi

View File

@ -87,6 +87,20 @@ let
}; };
}; };
dont-patch-builtins = stdenv.mkDerivation {
name = "dont-patch-builtins";
strictDeps = false;
dontUnpack = true;
installPhase = ''
mkdir -p $out/bin
echo "#!/usr/bin/builtin" > $out/bin/test
chmod +x $out/bin/test
dontPatchShebangs=
'';
passthru = {
assertion = "grep '^#!/usr/bin/builtin' $out/bin/test > /dev/null";
};
};
}; };
in in
stdenv.mkDerivation { stdenv.mkDerivation {