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:
commit
f7edee7c14
|
@ -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 ?
|
"%2%"
|
||||||
"trap _nix_shell_clean_tmpdir EXIT; "
|
"%3%"
|
||||||
"exitHooks+=(_nix_shell_clean_tmpdir); "
|
// always clear PATH.
|
||||||
"failureHooks+=(_nix_shell_clean_tmpdir); ":
|
// when nix-shell is run impure, we rehydrate it with the `p=$PATH` above
|
||||||
"_nix_shell_clean_tmpdir; ") +
|
"unset PATH;"
|
||||||
(pure ? "" : "[ -n \"$PS1\" ] && [ -e ~/.bashrc ] && source ~/.bashrc;") +
|
"dontAddDisableDepTrack=1;\n",
|
||||||
"%2%"
|
shellEscape(tmpDir),
|
||||||
// always clear PATH.
|
(keepTmp
|
||||||
// when nix-shell is run impure, we rehydrate it with the `p=$PATH` above
|
? "trap _nix_shell_clean_tmpdir EXIT; "
|
||||||
"unset PATH;"
|
"exitHooks+=(_nix_shell_clean_tmpdir); "
|
||||||
"dontAddDisableDepTrack=1;\n"
|
"failureHooks+=(_nix_shell_clean_tmpdir); "
|
||||||
+ structuredAttrsRC +
|
: "_nix_shell_clean_tmpdir; "),
|
||||||
"\n[ -e $stdenv/setup ] && source $stdenv/setup; "
|
(pure
|
||||||
"%3%"
|
? ""
|
||||||
"PATH=%4%:\"$PATH\"; "
|
: "[ -n \"$PS1\" ] && [ -e ~/.bashrc ] && source ~/.bashrc; p=$PATH; ")
|
||||||
"SHELL=%5%; "
|
);
|
||||||
"BASH=%5%; "
|
rc += structuredAttrsRC;
|
||||||
"set +e; "
|
rc += fmt(
|
||||||
R"s([ -n "$PS1" -a -z "$NIX_SHELL_PRESERVE_PROMPT" ] && )s" +
|
"\n[ -e $stdenv/setup ] && source $stdenv/setup; "
|
||||||
(getuid() == 0 ? R"s(PS1='\n\[\033[1;31m\][nix-shell:\w]\$\[\033[0m\] '; )s"
|
"%1%"
|
||||||
: R"s(PS1='\n\[\033[1;32m\][nix-shell:\w]\$\[\033[0m\] '; )s") +
|
"PATH=%2%:\"$PATH\"; "
|
||||||
"if [ \"$(type -t runHook)\" = function ]; then runHook shellHook; fi; "
|
"SHELL=%3%; "
|
||||||
"unset NIX_ENFORCE_PURITY; "
|
"BASH=%3%; "
|
||||||
"shopt -u nullglob; "
|
"set +e; "
|
||||||
"unset TZ; %6%"
|
R"s([ -n "$PS1" -a -z "$NIX_SHELL_PRESERVE_PROMPT" ] && )s"
|
||||||
"shopt -s execfail;"
|
"%4%"
|
||||||
"%7%",
|
"if [ \"$(type -t runHook)\" = function ]; then runHook shellHook; fi; "
|
||||||
shellEscape(tmpDir),
|
"unset NIX_ENFORCE_PURITY; "
|
||||||
(pure ? "" : "p=$PATH; "),
|
"shopt -u nullglob; "
|
||||||
(pure ? "" : "PATH=$PATH:$p; unset p; "),
|
"unset TZ; %5%"
|
||||||
shellEscape(dirOf(*shell)),
|
"shopt -s execfail;"
|
||||||
shellEscape(*shell),
|
"%6%",
|
||||||
(getenv("TZ") ? (std::string("export TZ=") + shellEscape(getenv("TZ")) + "; ") : ""),
|
(pure ? "" : "PATH=$PATH:$p; unset p; "),
|
||||||
envCommand);
|
shellEscape(dirOf(*shell)),
|
||||||
|
shellEscape(*shell),
|
||||||
|
(getuid() == 0 ? R"s(PS1='\n\[\033[1;31m\][nix-shell:\w]\$\[\033[0m\] '; )s"
|
||||||
|
: 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);
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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}: {
|
||||||
|
|
|
@ -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}: {
|
||||||
|
|
|
@ -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',
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
source common.sh
|
source ../common.sh
|
||||||
|
|
||||||
clearStore
|
clearStore
|
||||||
|
|
1
tests/functional/nix-shell/config.nix.in
Symbolic link
1
tests/functional/nix-shell/config.nix.in
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
../config.nix.in
|
6
tests/functional/nix-shell/meson.build
Normal file
6
tests/functional/nix-shell/meson.build
Normal 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,
|
||||||
|
)
|
18
tests/functional/nix-shell/regression-533.sh
Normal file
18
tests/functional/nix-shell/regression-533.sh
Normal 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
|
28
tests/functional/nix-shell/structured-attrs.sh
Normal file
28
tests/functional/nix-shell/structured-attrs.sh
Normal 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')"
|
|
@ -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')"
|
|
||||||
|
|
Loading…
Reference in a new issue