Skip to content

Commit

Permalink
stdenv/patchShebangs: use builtins where possible
Browse files Browse the repository at this point in the history
builtins for small input sizes should be faster due to less forking.
  • Loading branch information
Mic92 committed Jan 14, 2021
1 parent b1d838e commit ab4c359
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions pkgs/build-support/setup-hooks/patch-shebangs.sh
Expand Up @@ -50,8 +50,8 @@ patchShebangs() {
while IFS= read -r -d $'\0' f; do
isScript "$f" || continue

oldInterpreterLine=$(head -1 "$f" | tail -c+3)
read -r oldPath arg0 args <<< "$oldInterpreterLine"
read -r oldInterpreterLine < "$f"
read -r oldPath arg0 args <<< "${oldInterpreterLine:3}"

if [ -z "$pathName" ]; then
if [ -n "$strictDeps" ] && [[ "$f" = "$NIX_STORE"* ]]; then
Expand All @@ -61,11 +61,11 @@ patchShebangs() {
fi
fi

if $(echo "$oldPath" | grep -q "/bin/env$"); then
if [[ "$oldPath" == *"/bin/env" ]]; then
# Check for unsupported 'env' functionality:
# - options: something starting with a '-'
# - environment variables: foo=bar
if $(echo "$arg0" | grep -q -- "^-.*\|.*=.*"); then
if [[ "$arg0" == "-"* ]] || [[ "$arg0" == *"="* ]]; then
echo "$f: unsupported interpreter directive \"$oldInterpreterLine\" (set dontPatchShebangs=1 and handle shebang patching yourself)" >&2
exit 1
fi
Expand All @@ -84,13 +84,15 @@ patchShebangs() {
fi

# Strip trailing whitespace introduced when no arguments are present
newInterpreterLine="$(echo "$newPath $args" | sed 's/[[:space:]]*$//')"
newInterpreterLine="$newPath $args"
newInterpreterLine=${newInterpreterLine%${newInterpreterLine##*[![:space:]]}}

if [ -n "$oldPath" -a "${oldPath:0:${#NIX_STORE}}" != "$NIX_STORE" ]; then
if [ -n "$newPath" -a "$newPath" != "$oldPath" ]; then
echo "$f: interpreter directive changed from \"$oldInterpreterLine\" to \"$newInterpreterLine\""
# escape the escape chars so that sed doesn't interpret them
escapedInterpreterLine=$(echo "$newInterpreterLine" | sed 's|\\|\\\\|g')
escapedInterpreterLine=${newInterpreterLine//\\/\\\\}

# Preserve times, see: https://github.com/NixOS/nixpkgs/pull/33281
timestamp=$(mktemp)
touch -r "$f" "$timestamp"
Expand Down

0 comments on commit ab4c359

Please sign in to comment.