Merge changes I8e11ddbe,Idb8d9a00 into main

* changes:
  nix-shell: stop using dynamic format strings!!
  tests: move nix-shell related tests to subdir
This commit is contained in:
raito 2024-10-27 18:10:17 +00:00 committed by Gerrit Code Review
commit f7edee7c14
17 changed files with 107 additions and 62 deletions

View file

@ -490,42 +490,51 @@ static void main_nix_build(int argc, char * * argv)
environment variables and shell functions. Also don't environment variables and shell functions. Also don't
lose the current $PATH directories. */ lose the current $PATH directories. */
auto rcfile = (Path) tmpDir + "/rc"; auto rcfile = (Path) tmpDir + "/rc";
auto tz = getEnv("TZ");
std::string rc = fmt( std::string rc = fmt(
R"(_nix_shell_clean_tmpdir() { command rm -rf %1%; }; )"s + R"(_nix_shell_clean_tmpdir() { command rm -rf %1%; }; )"
(keepTmp ?
"trap _nix_shell_clean_tmpdir EXIT; "
"exitHooks+=(_nix_shell_clean_tmpdir); "
"failureHooks+=(_nix_shell_clean_tmpdir); ":
"_nix_shell_clean_tmpdir; ") +
(pure ? "" : "[ -n \"$PS1\" ] && [ -e ~/.bashrc ] && source ~/.bashrc;") +
"%2%" "%2%"
"%3%"
// always clear PATH. // always clear PATH.
// when nix-shell is run impure, we rehydrate it with the `p=$PATH` above // when nix-shell is run impure, we rehydrate it with the `p=$PATH` above
"unset PATH;" "unset PATH;"
"dontAddDisableDepTrack=1;\n" "dontAddDisableDepTrack=1;\n",
+ structuredAttrsRC + shellEscape(tmpDir),
(keepTmp
? "trap _nix_shell_clean_tmpdir EXIT; "
"exitHooks+=(_nix_shell_clean_tmpdir); "
"failureHooks+=(_nix_shell_clean_tmpdir); "
: "_nix_shell_clean_tmpdir; "),
(pure
? ""
: "[ -n \"$PS1\" ] && [ -e ~/.bashrc ] && source ~/.bashrc; p=$PATH; ")
);
rc += structuredAttrsRC;
rc += fmt(
"\n[ -e $stdenv/setup ] && source $stdenv/setup; " "\n[ -e $stdenv/setup ] && source $stdenv/setup; "
"%3%" "%1%"
"PATH=%4%:\"$PATH\"; " "PATH=%2%:\"$PATH\"; "
"SHELL=%5%; " "SHELL=%3%; "
"BASH=%5%; " "BASH=%3%; "
"set +e; " "set +e; "
R"s([ -n "$PS1" -a -z "$NIX_SHELL_PRESERVE_PROMPT" ] && )s" + R"s([ -n "$PS1" -a -z "$NIX_SHELL_PRESERVE_PROMPT" ] && )s"
(getuid() == 0 ? R"s(PS1='\n\[\033[1;31m\][nix-shell:\w]\$\[\033[0m\] '; )s" "%4%"
: R"s(PS1='\n\[\033[1;32m\][nix-shell:\w]\$\[\033[0m\] '; )s") +
"if [ \"$(type -t runHook)\" = function ]; then runHook shellHook; fi; " "if [ \"$(type -t runHook)\" = function ]; then runHook shellHook; fi; "
"unset NIX_ENFORCE_PURITY; " "unset NIX_ENFORCE_PURITY; "
"shopt -u nullglob; " "shopt -u nullglob; "
"unset TZ; %6%" "unset TZ; %5%"
"shopt -s execfail;" "shopt -s execfail;"
"%7%", "%6%",
shellEscape(tmpDir),
(pure ? "" : "p=$PATH; "),
(pure ? "" : "PATH=$PATH:$p; unset p; "), (pure ? "" : "PATH=$PATH:$p; unset p; "),
shellEscape(dirOf(*shell)), shellEscape(dirOf(*shell)),
shellEscape(*shell), shellEscape(*shell),
(getenv("TZ") ? (std::string("export TZ=") + shellEscape(getenv("TZ")) + "; ") : ""), (getuid() == 0 ? R"s(PS1='\n\[\033[1;31m\][nix-shell:\w]\$\[\033[0m\] '; )s"
envCommand); : R"s(PS1='\n\[\033[1;32m\][nix-shell:\w]\$\[\033[0m\] '; )s"),
(tz.has_value()
? (std::string("export TZ=") + shellEscape(*tz) + "; ")
: ""),
envCommand
);
vomit("Sourcing nix-shell with file %s and contents:\n%s", rcfile, rc); vomit("Sourcing nix-shell with file %s and contents:\n%s", rcfile, rc);
writeFile(rcfile, rc); writeFile(rcfile, rc);

