Skip to content

Commit

Permalink
Add inNixShell = true to nix-shell auto-call
Browse files Browse the repository at this point in the history
This is an alternative to the IN_NIX_SHELL environment variable,
allowing the expression to adapt itself to nix-shell without
triggering those adaptations when used as a dependency of another
shell.

Closes #3147
  • Loading branch information
roberth committed Oct 27, 2019
1 parent 2f96a89 commit 9d612c3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
16 changes: 15 additions & 1 deletion src/nix-build/nix-build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,21 @@ static void _main(int argc, char * * argv)
auto state = std::make_unique<EvalState>(myArgs.searchPath, store);
state->repair = repair;

Bindings & autoArgs = *myArgs.getAutoArgs(*state);
Bindings & autoArgs = *[&](){
Bindings *userAutoArgs = myArgs.getAutoArgs(*state);
if (runEnv) {
Bindings * res = state->allocBindings(userAutoArgs->size() + 1);
Value * tru = state->allocValue();
mkBool(*tru, true);
res->push_back(Attr(state->symbols.create("inNixShell"), tru));
for (auto & i : *userAutoArgs) {
res->push_back(i);
}
res->sort();
return res;
}
else return userAutoArgs;
}();

if (packages) {
std::ostringstream joined;
Expand Down
8 changes: 4 additions & 4 deletions tests/nix-shell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export IMPURE_VAR=foo
export SELECTED_IMPURE_VAR=baz
export NIX_BUILD_SHELL=$SHELL
output=$(nix-shell --pure shell.nix -A shellDrv --run \
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX"')
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX - $TEST_inNixShell"')

[ "$output" = " - foo - bar" ]
[ "$output" = " - foo - bar - true" ]

# Test --keep
output=$(nix-shell --pure --keep SELECTED_IMPURE_VAR shell.nix -A shellDrv --run \
Expand All @@ -19,10 +19,10 @@ output=$(nix-shell --pure --keep SELECTED_IMPURE_VAR shell.nix -A shellDrv --run

# Test nix-shell on a .drv
[[ $(nix-shell --pure $(nix-instantiate shell.nix -A shellDrv) --run \
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX"') = " - foo - bar" ]]
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX - $TEST_inNixShell"') = " - foo - bar - false" ]]

[[ $(nix-shell --pure $(nix-instantiate shell.nix -A shellDrv) --run \
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX"') = " - foo - bar" ]]
'echo "$IMPURE_VAR - $VAR_FROM_STDENV_SETUP - $VAR_FROM_NIX - $TEST_inNixShell"') = " - foo - bar - false" ]]

# Test nix-shell on a .drv symlink

Expand Down
3 changes: 2 additions & 1 deletion tests/shell.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ }:
{ inNixShell ? false }:

with import ./config.nix;

Expand All @@ -22,6 +22,7 @@ let pkgs = rec {
name = "shellDrv";
builder = "/does/not/exist";
VAR_FROM_NIX = "bar";
TEST_inNixShell = if inNixShell then "true" else "false";
inherit stdenv;
};

Expand Down

0 comments on commit 9d612c3

Please sign in to comment.