2014-06-27 15:33:05 +04: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.
|
2019-05-01 04:18:35 +03:00
|
|
|
# A script file must be marked as executable, otherwise it will not be
|
|
|
|
# considered.
|
2014-06-27 15:33:05 +04:00
|
|
|
|
2019-02-07 04:10:07 +03:00
|
|
|
fixupOutputHooks+=(patchShebangsAuto)
|
|
|
|
|
2019-05-01 04:19:02 +03:00
|
|
|
# Run patch shebangs on a directory or file.
|
|
|
|
# Can take multiple paths as arguments.
|
|
|
|
# patchShebangs [--build | --host] PATH...
|
2019-02-07 04:10:07 +03:00
|
|
|
|
|
|
|
# Flags:
|
|
|
|
# --build : Lookup commands available at build-time
|
|
|
|
# --host : Lookup commands available at runtime
|
|
|
|
|
|
|
|
# Example use cases,
|
|
|
|
# $ patchShebangs --host /nix/store/...-hello-1.0/bin
|
|
|
|
# $ patchShebangs --build configure
|
2014-06-27 15:33:05 +04:00
|
|
|
|
|
|
|
patchShebangs() {
|
2019-02-07 04:10:07 +03:00
|
|
|
local pathName
|
|
|
|
|
2020-08-04 08:48:30 +03:00
|
|
|
if [[ "$1" == "--host" ]]; then
|
2019-02-07 04:10:07 +03:00
|
|
|
pathName=HOST_PATH
|
|
|
|
shift
|
2020-08-04 08:48:30 +03:00
|
|
|
elif [[ "$1" == "--build" ]]; then
|
2019-02-07 04:10:07 +03:00
|
|
|
pathName=PATH
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
2019-05-25 17:21:56 +03:00
|
|
|
echo "patching script interpreter paths in $@"
|
2014-06-27 15:33:05 +04:00
|
|
|
local f
|
|
|
|
local oldPath
|
|
|
|
local newPath
|
|
|
|
local arg0
|
|
|
|
local args
|
|
|
|
local oldInterpreterLine
|
|
|
|
local newInterpreterLine
|
|
|
|
|
2020-08-04 08:48:30 +03:00
|
|
|
if [[ $# -eq 0 ]]; then
|
2020-03-21 03:07:26 +03:00
|
|
|
echo "No arguments supplied to patchShebangs" >&2
|
2019-05-25 17:21:56 +03:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2018-09-28 19:21:51 +03:00
|
|
|
local f
|
|
|
|
while IFS= read -r -d $'\0' f; do
|
2018-09-28 19:17:33 +03:00
|
|
|
isScript "$f" || continue
|
2014-06-27 15:33:05 +04:00
|
|
|
|
2020-08-04 08:39:51 +03:00
|
|
|
read -r oldInterpreterLine < "$f"
|
2021-02-09 06:51:42 +03:00
|
|
|
read -r oldPath arg0 args <<< "${oldInterpreterLine:2}"
|
2014-06-27 15:33:05 +04:00
|
|
|
|
2020-08-04 08:48:30 +03:00
|
|
|
if [[ -z "$pathName" ]]; then
|
|
|
|
if [[ -n $strictDeps && $f == "$NIX_STORE"* ]]; then
|
2019-02-07 04:10:07 +03:00
|
|
|
pathName=HOST_PATH
|
|
|
|
else
|
|
|
|
pathName=PATH
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-08-04 08:39:51 +03:00
|
|
|
if [[ "$oldPath" == *"/bin/env" ]]; then
|
2014-06-27 15:33:05 +04:00
|
|
|
# Check for unsupported 'env' functionality:
|
|
|
|
# - options: something starting with a '-'
|
|
|
|
# - environment variables: foo=bar
|
2020-08-04 08:48:30 +03:00
|
|
|
if [[ $arg0 == "-"* || $arg0 == *"="* ]]; then
|
2020-03-21 03:07:26 +03:00
|
|
|
echo "$f: unsupported interpreter directive \"$oldInterpreterLine\" (set dontPatchShebangs=1 and handle shebang patching yourself)" >&2
|
2014-06-27 15:33:05 +04:00
|
|
|
exit 1
|
|
|
|
fi
|
2019-02-07 04:10:07 +03:00
|
|
|
|
|
|
|
newPath="$(PATH="${!pathName}" command -v "$arg0" || true)"
|
2014-06-27 15:33:05 +04:00
|
|
|
else
|
2020-08-04 08:48:30 +03:00
|
|
|
if [[ -z $oldPath ]]; then
|
2014-06-27 15:33:05 +04: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
|
2019-02-07 04:10:07 +03:00
|
|
|
|
|
|
|
newPath="$(PATH="${!pathName}" command -v "$(basename "$oldPath")" || true)"
|
|
|
|
|
2014-06-27 15:33:05 +04:00
|
|
|
args="$arg0 $args"
|
|
|
|
fi
|
|
|
|
|
2016-07-07 23:35:08 +03:00
|
|
|
# Strip trailing whitespace introduced when no arguments are present
|
2020-08-04 08:39:51 +03:00
|
|
|
newInterpreterLine="$newPath $args"
|
|
|
|
newInterpreterLine=${newInterpreterLine%${newInterpreterLine##*[![:space:]]}}
|
2014-06-27 15:33:05 +04:00
|
|
|
|
2020-08-04 08:48:30 +03:00
|
|
|
if [[ -n "$oldPath" && "${oldPath:0:${#NIX_STORE}}" != "$NIX_STORE" ]]; then
|
2021-02-09 06:51:09 +03:00
|
|
|
if [[ -n "$newPath" && "$newPath" != "$oldPath" ]]; then
|
2014-06-27 15:33:05 +04:00
|
|
|
echo "$f: interpreter directive changed from \"$oldInterpreterLine\" to \"$newInterpreterLine\""
|
|
|
|
# escape the escape chars so that sed doesn't interpret them
|
2020-08-04 08:39:51 +03:00
|
|
|
escapedInterpreterLine=${newInterpreterLine//\\/\\\\}
|
|
|
|
|
2018-01-01 20:05:46 +03:00
|
|
|
# Preserve times, see: https://github.com/NixOS/nixpkgs/pull/33281
|
2021-02-09 06:51:09 +03:00
|
|
|
timestamp=$(stat --printf "%y" "$f")
|
2014-06-27 15:33:05 +04:00
|
|
|
sed -i -e "1 s|.*|#\!$escapedInterpreterLine|" "$f"
|
2021-02-09 06:51:09 +03:00
|
|
|
touch --date "$timestamp" "$f"
|
2014-06-27 15:33:05 +04:00
|
|
|
fi
|
|
|
|
fi
|
2019-05-01 04:19:02 +03:00
|
|
|
done < <(find "$@" -type f -perm -0100 -print0)
|
2014-06-27 15:33:05 +04:00
|
|
|
|
|
|
|
stopNest
|
|
|
|
}
|
2019-02-07 04:10:07 +03:00
|
|
|
|
|
|
|
patchShebangsAuto () {
|
2020-08-04 08:48:30 +03:00
|
|
|
if [[ -z "${dontPatchShebangs-}" && -e "$prefix" ]]; then
|
2019-02-07 04:10:07 +03: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).
|
2020-08-04 08:48:30 +03:00
|
|
|
if [[ "$output" != out && "$output" = "$outputDev" ]]; then
|
2019-02-07 04:10:07 +03:00
|
|
|
patchShebangs --build "$prefix"
|
|
|
|
else
|
|
|
|
patchShebangs --host "$prefix"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|