Detect if in Nix3 shell

This replicates behaviour found in the Nix2 commands, where IN_NIX_SHELL
is set. This is for shells to determine whether they are inside of a
Nix3 shell, and set a custom prompt accordingly.

For example, Fish's Tide prompt framework checks for that environment
variable and displays it in the prompt, indicating that the shell is in
a Nix environment.

This is not new behaviour, and the old Nix2 commands set the variable.

If the shell that is created is a "pure" shell, ie --ignore-environment
is passed, then IN_NIX_SHELL will be set to "pure". However, "nix
develop" will always create an impure environment.

Replicated from my Nix PR: https://github.com/NixOS/nix/pull/8885

Change-Id: I695cdc336f76541940a302835124fe7d8f7f39b2
Signed-off-by: Ersei Saggi <vcs@ersei.net>
This commit is contained in:
Ersei Saggi
2025-03-07 07:45:08 -05:00
parent 18975fa016
commit 7928669ef9
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 ]]