From 792907e3bf429784a42ca57805025f2737c98eed Mon Sep 17 00:00:00 2001 From: Timothy DeHerrera Date: Sat, 17 Sep 2022 18:35:07 -0600 Subject: [PATCH 1/3] patch-shebangs: handle env -S shebangs `env -S` allows one to set more than one argument in a shebang. This small patch allows the patch-shebangs hook to handle this case appropriately. --- pkgs/build-support/setup-hooks/patch-shebangs.sh | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pkgs/build-support/setup-hooks/patch-shebangs.sh b/pkgs/build-support/setup-hooks/patch-shebangs.sh index a26113abea88..9a48440debec 100644 --- a/pkgs/build-support/setup-hooks/patch-shebangs.sh +++ b/pkgs/build-support/setup-hooks/patch-shebangs.sh @@ -62,15 +62,21 @@ patchShebangs() { fi if [[ "$oldPath" == *"/bin/env" ]]; then + if [[ $arg0 == "-S" ]]; then + arg0=${args%% *} + args=${args#* } + newPath="$(PATH="${!pathName}" command -v "env" || true)" + args="-S $(PATH="${!pathName}" command -v "$arg0" || true) $args" + # Check for unsupported 'env' functionality: - # - options: something starting with a '-' + # - options: something starting with a '-' besides '-S' # - environment variables: foo=bar - if [[ $arg0 == "-"* || $arg0 == *"="* ]]; then + elif [[ $arg0 == "-"* || $arg0 == *"="* ]]; then echo "$f: unsupported interpreter directive \"$oldInterpreterLine\" (set dontPatchShebangs=1 and handle shebang patching yourself)" >&2 exit 1 + else + newPath="$(PATH="${!pathName}" command -v "$arg0" || true)" fi - - newPath="$(PATH="${!pathName}" command -v "$arg0" || true)" else if [[ -z $oldPath ]]; then # If no interpreter is specified linux will use /bin/sh. Set From 8876a5c91faf35e37e08d647dd6e9ea9d4a4f79f Mon Sep 17 00:00:00 2001 From: Artturin Date: Tue, 17 Jan 2023 04:20:36 +0200 Subject: [PATCH 2/3] tests.stdenv: move patch-shebangs test --- pkgs/test/default.nix | 2 -- pkgs/test/stdenv/default.nix | 4 ++-- pkgs/test/stdenv/hooks.nix | 4 ++-- .../default.nix => stdenv/patch-shebangs.nix} | 0 pkgs/top-level/release.nix | 6 +++--- 5 files changed, 7 insertions(+), 9 deletions(-) rename pkgs/test/{patch-shebangs/default.nix => stdenv/patch-shebangs.nix} (100%) diff --git a/pkgs/test/default.nix b/pkgs/test/default.nix index 71d065179d17..5d154d1630b6 100644 --- a/pkgs/test/default.nix +++ b/pkgs/test/default.nix @@ -63,8 +63,6 @@ with pkgs; overriding = callPackage ./overriding.nix { }; - patch-shebangs = callPackage ./patch-shebangs {}; - texlive = callPackage ./texlive {}; cuda = callPackage ./cuda { }; diff --git a/pkgs/test/stdenv/default.nix b/pkgs/test/stdenv/default.nix index bad6eb7ba7aa..91964213bbc6 100644 --- a/pkgs/test/stdenv/default.nix +++ b/pkgs/test/stdenv/default.nix @@ -98,7 +98,7 @@ in { # tests for hooks in `stdenv.defaultNativeBuildInputs` - hooks = lib.recurseIntoAttrs (import ./hooks.nix { stdenv = bootStdenv; pkgs = earlyPkgs; }); + hooks = lib.recurseIntoAttrs (import ./hooks.nix { stdenv = bootStdenv; pkgs = earlyPkgs; inherit lib; }); outputs-no-out = runCommand "outputs-no-out-assert" { result = testers.testBuildFailure (stdenv.mkDerivation { @@ -158,7 +158,7 @@ in structuredAttrsByDefault = lib.recurseIntoAttrs { - hooks = lib.recurseIntoAttrs (import ./hooks.nix { stdenv = bootStdenvStructuredAttrsByDefault; pkgs = earlyPkgs; }); + hooks = lib.recurseIntoAttrs (import ./hooks.nix { stdenv = bootStdenvStructuredAttrsByDefault; pkgs = earlyPkgs; inherit lib; }); test-cc-wrapper-substitutions = ccWrapperSubstitutionsTest { name = "test-cc-wrapper-substitutions-structuredAttrsByDefault"; diff --git a/pkgs/test/stdenv/hooks.nix b/pkgs/test/stdenv/hooks.nix index 3d72efae6c47..eb1b3f61bda6 100644 --- a/pkgs/test/stdenv/hooks.nix +++ b/pkgs/test/stdenv/hooks.nix @@ -1,4 +1,4 @@ -{ stdenv, pkgs }: +{ stdenv, pkgs, lib }: # ordering should match defaultNativeBuildInputs @@ -91,7 +91,7 @@ ''; }; # TODO: add multiple-outputs - # TODO: move patch-shebangs test from pkgs/test/patch-shebangs/default.nix to here + patch-shebangs = import ./patch-shebangs.nix { inherit stdenv lib pkgs; }; prune-libtool-files = let libFoo = pkgs.writeText "libFoo" '' diff --git a/pkgs/test/patch-shebangs/default.nix b/pkgs/test/stdenv/patch-shebangs.nix similarity index 100% rename from pkgs/test/patch-shebangs/default.nix rename to pkgs/test/stdenv/patch-shebangs.nix diff --git a/pkgs/top-level/release.nix b/pkgs/top-level/release.nix index 374a308ac141..c22ea7f503f4 100644 --- a/pkgs/top-level/release.nix +++ b/pkgs/top-level/release.nix @@ -82,7 +82,7 @@ let jobs.tests.cc-wrapper-libcxx.x86_64-darwin jobs.tests.stdenv-inputs.x86_64-darwin jobs.tests.macOSSierraShared.x86_64-darwin - jobs.tests.patch-shebangs.x86_64-darwin + jobs.tests.stdenv.hooks.patch-shebangs.x86_64-darwin */ ]; } else null; @@ -127,7 +127,7 @@ let jobs.tests.cc-multilib-gcc.x86_64-linux jobs.tests.cc-multilib-clang.x86_64-linux jobs.tests.stdenv-inputs.x86_64-linux - jobs.tests.patch-shebangs.x86_64-linux + jobs.tests.stdenv.hooks.patch-shebangs.x86_64-linux */ ] # FIXME: reintroduce aarch64-darwin after this builds again @@ -158,7 +158,7 @@ let jobs.tests.cc-wrapper-libcxx-6.x86_64-darwin jobs.tests.stdenv-inputs.x86_64-darwin jobs.tests.macOSSierraShared.x86_64-darwin - jobs.tests.patch-shebangs.x86_64-darwin + jobs.tests.stdenv.hooks.patch-shebangs.x86_64-darwin */ ]; }; From 883daacbaaabbf826bffe9a414b06e1d935e3133 Mon Sep 17 00:00:00 2001 From: Artturin Date: Tue, 17 Jan 2023 07:55:51 +0200 Subject: [PATCH 3/3] tests.stdenv: add hooks.patch-shebangs.split-string & tweak tests --- pkgs/test/stdenv/patch-shebangs.nix | 106 ++++++++++++++++++---------- 1 file changed, 67 insertions(+), 39 deletions(-) diff --git a/pkgs/test/stdenv/patch-shebangs.nix b/pkgs/test/stdenv/patch-shebangs.nix index 5c49787eee3b..fb52f38ecc91 100644 --- a/pkgs/test/stdenv/patch-shebangs.nix +++ b/pkgs/test/stdenv/patch-shebangs.nix @@ -1,70 +1,98 @@ -{ lib, stdenv, runCommand }: +{ lib, stdenv, pkgs }: + +# since the tests are using a early stdenv, the stdenv will have dontPatchShebangs=1, so it has to be unset +# https://github.com/NixOS/nixpkgs/blob/768a982bfc9d29a6bd3beb963ed4b054451ce3d0/pkgs/stdenv/linux/default.nix#L148-L153 + +# strictDeps has to be disabled because the shell isn't in buildInputs let tests = { bad-shebang = stdenv.mkDerivation { - name = "bad-shebang"; + name = "bad-shebang"; + strictDeps = false; dontUnpack = true; installPhase = '' mkdir -p $out/bin - echo "#!/bin/sh" > $out/bin/test + echo "#!/bin/bash" > $out/bin/test echo "echo -n hello" >> $out/bin/test chmod +x $out/bin/test + dontPatchShebangs= ''; passthru = { - assertion = "grep -v '^#!/bin/sh' $out/bin/test > /dev/null"; + assertion = "grep '^#!${stdenv.shell}' $out/bin/test > /dev/null"; }; }; ignores-nix-store = stdenv.mkDerivation { name = "ignores-nix-store"; + strictDeps = false; dontUnpack = true; installPhase = '' mkdir -p $out/bin - echo "#!$NIX_STORE/path/to/sh" > $out/bin/test + echo "#!$NIX_STORE/path/to/bash" > $out/bin/test echo "echo -n hello" >> $out/bin/test chmod +x $out/bin/test + dontPatchShebangs= ''; passthru = { - assertion = "grep \"^#!$NIX_STORE/path/to/sh\" $out/bin/test > /dev/null"; + assertion = "grep \"^#!$NIX_STORE/path/to/bash\" $out/bin/test > /dev/null"; }; }; + + split-string = stdenv.mkDerivation { + name = "split-string"; + strictDeps = false; + dontUnpack = true; + installPhase = '' + mkdir -p $out/bin + echo "#!/usr/bin/env -S bash --posix" > $out/bin/test + echo "echo -n hello" >> $out/bin/test + chmod +x $out/bin/test + dontPatchShebangs= + ''; + passthru = { + assertion = "grep -v '^#!${pkgs.coreutils}/bin/env -S ${stdenv.shell} --posix' $out/bin/test > /dev/null"; + }; + }; + }; -in runCommand "patch-shebangs-test" { - passthru = { inherit (tests) bad-shebang ignores-nix-store; }; - meta.platforms = lib.platforms.all; -} '' - validate() { - local name=$1 - local testout=$2 - local assertion=$3 +in +stdenv.mkDerivation { + name = "test-patch-shebangs"; + passthru = { inherit (tests) bad-shebang ignores-nix-store split-string; }; + buildCommand = '' + validate() { + local name=$1 + local testout=$2 + local assertion=$3 - echo -n "... $name: " >&2 + echo -n "... $name: " >&2 - local rc=0 - (out=$testout eval "$assertion") || rc=1 + local rc=0 + (out=$testout eval "$assertion") || rc=1 - if [ "$rc" -eq 0 ]; then - echo "yes" >&2 + 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 "no" >&2 + echo "succeeded" + touch $out 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 -'' + ''; +}