2024-05-28 16:43:04 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2020-04-29 21:37:21 +00:00
|
|
|
source common.sh
|
|
|
|
|
2024-06-16 10:51:46 +00:00
|
|
|
TODO_NixOS
|
|
|
|
|
2020-04-29 21:37:21 +00:00
|
|
|
clearStore
|
|
|
|
clearCache
|
|
|
|
|
2024-05-30 16:22:11 +00:00
|
|
|
# nix shell is an alias for nix env shell. We'll use the shorter form in the rest of the test.
|
|
|
|
nix env shell -f shell-hello.nix hello -c hello | grep 'Hello World'
|
|
|
|
|
2020-04-29 21:37:21 +00:00
|
|
|
nix shell -f shell-hello.nix hello -c hello | grep 'Hello World'
|
|
|
|
nix shell -f shell-hello.nix hello -c hello NixOS | grep 'Hello NixOS'
|
|
|
|
|
2022-04-22 13:17:01 +00:00
|
|
|
# Test output selection.
|
|
|
|
nix shell -f shell-hello.nix hello^dev -c hello2 | grep 'Hello2'
|
|
|
|
nix shell -f shell-hello.nix 'hello^*' -c hello2 | grep 'Hello2'
|
|
|
|
|
2024-04-10 21:46:19 +00:00
|
|
|
# Test output paths that are a symlink.
|
2024-04-11 07:00:47 +00:00
|
|
|
nix shell -f shell-hello.nix hello-symlink -c hello | grep 'Hello World'
|
2023-12-20 18:25:22 +00:00
|
|
|
|
2024-04-11 07:04:26 +00:00
|
|
|
# Test that symlinks outside of the store don't work.
|
|
|
|
expect 1 nix shell -f shell-hello.nix forbidden-symlink -c hello 2>&1 | grepQuiet "is not in the Nix store"
|
|
|
|
|
2024-06-10 09:39:33 +00:00
|
|
|
# Test that we're not setting any more environment variables than necessary.
|
|
|
|
# For instance, we might set an environment variable temporarily to affect some
|
|
|
|
# initialization or whatnot, but this must not leak into the environment of the
|
|
|
|
# command being run.
|
2024-10-30 04:33:28 +00:00
|
|
|
env > "$TEST_ROOT/expected-env"
|
|
|
|
nix shell -f shell-hello.nix hello -c env > "$TEST_ROOT/actual-env"
|
2024-06-10 09:39:33 +00:00
|
|
|
# Remove/reset variables we expect to be different.
|
|
|
|
# - PATH is modified by nix shell
|
2024-09-18 18:32:49 +00:00
|
|
|
# - we unset TMPDIR on macOS if it contains /var/folders
|
2024-06-10 09:39:33 +00:00
|
|
|
# - _ is set by bash and is expectedf to differ because it contains the original command
|
|
|
|
# - __CF_USER_TEXT_ENCODING is set by macOS and is beyond our control
|
|
|
|
sed -i \
|
|
|
|
-e 's/PATH=.*/PATH=.../' \
|
|
|
|
-e 's/_=.*/_=.../' \
|
2024-09-18 18:32:49 +00:00
|
|
|
-e '/^TMPDIR=\/var\/folders\/.*/d' \
|
2024-06-10 09:39:33 +00:00
|
|
|
-e '/^__CF_USER_TEXT_ENCODING=.*$/d' \
|
2024-10-30 04:33:28 +00:00
|
|
|
"$TEST_ROOT/expected-env" "$TEST_ROOT/actual-env"
|
|
|
|
sort "$TEST_ROOT/expected-env" > "$TEST_ROOT/expected-env.sorted"
|
|
|
|
sort "$TEST_ROOT/actual-env" > "$TEST_ROOT/actual-env.sorted"
|
|
|
|
diff "$TEST_ROOT/expected-env.sorted" "$TEST_ROOT/actual-env.sorted"
|
2024-06-10 09:39:33 +00:00
|
|
|
|
2023-12-20 18:25:22 +00:00
|
|
|
if isDaemonNewer "2.20.0pre20231220"; then
|
|
|
|
# Test that command line attribute ordering is reflected in the PATH
|
|
|
|
# https://github.com/NixOS/nix/issues/7905
|
|
|
|
nix shell -f shell-hello.nix hello salve-mundi -c hello | grep 'Hello World'
|
|
|
|
nix shell -f shell-hello.nix salve-mundi hello -c hello | grep 'Salve Mundi'
|
|
|
|
fi
|
|
|
|
|
2023-03-16 20:00:20 +00:00
|
|
|
requireSandboxSupport
|
2020-04-29 21:37:21 +00:00
|
|
|
|
2024-10-30 04:33:28 +00:00
|
|
|
chmod -R u+w "$TEST_ROOT/store0" || true
|
|
|
|
rm -rf "$TEST_ROOT/store0"
|
2020-04-29 21:37:21 +00:00
|
|
|
|
|
|
|
clearStore
|
|
|
|
|
|
|
|
path=$(nix eval --raw -f shell-hello.nix hello)
|
|
|
|
|
|
|
|
# Note: we need the sandbox paths to ensure that the shell is
|
|
|
|
# visible in the sandbox.
|
|
|
|
nix shell --sandbox-build-dir /build-tmp \
|
|
|
|
--sandbox-paths '/nix? /bin? /lib? /lib64? /usr?' \
|
2024-10-30 04:33:28 +00:00
|
|
|
--store "$TEST_ROOT/store0" -f shell-hello.nix hello -c hello | grep 'Hello World'
|
2020-04-29 21:37:21 +00:00
|
|
|
|
2024-10-30 04:33:28 +00:00
|
|
|
path2=$(nix shell --sandbox-paths '/nix? /bin? /lib? /lib64? /usr?' --store "$TEST_ROOT/store0" -f shell-hello.nix hello -c "$SHELL" -c 'type -p hello')
|
2020-04-29 21:37:21 +00:00
|
|
|
|
2024-10-30 04:33:28 +00:00
|
|
|
[[ "$path/bin/hello" = "$path2" ]]
|
2020-04-29 21:37:21 +00:00
|
|
|
|
2024-10-30 04:33:28 +00:00
|
|
|
[[ -e $TEST_ROOT/store0/nix/store/$(basename "$path")/bin/hello ]]
|