2024-05-28 16:43:04 +00:00
#!/usr/bin/env bash
2017-01-01 16:45:51 +00:00
source common.sh
2024-06-16 15:56:50 +00:00
clearStoreIfPossible
2017-01-01 16:45:51 +00:00
2021-05-20 11:55:00 +00:00
if [ [ -n ${ CONTENT_ADDRESSED :- } ] ] ; then
2021-11-26 08:56:48 +00:00
shellDotNix = " $PWD /ca-shell.nix "
2021-06-11 11:31:19 +00:00
else
2021-11-26 08:56:48 +00:00
shellDotNix = " $PWD /shell.nix "
2021-05-20 11:55:00 +00:00
fi
2021-11-26 08:56:48 +00:00
export NIX_PATH = nixpkgs = " $shellDotNix "
2017-01-01 16:45:51 +00:00
# Test nix-shell -A
export IMPURE_VAR = foo
2018-08-02 01:28:49 +00:00
export SELECTED_IMPURE_VAR = baz
2021-11-26 08:56:48 +00:00
output = $( nix-shell --pure " $shellDotNix " -A shellDrv --run \
2019-10-27 08:34:33 +00:00
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX - $TEST_inNixShell"' )
2017-01-01 16:45:51 +00:00
2019-10-27 08:34:33 +00:00
[ " $output " = " - foo - bar - true" ]
2017-01-01 16:45:51 +00:00
2024-07-06 20:58:15 +00:00
output = $( nix-shell --pure " $shellDotNix " -A shellDrv --option nix-shell-always-looks-for-shell-nix false --run \
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX - $TEST_inNixShell"' )
[ " $output " = " - foo - bar - true" ]
2018-08-02 01:28:49 +00:00
# Test --keep
2021-11-26 08:56:48 +00:00
output = $( nix-shell --pure --keep SELECTED_IMPURE_VAR " $shellDotNix " -A shellDrv --run \
2018-08-02 01:28:49 +00:00
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX - $SELECTED_IMPURE_VAR"' )
[ " $output " = " - foo - bar - baz" ]
2024-10-18 09:03:33 +00:00
# test NIX_BUILD_TOP
testTmpDir = $( pwd ) /nix-shell
mkdir -p " $testTmpDir "
output = $( TMPDIR = " $testTmpDir " nix-shell --pure " $shellDotNix " -A shellDrv --run 'echo $NIX_BUILD_TOP' )
[ [ " $output " = ~ ${ testTmpDir } .* ] ] || {
echo " expected $output =~ ${ testTmpDir } .* " >& 2
exit 1
}
2017-11-24 17:07:29 +00:00
# Test nix-shell on a .drv
2021-11-26 08:56:48 +00:00
[ [ $( nix-shell --pure $( nix-instantiate " $shellDotNix " -A shellDrv) --run \
2019-10-27 08:34:33 +00:00
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX - $TEST_inNixShell"' ) = " - foo - bar - false" ] ]
2017-11-24 17:07:29 +00:00
2021-11-26 08:56:48 +00:00
[ [ $( nix-shell --pure $( nix-instantiate " $shellDotNix " -A shellDrv) --run \
2019-10-27 08:34:33 +00:00
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX - $TEST_inNixShell"' ) = " - foo - bar - false" ] ]
2017-11-24 17:07:29 +00:00
2018-03-16 22:15:24 +00:00
# Test nix-shell on a .drv symlink
# Legacy: absolute path and .drv extension required
2021-11-26 08:56:48 +00:00
nix-instantiate " $shellDotNix " -A shellDrv --add-root $TEST_ROOT /shell.drv
2019-05-07 19:15:45 +00:00
[ [ $( nix-shell --pure $TEST_ROOT /shell.drv --run \
2018-03-16 22:15:24 +00:00
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX"' ) = " - foo - bar" ] ]
# New behaviour: just needs to resolve to a derivation in the store
2021-11-26 08:56:48 +00:00
nix-instantiate " $shellDotNix " -A shellDrv --add-root $TEST_ROOT /shell
2019-05-07 19:15:45 +00:00
[ [ $( nix-shell --pure $TEST_ROOT /shell --run \
2018-03-16 22:15:24 +00:00
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX"' ) = " - foo - bar" ] ]
2017-01-01 16:45:51 +00:00
# Test nix-shell -p
2021-11-26 08:56:48 +00:00
output = $( NIX_PATH = nixpkgs = " $shellDotNix " nix-shell --pure -p foo bar --run 'echo "$(foo) $(bar)"' )
2017-01-01 16:45:51 +00:00
[ " $output " = "foo bar" ]
2021-07-31 14:55:53 +00:00
# Test nix-shell -p --arg x y
2021-11-26 08:56:48 +00:00
output = $( NIX_PATH = nixpkgs = " $shellDotNix " nix-shell --pure -p foo --argstr fooContents baz --run 'echo "$(foo)"' )
2021-07-31 14:55:53 +00:00
[ " $output " = "baz" ]
2017-01-01 16:45:51 +00:00
# Test nix-shell shebang mode
2021-05-20 11:55:00 +00:00
sed -e " s|@ENV_PROG@| $( type -P env) | " shell.shebang.sh > $TEST_ROOT /shell.shebang.sh
2017-01-01 16:45:51 +00:00
chmod a+rx $TEST_ROOT /shell.shebang.sh
2017-01-03 10:40:51 +00:00
output = $( $TEST_ROOT /shell.shebang.sh abc def)
[ " $output " = "foo bar abc def" ]
2018-04-08 04:45:28 +00:00
2023-11-26 00:06:45 +00:00
# Test nix-shell shebang mode with an alternate working directory
sed -e " s|@ENV_PROG@| $( type -P env) | " shell.shebang.expr > $TEST_ROOT /shell.shebang.expr
chmod a+rx $TEST_ROOT /shell.shebang.expr
# Should fail due to expressions using relative path
! $TEST_ROOT /shell.shebang.expr bar
2024-11-01 13:56:50 +00:00
cp shell.nix " ${ config_nix } " $TEST_ROOT
2023-11-26 00:06:45 +00:00
# Should succeed
2024-07-06 22:22:21 +00:00
echo " cwd: $PWD "
2023-11-26 00:06:45 +00:00
output = $( $TEST_ROOT /shell.shebang.expr bar)
2024-07-06 22:22:21 +00:00
[ " $output " = foo ]
2023-11-26 00:06:45 +00:00
2024-07-06 22:55:33 +00:00
# Test nix-shell shebang mode with an alternate working directory
sed -e " s|@ENV_PROG@| $( type -P env) | " shell.shebang.legacy.expr > $TEST_ROOT /shell.shebang.legacy.expr
chmod a+rx $TEST_ROOT /shell.shebang.legacy.expr
# Should fail due to expressions using relative path
mkdir -p " $TEST_ROOT /somewhere-unrelated "
output = " $( cd " $TEST_ROOT /somewhere-unrelated " ; $TEST_ROOT /shell.shebang.legacy.expr bar; ) "
[ [ $( realpath " $output " ) = $( realpath " $TEST_ROOT /somewhere-unrelated " ) ] ]
2020-11-24 22:12:32 +00:00
# Test nix-shell shebang mode again with metacharacters in the filename.
# First word of filename is chosen to not match any file in the test root.
2021-05-20 11:55:00 +00:00
sed -e " s|@ENV_PROG@| $( type -P env) | " shell.shebang.sh > $TEST_ROOT /spaced\ \\ \' \" shell.shebang.sh
2020-11-24 22:12:32 +00:00
chmod a+rx $TEST_ROOT /spaced\ \\ \' \" shell.shebang.sh
output = $( $TEST_ROOT /spaced\ \\ \' \" shell.shebang.sh abc def)
[ " $output " = "foo bar abc def" ]
2018-04-08 04:45:28 +00:00
# Test nix-shell shebang mode for ruby
# This uses a fake interpreter that returns the arguments passed
# This, in turn, verifies the `rc` script is valid and the `load()` script (given using `-e`) is as expected.
2021-05-20 11:55:00 +00:00
sed -e " s|@SHELL_PROG@| $( type -P nix-shell) | " shell.shebang.rb > $TEST_ROOT /shell.shebang.rb
2018-04-08 04:45:28 +00:00
chmod a+rx $TEST_ROOT /shell.shebang.rb
output = $( $TEST_ROOT /shell.shebang.rb abc ruby)
2020-11-24 22:12:32 +00:00
[ " $output " = '-e load(ARGV.shift) -- ' " $TEST_ROOT " '/shell.shebang.rb abc ruby' ]
# Test nix-shell shebang mode for ruby again with metacharacters in the filename.
# Note: fake interpreter only space-separates args without adding escapes to its output.
2021-05-20 11:55:00 +00:00
sed -e " s|@SHELL_PROG@| $( type -P nix-shell) | " shell.shebang.rb > $TEST_ROOT /spaced\ \\ \' \" shell.shebang.rb
2020-11-24 22:12:32 +00:00
chmod a+rx $TEST_ROOT /spaced\ \\ \' \" shell.shebang.rb
output = $( $TEST_ROOT /spaced\ \\ \' \" shell.shebang.rb abc ruby)
[ " $output " = '-e load(ARGV.shift) -- ' " $TEST_ROOT " '/spaced \' \' '"shell.shebang.rb abc ruby' ]
2020-07-02 16:32:45 +00:00
2023-06-17 12:54:34 +00:00
# Test nix-shell shebang quoting
sed -e " s|@ENV_PROG@| $( type -P env) | " shell.shebang.nix > $TEST_ROOT /shell.shebang.nix
chmod a+rx $TEST_ROOT /shell.shebang.nix
$TEST_ROOT /shell.shebang.nix
2024-07-06 19:31:24 +00:00
mkdir $TEST_ROOT /lookup-test $TEST_ROOT /empty
2024-07-06 19:31:24 +00:00
echo " import $shellDotNix " > $TEST_ROOT /lookup-test/shell.nix
2024-11-01 13:56:50 +00:00
cp " ${ config_nix } " $TEST_ROOT /lookup-test/
2024-07-06 19:31:24 +00:00
echo 'abort "do not load default.nix!"' > $TEST_ROOT /lookup-test/default.nix
nix-shell $TEST_ROOT /lookup-test -A shellDrv --run 'echo "it works"' | grepQuiet "it works"
# https://github.com/NixOS/nix/issues/4529
nix-shell -I " testRoot= $TEST_ROOT " '<testRoot/lookup-test>' -A shellDrv --run 'echo "it works"' | grepQuiet "it works"
2024-07-06 20:58:15 +00:00
expectStderr 1 nix-shell $TEST_ROOT /lookup-test -A shellDrv --run 'echo "it works"' --option nix-shell-always-looks-for-shell-nix false \
| grepQuiet -F "do not load default.nix!" # we did, because we chose to enable legacy behavior
expectStderr 1 nix-shell $TEST_ROOT /lookup-test -A shellDrv --run 'echo "it works"' --option nix-shell-always-looks-for-shell-nix false \
| grepQuiet "Skipping .*lookup-test/shell\.nix.*, because the setting .*nix-shell-always-looks-for-shell-nix.* is disabled. This is a deprecated behavior\. Consider enabling .*nix-shell-always-looks-for-shell-nix.*"
2024-07-06 19:31:24 +00:00
(
cd $TEST_ROOT /empty;
expectStderr 1 nix-shell | \
grepQuiet "error.*no argument specified and no .*shell\.nix.* or .*default\.nix.* file found in the working directory"
)
expectStderr 1 nix-shell -I " testRoot= $TEST_ROOT " '<testRoot/empty>' |
grepQuiet "error.*neither .*shell\.nix.* nor .*default\.nix.* found in .*/empty"
2024-07-06 19:31:24 +00:00
cat >$TEST_ROOT /lookup-test/shebangscript <<EOF
#!$(type -P env) nix-shell
2024-07-06 19:31:24 +00:00
#!nix-shell -A shellDrv -i bash
2024-07-06 19:31:24 +00:00
[ [ \$ VAR_FROM_NIX = = bar ] ]
2024-07-06 19:31:24 +00:00
echo "script works"
EOF
chmod +x $TEST_ROOT /lookup-test/shebangscript
$TEST_ROOT /lookup-test/shebangscript | grepQuiet "script works"
# https://github.com/NixOS/nix/issues/5431
mkdir $TEST_ROOT /marco{ ,/polo}
echo 'abort "marco/shell.nix must not be used, but its mere existence used to cause #5431"' > $TEST_ROOT /marco/shell.nix
cat >$TEST_ROOT /marco/polo/default.nix <<EOF
2024-07-06 19:31:24 +00:00
#!$(type -P env) nix-shell
2024-07-06 19:31:24 +00:00
( import $TEST_ROOT /lookup-test/shell.nix { } ) .polo
EOF
chmod a+x $TEST_ROOT /marco/polo/default.nix
( cd $TEST_ROOT /marco && ./polo/default.nix | grepQuiet "Polo" )
2024-11-18 15:45:18 +00:00
# https://github.com/NixOS/nix/issues/11892
mkdir $TEST_ROOT /issue-11892
cat >$TEST_ROOT /issue-11892/shebangscript <<EOF
#!$(type -P env) nix-shell
#! nix-shell -I nixpkgs=$shellDotNix
#! nix-shell -p 'callPackage (import ./my_package.nix) {}'
#! nix-shell -i bash
set -euxo pipefail
my_package
EOF
cat >$TEST_ROOT /issue-11892/my_package.nix <<EOF
{ stdenv, shell, ... } :
stdenv.mkDerivation {
name = "my_package" ;
buildCommand = ''
mkdir -p \$ out/bin
( echo "#!\${shell}"
echo "echo 'ok' 'baz11892'"
) > \$ out/bin/my_package
cat \$ out/bin/my_package
chmod a+x \$ out/bin/my_package
'' ;
}
EOF
chmod a+x $TEST_ROOT /issue-11892/shebangscript
$TEST_ROOT /issue-11892/shebangscript \
| tee /dev/stderr \
| grepQuiet "ok baz11892"
2024-07-06 19:31:24 +00:00
#####################
# Flake equivalents #
#####################
2020-07-02 16:32:45 +00:00
# Test 'nix develop'.
2021-11-26 08:56:48 +00:00
nix develop -f " $shellDotNix " shellDrv -c bash -c '[[ -n $stdenv ]]'
2020-11-09 16:57:39 +00:00
# Ensure `nix develop -c` preserves stdin
2021-12-09 15:26:46 +00:00
echo foo | nix develop -f " $shellDotNix " shellDrv -c cat | grepQuiet foo
2020-07-02 16:32:45 +00:00
2020-11-09 16:57:39 +00:00
# Ensure `nix develop -c` actually executes the command if stdout isn't a terminal
2021-12-09 15:26:46 +00:00
nix develop -f " $shellDotNix " shellDrv -c echo foo | & grepQuiet foo
2020-11-09 16:57:39 +00:00
2020-07-02 16:32:45 +00:00
# Test 'nix print-dev-env'.
2023-03-13 06:50:36 +00:00
2023-03-13 14:48:12 +00:00
nix print-dev-env -f " $shellDotNix " shellDrv > $TEST_ROOT /dev-env.sh
nix print-dev-env -f " $shellDotNix " shellDrv --json > $TEST_ROOT /dev-env.json
2023-03-13 06:50:36 +00:00
2023-05-09 15:50:22 +00:00
# Test with raw drv
shellDrv = $( nix-instantiate " $shellDotNix " -A shellDrv.out)
nix develop $shellDrv -c bash -c '[[ -n $stdenv ]]'
nix print-dev-env $shellDrv > $TEST_ROOT /dev-env2.sh
nix print-dev-env $shellDrv --json > $TEST_ROOT /dev-env2.json
diff $TEST_ROOT /dev-env{ ,2} .sh
diff $TEST_ROOT /dev-env{ ,2} .json
2023-03-13 06:50:36 +00:00
# Ensure `nix print-dev-env --json` contains variable assignments.
2023-03-13 14:48:12 +00:00
[ [ $( jq -r .variables.arr1.value[ 2] $TEST_ROOT /dev-env.json) = '3 4' ] ]
2023-03-13 06:50:36 +00:00
2024-02-04 04:02:06 +00:00
# Run tests involving `source <(nix print-dev-env)` in subshells to avoid modifying the current
2023-03-13 06:50:36 +00:00
# environment.
2021-07-09 10:10:48 +00:00
2024-02-04 04:02:06 +00:00
set -u
2021-12-09 15:26:46 +00:00
2023-03-13 06:50:36 +00:00
# Ensure `source <(nix print-dev-env)` modifies the environment.
(
path = $PATH
2023-03-13 14:48:12 +00:00
source $TEST_ROOT /dev-env.sh
2023-03-13 06:50:36 +00:00
[ [ -n $stdenv ] ]
[ [ ${ arr1 [2] } = "3 4" ] ]
[ [ ${ arr2 [1] } = $'\n' ] ]
[ [ ${ arr2 [2] } = $'x\ny' ] ]
[ [ $( fun) = blabla ] ]
2023-03-13 14:48:12 +00:00
[ [ $PATH = $( jq -r .variables.PATH.value $TEST_ROOT /dev-env.json) :$path ] ]
2023-03-13 06:50:36 +00:00
)
# Ensure `source <(nix print-dev-env)` handles the case when PATH is empty.
(
path = $PATH
PATH =
2023-03-13 14:48:12 +00:00
source $TEST_ROOT /dev-env.sh
[ [ $PATH = $( PATH = $path jq -r .variables.PATH.value $TEST_ROOT /dev-env.json) ] ]
2023-03-13 06:50:36 +00:00
)
2022-06-13 21:01:13 +00:00
# Test nix-shell with ellipsis and no `inNixShell` argument (for backwards compat with old nixpkgs)
cat >$TEST_ROOT /shell-ellipsis.nix <<EOF
2022-06-22 20:35:48 +00:00
{ system ? "x86_64-linux" , ... } @args:
assert ( !( args ? inNixShell) ) ;
2022-06-13 21:01:13 +00:00
( import $shellDotNix { } ) .shellDrv
EOF
nix-shell $TEST_ROOT /shell-ellipsis.nix --run "true"