diff --git a/pkgs/build-support/setup-hooks/patch-shebangs.sh b/pkgs/build-support/setup-hooks/patch-shebangs.sh index 316803dd4dd0..7fb6dc86c87a 100644 --- a/pkgs/build-support/setup-hooks/patch-shebangs.sh +++ b/pkgs/build-support/setup-hooks/patch-shebangs.sh @@ -72,12 +72,25 @@ patchShebangs() { _sponge() { local content local target + local restoreReadOnly content="" target="$1" + + # Make file writable if it is read-only + if [[ ! -w "$target" ]]; then + chmod +w "$target" + restoreReadOnly=true + fi + while IFS= read -r line || [[ -n "$line" ]]; do content+="$line"$'\n' done printf '%s' "$content" > "$target" + + # Restore read-only if it was read-only before + if [[ -n "${restoreReadOnly:-}" ]]; then + chmod -w "$target" + fi } local f