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 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/patch-shebangs/default.nix b/pkgs/test/patch-shebangs/default.nix deleted file mode 100644 index 5c49787eee3b..000000000000 --- a/pkgs/test/patch-shebangs/default.nix +++ /dev/null @@ -1,70 +0,0 @@ -{ lib, stdenv, runCommand }: - -let - 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 (tests) bad-shebang ignores-nix-store; }; - meta.platforms = lib.platforms.all; -} '' - 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 -'' 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/stdenv/patch-shebangs.nix b/pkgs/test/stdenv/patch-shebangs.nix new file mode 100644 index 000000000000..fb52f38ecc91 --- /dev/null +++ b/pkgs/test/stdenv/patch-shebangs.nix @@ -0,0 +1,98 @@ +{ 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"; + strictDeps = false; + dontUnpack = true; + installPhase = '' + mkdir -p $out/bin + echo "#!/bin/bash" > $out/bin/test + echo "echo -n hello" >> $out/bin/test + chmod +x $out/bin/test + dontPatchShebangs= + ''; + passthru = { + 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/bash" > $out/bin/test + echo "echo -n hello" >> $out/bin/test + chmod +x $out/bin/test + dontPatchShebangs= + ''; + passthru = { + 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 +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 + + 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 + ''; +} 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 */ ]; };