2020-04-24 23:36:52 +00:00
|
|
|
# This setup hook causes the fixup phase to rewrite all script
|
|
|
|
# interpreter file names (`#! /path') to paths found in $PATH. E.g.,
|
|
|
|
# /bin/sh will be rewritten to /nix/store/<hash>-some-bash/bin/sh.
|
|
|
|
# /usr/bin/env gets special treatment so that ".../bin/env python" is
|
|
|
|
# rewritten to /nix/store/<hash>/bin/python. Interpreters that are
|
|
|
|
# already in the store are left untouched.
|
|
|
|
# A script file must be marked as executable, otherwise it will not be
|
|
|
|
# considered.
|
|
|
|
|
|
|
|
fixupOutputHooks+=(patchShebangsAuto)
|
|
|
|
|
|
|
|
# Run patch shebangs on a directory or file.
|
|
|
|
# Can take multiple paths as arguments.
|
2023-08-22 20:05:09 +00:00
|
|
|
# patchShebangs [--build | --host | --update] [--] PATH...
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Flags:
|
|
|
|
# --build : Lookup commands available at build-time
|
|
|
|
# --host : Lookup commands available at runtime
|
2023-08-22 20:05:09 +00:00
|
|
|
# --update : Update shebang paths that are in Nix store
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Example use cases,
|
|
|
|
# $ patchShebangs --host /nix/store/...-hello-1.0/bin
|
|
|
|
# $ patchShebangs --build configure
|
|
|
|
|
|
|
|
patchShebangs() {
|
|
|
|
local pathName
|
2023-08-22 20:05:09 +00:00
|
|
|
local update
|
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case "$1" in
|
|
|
|
--host)
|
|
|
|
pathName=HOST_PATH
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--build)
|
|
|
|
pathName=PATH
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--update)
|
|
|
|
update=true
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--)
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
-*|--*)
|
|
|
|
echo "Unknown option $1 supplied to patchShebangs" >&2
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
echo "patching script interpreter paths in $@"
|
|
|
|
local f
|
|
|
|
local oldPath
|
|
|
|
local newPath
|
|
|
|
local arg0
|
|
|
|
local args
|
|
|
|
local oldInterpreterLine
|
|
|
|
local newInterpreterLine
|
|
|
|
|
2021-02-19 19:06:45 +00:00
|
|
|
if [[ $# -eq 0 ]]; then
|
2020-04-24 23:36:52 +00:00
|
|
|
echo "No arguments supplied to patchShebangs" >&2
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
local f
|
|
|
|
while IFS= read -r -d $'\0' f; do
|
|
|
|
isScript "$f" || continue
|
|
|
|
|
2021-02-19 19:06:45 +00:00
|
|
|
read -r oldInterpreterLine < "$f"
|
|
|
|
read -r oldPath arg0 args <<< "${oldInterpreterLine:2}"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
if [[ -z "${pathName:-}" ]]; then
|
2021-02-19 19:06:45 +00:00
|
|
|
if [[ -n $strictDeps && $f == "$NIX_STORE"* ]]; then
|
2020-04-24 23:36:52 +00:00
|
|
|
pathName=HOST_PATH
|
|
|
|
else
|
|
|
|
pathName=PATH
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2021-02-19 19:06:45 +00:00
|
|
|
if [[ "$oldPath" == *"/bin/env" ]]; then
|
2023-03-15 16:39:30 +00:00
|
|
|
if [[ $arg0 == "-S" ]]; then
|
|
|
|
arg0=${args%% *}
|
|
|
|
args=${args#* }
|
|
|
|
newPath="$(PATH="${!pathName}" command -v "env" || true)"
|
|
|
|
args="-S $(PATH="${!pathName}" command -v "$arg0" || true) $args"
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
# Check for unsupported 'env' functionality:
|
2023-03-15 16:39:30 +00:00
|
|
|
# - options: something starting with a '-' besides '-S'
|
2020-04-24 23:36:52 +00:00
|
|
|
# - environment variables: foo=bar
|
2023-03-15 16:39:30 +00:00
|
|
|
elif [[ $arg0 == "-"* || $arg0 == *"="* ]]; then
|
2020-04-24 23:36:52 +00:00
|
|
|
echo "$f: unsupported interpreter directive \"$oldInterpreterLine\" (set dontPatchShebangs=1 and handle shebang patching yourself)" >&2
|
|
|
|
exit 1
|
2023-03-15 16:39:30 +00:00
|
|
|
else
|
|
|
|
newPath="$(PATH="${!pathName}" command -v "$arg0" || true)"
|
2020-04-24 23:36:52 +00:00
|
|
|
fi
|
|
|
|
else
|
2021-02-19 19:06:45 +00:00
|
|
|
if [[ -z $oldPath ]]; then
|
2020-04-24 23:36:52 +00:00
|
|
|
# If no interpreter is specified linux will use /bin/sh. Set
|
|
|
|
# oldpath="/bin/sh" so that we get /nix/store/.../sh.
|
|
|
|
oldPath="/bin/sh"
|
|
|
|
fi
|
|
|
|
|
|
|
|
newPath="$(PATH="${!pathName}" command -v "$(basename "$oldPath")" || true)"
|
|
|
|
|
|
|
|
args="$arg0 $args"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Strip trailing whitespace introduced when no arguments are present
|
2021-02-19 19:06:45 +00:00
|
|
|
newInterpreterLine="$newPath $args"
|
|
|
|
newInterpreterLine=${newInterpreterLine%${newInterpreterLine##*[![:space:]]}}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-22 20:05:09 +00:00
|
|
|
if [[ -n "$oldPath" && ( "$update" == true || "${oldPath:0:${#NIX_STORE}}" != "$NIX_STORE" ) ]]; then
|
2021-02-19 19:06:45 +00:00
|
|
|
if [[ -n "$newPath" && "$newPath" != "$oldPath" ]]; then
|
2020-04-24 23:36:52 +00:00
|
|
|
echo "$f: interpreter directive changed from \"$oldInterpreterLine\" to \"$newInterpreterLine\""
|
|
|
|
# escape the escape chars so that sed doesn't interpret them
|
2021-02-19 19:06:45 +00:00
|
|
|
escapedInterpreterLine=${newInterpreterLine//\\/\\\\}
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
# Preserve times, see: https://github.com/NixOS/nixpkgs/pull/33281
|
2021-02-19 19:06:45 +00:00
|
|
|
timestamp=$(stat --printf "%y" "$f")
|
2020-04-24 23:36:52 +00:00
|
|
|
sed -i -e "1 s|.*|#\!$escapedInterpreterLine|" "$f"
|
2021-02-19 19:06:45 +00:00
|
|
|
touch --date "$timestamp" "$f"
|
2020-04-24 23:36:52 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done < <(find "$@" -type f -perm -0100 -print0)
|
|
|
|
}
|
|
|
|
|
|
|
|
patchShebangsAuto () {
|
2021-02-19 19:06:45 +00:00
|
|
|
if [[ -z "${dontPatchShebangs-}" && -e "$prefix" ]]; then
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Dev output will end up being run on the build platform. An
|
|
|
|
# example case of this is sdl2-config. Otherwise, we can just
|
|
|
|
# use the runtime path (--host).
|
2021-02-19 19:06:45 +00:00
|
|
|
if [[ "$output" != out && "$output" = "$outputDev" ]]; then
|
2020-04-24 23:36:52 +00:00
|
|
|
patchShebangs --build "$prefix"
|
|
|
|
else
|
|
|
|
patchShebangs --host "$prefix"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|