2024-05-28 19:43:04 +03:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2020-04-30 00:37:21 +03:00
|
|
|
source common.sh
|
|
|
|
|
2024-06-16 13:51:46 +03:00
|
|
|
TODO_NixOS
|
|
|
|
|
2020-04-30 00:37:21 +03:00
|
|
|
clearStore
|
|
|
|
clearCache
|
|
|
|
|
2024-05-30 19:22:11 +03: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-30 00:37:21 +03: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 16:17:01 +03: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-11 00:46:19 +03:00
|
|
|
# Test output paths that are a symlink.
|
2024-04-11 10:00:47 +03:00
|
|
|
nix shell -f shell-hello.nix hello-symlink -c hello | grep 'Hello World'
|
2023-12-20 20:25:22 +02:00
|
|
|
|
2024-04-11 10:04:26 +03: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 12:39:33 +03: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 06:33:28 +02:00
|
|
|
env > "$TEST_ROOT/expected-env"
|
|
|
|
nix shell -f shell-hello.nix hello -c env > "$TEST_ROOT/actual-env"
|
2024-06-10 12:39:33 +03:00
|
|
|
# Remove/reset variables we expect to be different.
|
|
|
|
# - PATH is modified by nix shell
|
2024-09-18 21:32:49 +03:00
|
|
|
# - we unset TMPDIR on macOS if it contains /var/folders
|
2024-06-10 12:39:33 +03: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 21:32:49 +03:00
|
|
|
-e '/^TMPDIR=\/var\/folders\/.*/d' \
|
2024-06-10 12:39:33 +03:00
|
|
|
-e '/^__CF_USER_TEXT_ENCODING=.*$/d' \
|
2024-10-30 06:33:28 +02: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 12:39:33 +03:00
|
|
|
|
2023-12-20 20:25:22 +02: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 22:00:20 +02:00
|
|
|
requireSandboxSupport
|
2020-04-30 00:37:21 +03:00
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
chmod -R u+w "$TEST_ROOT/store0" || true
|
|
|
|
rm -rf "$TEST_ROOT/store0"
|
2020-04-30 00:37:21 +03: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 06:33:28 +02:00
|
|
|
--store "$TEST_ROOT/store0" -f shell-hello.nix hello -c hello | grep 'Hello World'
|
2020-04-30 00:37:21 +03:00
|
|
|
|
2024-10-30 06:33:28 +02: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-30 00:37:21 +03:00
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
[[ "$path/bin/hello" = "$path2" ]]
|
2020-04-30 00:37:21 +03:00
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
[[ -e $TEST_ROOT/store0/nix/store/$(basename "$path")/bin/hello ]]
|