mirror of
https://github.com/NixOS/nix.git
synced 2025-04-16 14:19:21 +00:00
Merge 20127a37f5
into 0e1323c041
This commit is contained in:
commit
a40b7d1c4b
@ -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);
|
||||
|
@ -10,7 +10,7 @@ in
|
||||
rec {
|
||||
shell = "@bash@";
|
||||
|
||||
path = "@coreutils@";
|
||||
path = "@coreutils@:${dirOf shell}";
|
||||
|
||||
system = "@system@";
|
||||
|
||||
|
1
tests/functional/dummy-build-shells/1-shell
Executable file
1
tests/functional/dummy-build-shells/1-shell
Executable file
@ -0,0 +1 @@
|
||||
echo "1"
|
11
tests/functional/dummy-build-shells/2-nixpkgs.nix
Normal file
11
tests/functional/dummy-build-shells/2-nixpkgs.nix
Normal 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
|
||||
'';
|
||||
}
|
@ -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 #
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user