Merge "Detect if in Nix3 shell" into main

This commit is contained in:
9p4
2025-03-09 17:45:14 +00:00
committed by Gerrit Code Review
5 changed files with 24 additions and 0 deletions
+4
View File
@@ -12,6 +12,10 @@
forgejo: rbt
github: 9999years
9p4:
display_name: Ersei Saggi
github: 9p4
Artturin:
github: Artturin
@@ -0,0 +1,11 @@
---
synopsis: Add a straightforward way to detect if in a Nix3 Shell
issues: [nix#6677, nix#3862]
cls: [2090]
category: Fixes
credits: [9p4]
---
Running `nix shell` or `nix develop` will now set `IN_NIX_SHELL` to
either `pure` or `impure`, depending on whether `--ignore-environment`
is passed. `nix develop` will always be an impure environment.
+1
View File
@@ -149,6 +149,7 @@ struct CmdShell : InstallablesCommand, MixEnvironment
unixPath.insert(unixPath.begin(), pathAdditions.begin(), pathAdditions.end());
auto unixPathString = concatStringsSep(":", unixPath);
setenv("PATH", unixPathString.c_str(), 1);
setenv("IN_NIX_SHELL", ignoreEnvironment ? "pure" : "impure", 1);
Strings args;
for (auto & arg : command) args.push_back(arg);
+4
View File
@@ -64,4 +64,8 @@ echo "\$SHELL"
EOF
)" -ef "$BASH_INTERACTIVE_EXECUTABLE" ]]
# Test whether `nix develop` sets `IN_NIX_SHELL`
[[ "$(nix develop --no-write-lock-file .#hello -c sh -c 'echo $IN_NIX_SHELL')" = "impure" ]]
[[ "$(nix develop --no-write-lock-file --ignore-environment .#hello -c /bin/sh -c 'echo $IN_NIX_SHELL')" = "impure" ]]
clearStore
+4
View File
@@ -38,3 +38,7 @@ path2=$(nix shell --sandbox-paths '/nix? /bin? /lib? /lib64? /usr?' --store $TES
[[ $path/bin/hello = $path2 ]]
[[ -e $TEST_ROOT/store0/nix/store/$(basename $path)/bin/hello ]]
# Test whether `nix shell` sets `IN_NIX_SHELL` appropriately
[[ "$(nix shell -f shell-hello.nix -c sh -c 'echo $IN_NIX_SHELL')" == impure ]]
[[ "$(nix shell --ignore-environment -f shell-hello.nix -c /bin/sh -c 'echo $IN_NIX_SHELL')" == pure ]]