diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc
index 085650e4f2904f60de6fe94018d75150a6df7c64..357986980a0bc551b291549e31751712e0c687ba 100755
--- a/src/nix-build/nix-build.cc
+++ b/src/nix-build/nix-build.cc
@@ -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;
diff --git a/tests/nix-shell.sh b/tests/nix-shell.sh
index ee502dddb9555366ac16007eaa65f6b8b9bc36c4..235e2a5ffbf758c0c0975a92d12a554f38ae2a57 100644
--- a/tests/nix-shell.sh
+++ b/tests/nix-shell.sh
@@ -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 \
@@ -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
 
diff --git a/tests/shell.nix b/tests/shell.nix
index eb39f9039a88d2d2666b94ae6b298eb77b382696..6cb4f082b448deea923820828295ed5dea939af3 100644
--- a/tests/shell.nix
+++ b/tests/shell.nix
@@ -1,4 +1,4 @@
-{ }:
+{ inNixShell ? false }:
 
 with import ./config.nix;
 
@@ -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;
   };