Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nix-shell: unset IN_NIX_SHELL for nested nix commands #2797

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/nix-build/nix-build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ static void _main(int argc, char * * argv)

if (runEnv)
setenv("IN_NIX_SHELL", pure ? "pure" : "impure", 1);
else
unsetenv("IN_NIX_SHELL");

DrvInfos drvs;

Expand Down
2 changes: 2 additions & 0 deletions src/nix-env/nix-env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,8 @@ static int _main(int argc, char * * argv)

initPlugins();

unsetenv("IN_NIX_SHELL");

if (!op) throw UsageError("no operation specified");

auto store = openStore();
Expand Down
2 changes: 2 additions & 0 deletions src/nix-instantiate/nix-instantiate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ static int _main(int argc, char * * argv)

initPlugins();

unsetenv("IN_NIX_SHELL");

if (evalOnly && !wantsReadWrite)
settings.readOnlyMode = true;

Expand Down
2 changes: 2 additions & 0 deletions src/nix/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ void mainWrapped(int argc, char * * argv)

initPlugins();

unsetenv("IN_NIX_SHELL");

if (!args.command) args.showHelpAndExit();

Finally f([]() { stopProgressBar(); });
Expand Down
24 changes: 23 additions & 1 deletion tests/nix-shell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ output=$(nix-shell --pure --keep SELECTED_IMPURE_VAR shell.nix -A shellDrv --run
# Test nix-shell on a .drv symlink

# Legacy: absolute path and .drv extension required
nix-instantiate shell.nix -A shellDrv --indirect --add-root shell.drv
rm -f shell.drv
nix-instantiate shell.nix -A shellDrv --indirect --add-root ./shell.drv
[[ $(nix-shell --pure $PWD/shell.drv --run \
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX"') = " - foo - bar" ]]

# New behaviour: just needs to resolve to a derivation in the store
rm -f shell
nix-instantiate shell.nix -A shellDrv --indirect --add-root shell
[[ $(nix-shell --pure shell --run \
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX"') = " - foo - bar" ]]
Expand All @@ -55,3 +57,23 @@ chmod a+rx $TEST_ROOT/shell.shebang.rb

output=$($TEST_ROOT/shell.shebang.rb abc ruby)
[ "$output" = '-e load("'"$TEST_ROOT"'/shell.shebang.rb") -- abc ruby' ]

# Test IN_NIX_SHELL.
output=$(nix-shell --pure shell.nix -A shellDrv --run \
'echo $IN_NIX_SHELL')
[ "$output" = "pure" ]

output=$(nix-shell shell.nix -A shellDrv --run \
'echo $IN_NIX_SHELL')
[ "$output" = "impure" ]

# Nested nix commands inside a nix-shell.
cmd=$(type -p nix-instantiate)
output=$(nix-shell shell.nix -A shellDrv --run \
"$cmd --eval shell.nix -A inNixShell")
[ "$output" = "false" ]

cmd=$(type -p nix)
output=$(nix-shell shell.nix -A shellDrv --run \
"$cmd eval -f shell.nix inNixShell")
[ "$output" = "false" ]
2 changes: 2 additions & 0 deletions tests/shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
with import ./config.nix;

let pkgs = rec {
inNixShell = builtins.getEnv "IN_NIX_SHELL" != "";

setupSh = builtins.toFile "setup" ''
export VAR_FROM_STDENV_SETUP=foo
for pkg in $buildInputs; do
Expand Down