View file

@ -3,5 +3,5 @@
source common.sh source common.sh
CONTENT_ADDRESSED=true CONTENT_ADDRESSED=true
cd .. cd ../nix-shell
source ./nix-shell.sh source ./basic.sh

View file

@ -24,7 +24,7 @@ EOF
# Create fake nixpkgs flake. # Create fake nixpkgs flake.
mkdir -p $TEST_HOME/nixpkgs mkdir -p $TEST_HOME/nixpkgs
cp ../config.nix ../shell.nix $TEST_HOME/nixpkgs cp ../config.nix ../nix-shell/shell.nix $TEST_HOME/nixpkgs
cat <<EOF >$TEST_HOME/nixpkgs/flake.nix cat <<EOF >$TEST_HOME/nixpkgs/flake.nix
{ {
outputs = {self}: { outputs = {self}: {

View file

@ -21,7 +21,7 @@ EOF
# Create fake nixpkgs flake. # Create fake nixpkgs flake.
mkdir -p $TEST_HOME/nixpkgs mkdir -p $TEST_HOME/nixpkgs
cp ../config.nix ../shell.nix $TEST_HOME/nixpkgs cp ../config.nix ../nix-shell/shell.nix $TEST_HOME/nixpkgs
cat <<EOF >$TEST_HOME/nixpkgs/flake.nix cat <<EOF >$TEST_HOME/nixpkgs/flake.nix
{ {
outputs = {self}: { outputs = {self}: {

View file

@ -25,6 +25,8 @@ config_nix_in = configure_file(
subdir('ca') subdir('ca')
# Just configures `dyn-drv/config.nix.in`. Same as above. # Just configures `dyn-drv/config.nix.in`. Same as above.
subdir('dyn-drv') subdir('dyn-drv')
# Just configures `nix-shell/config.nix.in`. Same as above.
subdir('nix-shell')
subdir('plugins') subdir('plugins')
subdir('test-libstoreconsumer') subdir('test-libstoreconsumer')
@ -111,7 +113,9 @@ functional_tests_scripts = [
'hash.sh', 'hash.sh',
'gc-non-blocking.sh', 'gc-non-blocking.sh',
'check.sh', 'check.sh',
'nix-shell.sh', 'nix-shell/basic.sh',
'nix-shell/structured-attrs.sh',
'nix-shell/regression-533.sh',
'check-refs.sh', 'check-refs.sh',
'build-remote-input-addressed.sh', 'build-remote-input-addressed.sh',
'secure-drv-outputs.sh', 'secure-drv-outputs.sh',

View file

@ -1,4 +1,4 @@
source common.sh source ../common.sh
clearStore clearStore

View file

@ -0,0 +1 @@
../config.nix.in

View file

@ -0,0 +1,6 @@
# test_confdata set from tests/functional/meson.build
configure_file(
input : 'config.nix.in',
output : 'config.nix',
configuration : test_confdata,
)

View file

@ -0,0 +1,18 @@
source ../common.sh
clearStore
evil=$(cat <<-'EOF'
builtins.derivation {
name = "evil-kbity";
system = "x86_64-darwin";
builder = "/bin/sh";
args = [ "-c" "> $out" ];
__structuredAttrs = true;
env.oops = "lol %s";
}
EOF
)
# This should not crash
nix-shell --expr "$evil" --run 'echo yay' | grepQuiet yay

View file

@ -0,0 +1,28 @@
source ../common.sh
# 27ce722638 required some incompatible changes to the nix file, so skip this
# tests for the older versions
requireDaemonNewerThan "2.4pre20210712"
clearStore
export NIX_BUILD_SHELL=$SHELL
env NIX_PATH=nixpkgs=shell.nix nix-shell structured-attrs-shell.nix \
--run 'test "3" = "$(jq ".my.list|length" < $NIX_ATTRS_JSON_FILE)"'
nix develop -f structured-attrs-shell.nix -c bash -c 'test "3" = "$(jq ".my.list|length" < $NIX_ATTRS_JSON_FILE)"'
# `nix develop` is a slightly special way of dealing with environment vars, it parses
# these from a shell-file exported from a derivation. This is to test especially `outputs`
# (which is an associative array in thsi case) being fine.
nix develop -f structured-attrs-shell.nix -c bash -c 'test -n "$out"'
nix print-dev-env -f structured-attrs-shell.nix | grepQuiet 'NIX_ATTRS_JSON_FILE='
nix print-dev-env -f structured-attrs-shell.nix | grepQuiet 'NIX_ATTRS_SH_FILE='
nix print-dev-env -f shell.nix shellDrv | grepQuietInverse 'NIX_ATTRS_SH_FILE'
jsonOut="$(nix print-dev-env -f structured-attrs-shell.nix --json)"
test "$(<<<"$jsonOut" jq '.structuredAttrs|keys|.[]' -r)" = "$(printf ".attrs.json\n.attrs.sh")"
test "$(<<<"$jsonOut" jq '.variables.out.value' -r)" = "$(<<<"$jsonOut" jq '.structuredAttrs.".attrs.json"' -r | jq -r '.outputs.out')"

View file

@ -12,24 +12,3 @@ nix-build structured-attrs.nix -A all -o $TEST_ROOT/result
[[ $(cat $TEST_ROOT/result/foo) = bar ]] [[ $(cat $TEST_ROOT/result/foo) = bar ]]
[[ $(cat $TEST_ROOT/result-dev/foo) = foo ]] [[ $(cat $TEST_ROOT/result-dev/foo) = foo ]]
export NIX_BUILD_SHELL=$SHELL
env NIX_PATH=nixpkgs=shell.nix nix-shell structured-attrs-shell.nix \
--run 'test "3" = "$(jq ".my.list|length" < $NIX_ATTRS_JSON_FILE)"'
nix develop -f structured-attrs-shell.nix -c bash -c 'test "3" = "$(jq ".my.list|length" < $NIX_ATTRS_JSON_FILE)"'
# `nix develop` is a slightly special way of dealing with environment vars, it parses
# these from a shell-file exported from a derivation. This is to test especially `outputs`
# (which is an associative array in thsi case) being fine.
nix develop -f structured-attrs-shell.nix -c bash -c 'test -n "$out"'
nix print-dev-env -f structured-attrs-shell.nix | grepQuiet 'NIX_ATTRS_JSON_FILE='
nix print-dev-env -f structured-attrs-shell.nix | grepQuiet 'NIX_ATTRS_SH_FILE='
nix print-dev-env -f shell.nix shellDrv | grepQuietInverse 'NIX_ATTRS_SH_FILE'
jsonOut="$(nix print-dev-env -f structured-attrs-shell.nix --json)"
test "$(<<<"$jsonOut" jq '.structuredAttrs|keys|.[]' -r)" = "$(printf ".attrs.json\n.attrs.sh")"
test "$(<<<"$jsonOut" jq '.variables.out.value' -r)" = "$(<<<"$jsonOut" jq '.structuredAttrs.".attrs.json"' -r | jq -r '.outputs.out')"