From 2bd3aa4bc265d34b8ab18e917261b0dc24c5076d Mon Sep 17 00:00:00 2001 From: Andrew Childs Date: Tue, 9 Feb 2021 12:51:09 +0900 Subject: [PATCH 1/3] Revert "Revert "stdenv/patchShebangs: avoid temporary time reference file"" This reverts commit df21fb8afa235a71b4e814f1ed1c4003ce34b220. --- pkgs/build-support/setup-hooks/patch-shebangs.sh | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pkgs/build-support/setup-hooks/patch-shebangs.sh b/pkgs/build-support/setup-hooks/patch-shebangs.sh index bd254be24f9..d0efe039ced 100644 --- a/pkgs/build-support/setup-hooks/patch-shebangs.sh +++ b/pkgs/build-support/setup-hooks/patch-shebangs.sh @@ -88,17 +88,15 @@ patchShebangs() { newInterpreterLine=${newInterpreterLine%${newInterpreterLine##*[![:space:]]}} if [[ -n "$oldPath" && "${oldPath:0:${#NIX_STORE}}" != "$NIX_STORE" ]]; then - if [[ -n "$newPath" ]] && [[ "$newPath" != "$oldPath" ]]; then + if [[ -n "$newPath" && "$newPath" != "$oldPath" ]]; then echo "$f: interpreter directive changed from \"$oldInterpreterLine\" to \"$newInterpreterLine\"" # escape the escape chars so that sed doesn't interpret them escapedInterpreterLine=${newInterpreterLine//\\/\\\\} # Preserve times, see: https://github.com/NixOS/nixpkgs/pull/33281 - timestamp=$(mktemp) - touch -r "$f" "$timestamp" + timestamp=$(stat --printf "%y" "$f") sed -i -e "1 s|.*|#\!$escapedInterpreterLine|" "$f" - touch -r "$timestamp" "$f" - rm "$timestamp" + touch --date "$timestamp" "$f" fi fi done < <(find "$@" -type f -perm -0100 -print0) From ca156a66b75999153d746f57a11a19222fd5cdfb Mon Sep 17 00:00:00 2001 From: Andrew Childs Date: Tue, 9 Feb 2021 12:51:42 +0900 Subject: [PATCH 2/3] stdenv/patchShebangs: fix off by one reading old interpreter This caused shebangs that were already store paths to be rewritten. Introduced by ab4c35982269e8f1f15e07afa7fedf65c749f276 in #94642 Example difference: $ echo "hello world" | tail -c+3 llo world $ str="hello world"; echo ${str:3} lo world --- pkgs/build-support/setup-hooks/patch-shebangs.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/build-support/setup-hooks/patch-shebangs.sh b/pkgs/build-support/setup-hooks/patch-shebangs.sh index d0efe039ced..04ebcd2cc64 100644 --- a/pkgs/build-support/setup-hooks/patch-shebangs.sh +++ b/pkgs/build-support/setup-hooks/patch-shebangs.sh @@ -51,7 +51,7 @@ patchShebangs() { isScript "$f" || continue read -r oldInterpreterLine < "$f" - read -r oldPath arg0 args <<< "${oldInterpreterLine:3}" + read -r oldPath arg0 args <<< "${oldInterpreterLine:2}" if [[ -z "$pathName" ]]; then if [[ -n $strictDeps && $f == "$NIX_STORE"* ]]; then From 93a7e96c87ebb206dd18fb81376be573acb74828 Mon Sep 17 00:00:00 2001 From: Andrew Childs Date: Tue, 9 Feb 2021 12:50:20 +0900 Subject: [PATCH 3/3] tests.patch-shebangs: add case for ignoring store paths --- pkgs/test/patch-shebangs/default.nix | 76 ++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 16 deletions(-) diff --git a/pkgs/test/patch-shebangs/default.nix b/pkgs/test/patch-shebangs/default.nix index 5e1d859c138..5c49787eee3 100644 --- a/pkgs/test/patch-shebangs/default.nix +++ b/pkgs/test/patch-shebangs/default.nix @@ -1,26 +1,70 @@ { lib, stdenv, runCommand }: let - bad-shebang = stdenv.mkDerivation { - name = "bad-shebang"; - dontUnpack = true; - installPhase = '' - mkdir -p $out/bin - echo "#!/bin/sh" > $out/bin/test - echo "echo -n hello" >> $out/bin/test - chmod +x $out/bin/test - ''; + tests = { + bad-shebang = stdenv.mkDerivation { + name = "bad-shebang"; + dontUnpack = true; + installPhase = '' + mkdir -p $out/bin + echo "#!/bin/sh" > $out/bin/test + echo "echo -n hello" >> $out/bin/test + chmod +x $out/bin/test + ''; + passthru = { + assertion = "grep -v '^#!/bin/sh' $out/bin/test > /dev/null"; + }; + }; + + ignores-nix-store = stdenv.mkDerivation { + name = "ignores-nix-store"; + dontUnpack = true; + installPhase = '' + mkdir -p $out/bin + echo "#!$NIX_STORE/path/to/sh" > $out/bin/test + echo "echo -n hello" >> $out/bin/test + chmod +x $out/bin/test + ''; + passthru = { + assertion = "grep \"^#!$NIX_STORE/path/to/sh\" $out/bin/test > /dev/null"; + }; + }; }; in runCommand "patch-shebangs-test" { - passthru = { inherit bad-shebang; }; + passthru = { inherit (tests) bad-shebang ignores-nix-store; }; meta.platforms = lib.platforms.all; } '' - printf "checking whether patchShebangs works properly... ">&2 - if ! grep -q '^#!/bin/sh' ${bad-shebang}/bin/test; then - echo "yes" >&2 - touch $out - else - echo "no" >&2 + validate() { + local name=$1 + local testout=$2 + local assertion=$3 + + echo -n "... $name: " >&2 + + local rc=0 + (out=$testout eval "$assertion") || rc=1 + + if [ "$rc" -eq 0 ]; then + echo "yes" >&2 + else + echo "no" >&2 + fi + + return "$rc" + } + + echo "checking whether patchShebangs works properly... ">&2 + + fail= + ${lib.concatStringsSep "\n" (lib.mapAttrsToList (_: test: '' + validate "${test.name}" "${test}" ${lib.escapeShellArg test.assertion} || fail=1 + '') tests)} + + if [ "$fail" ]; then + echo "failed" exit 1 + else + echo "succeeded" + touch $out fi ''