This commit is contained in:
Taeer Bar-Yam 2025-04-13 13:31:47 +05:30 committed by GitHub
commit a40b7d1c4b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 65 additions and 9 deletions

View File

@ -473,8 +473,11 @@ static void main_nix_build(int argc, char * * argv)
shellDrv = bashDrv;
} catch (Error & e) {
logError(e.info());
notice("will use bash from your environment");
// in a shebang, it doesn't matter which bash we use since we immediately execute the interpreter
if (!inShebang) {
logErrorInfo(lvlInfo, e.info());
printInfo("Cannot access '(import <nixpkgs> {}).bashInteractive'; falling back to bash from your environment.");
}
shell = "bash";
}
}
@ -612,9 +615,8 @@ static void main_nix_build(int argc, char * * argv)
+ structuredAttrsRC +
"\n[ -e $stdenv/setup ] && source $stdenv/setup; "
"%3%"
"PATH=%4%:\"$PATH\"; "
"SHELL=%5%; "
"BASH=%5%; "
"SHELL=%4%; "
"BASH=%4%; "
"set +e; "
R"s([ -n "$PS1" -a -z "$NIX_SHELL_PRESERVE_PROMPT" ] && )s" +
(isRootUser()
@ -623,13 +625,12 @@ static void main_nix_build(int argc, char * * argv)
"if [ \"$(type -t runHook)\" = function ]; then runHook shellHook; fi; "
"unset NIX_ENFORCE_PURITY; "
"shopt -u nullglob; "
"unset TZ; %6%"
"unset TZ; %5%"
"shopt -s execfail;"
"%7%",
"%6%",
shellEscape(tmpDir.path().string()),
(pure ? "" : "p=$PATH; "),
(pure ? "" : "PATH=$PATH:$p; unset p; "),
shellEscape(dirOf(*shell)),
shellEscape(*shell),
(getenv("TZ") ? (std::string("export TZ=") + shellEscape(getenv("TZ")) + "; ") : ""),
envCommand);

View File

@ -10,7 +10,7 @@ in
rec {
shell = "@bash@";
path = "@coreutils@";
path = "@coreutils@:${dirOf shell}";
system = "@system@";

View File

@ -0,0 +1 @@
echo "1"

View File

@ -0,0 +1,11 @@
args:
let
pkgs = import ../shell.nix args;
in
{
bashInteractive = pkgs.runCommand "bash" { } ''
mkdir -p $out/bin
echo "echo 2" > $out/bin/bash
chmod +x $out/bin/bash
'';
}

View File

@ -196,6 +196,36 @@ $TEST_ROOT/issue-11892/shebangscript \
| tee /dev/stderr \
| grepQuiet "ok baz11892"
(
# Test the prioritization of build shells
export NIX_BUILD_SHELL="$PWD/dummy-build-shells/1-shell"
export NIX_PATH="nixpkgs=$PWD/dummy-build-shells/2-nixpkgs.nix"
# 1. $NIX_BUILD_SHELL
# "echo 3" will not run because the bash used is provied by 1-shell
output=$(nix-shell --pure "$shellDotNix" -A shellDrv --run "echo 3")
[ "$output" = "1" ]
# 2. (import <nixpkgs> {}).bashInteractive
# "echo 3" will not be run because the bash used is provided by 2-nixpkgs
unset NIX_BUILD_SHELL
output=$(nix-shell --pure "$shellDotNix" -A shellDrv --run "echo 3")
[ "$output" = "2" ]
# 3. bash from $PATH
# NOTE: we should really test this by overwriting $PATH and passing --keep
# PATH to nix-shell, but the mkDerivation in config.nix will overwrite
# whatever PATH we give here so we'll use what's defined in config.nix
# (actual bash) as our third example with --run 'echo 3'
unset NIX_PATH
output=$(nix-shell --pure "$shellDotNix" -A shellDrv --run 'echo 3')
[ "$output" = "3" ]
# 4. once we are in the nix-shell, `bash` should be whatever is provided by
# the derivation
output=$(nix-shell --pure "$shellDotNix" -A shellDrvFakeBash4 --run 'bash -e "echo 5"')
[ "$output" = "4" ]
)
#####################
# Flake equivalents #

View File

@ -84,6 +84,13 @@ let
'';
};
shellDrvFakeBash4 = mkDerivation {
name = "shellDrvFakeBash4";
builder = "/does/not/exist";
inherit stdenv;
PATH = "${fakeBash4}/bin:${path}";
};
# Used by nix-shell -p
runCommand =
name: args: buildCommand:
@ -120,6 +127,12 @@ let
chmod a+rx $out/bin/ruby
'';
fakeBash4 = runCommand "fakeBash4" { } ''
mkdir -p $out/bin
echo 'echo 4' > $out/bin/bash
chmod a+rx $out/bin/bash
'';
inherit (cfg) shell;
callPackage = f: args: f (pkgs // args);