From dadc7eb3297e6c2fb0c0e01149c8fcebd80770c1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= <joerg@thalheim.io>
Date: Tue, 26 Feb 2019 11:45:54 +0000
Subject: [PATCH] treewide: use runtimeShell instead of stdenv.shell whenever
 possible

Whenever we create scripts that are installed to $out, we must use runtimeShell
in order to get the shell that can be executed on the machine we create the
package for. This is relevant for cross-compiling. The only use case for
stdenv.shell are scripts that are executed as part of the build system.
Usages in checkPhase are borderline however to decrease the likelyhood
of people copying the wrong examples, I decided to use runtimeShell as well.
---
 doc/functions/dockertools.xml                 |  4 ++--
 pkgs/applications/audio/spotifywm/default.nix |  4 ++--
 .../editors/standardnotes/default.nix         |  4 ++--
 .../applications/editors/sublime/3/common.nix |  4 ++--
 .../applications/editors/vim/configurable.nix | 21 ++++++++++---------
 .../applications/graphics/alchemy/default.nix |  4 ++--
 .../graphics/autotrace/default.nix            |  7 ++++---
 .../graphics/swingsane/default.nix            |  4 ++--
 pkgs/applications/graphics/wings/default.nix  |  5 ++---
 .../graphics/zgrviewer/default.nix            |  4 ++--
 pkgs/applications/misc/avrdudess/default.nix  |  4 ++--
 .../misc/curabydagoma/default.nix             |  4 ++--
 pkgs/applications/misc/glava/default.nix      |  4 ++--
 .../misc/hello-unfree/default.nix             |  4 ++--
 .../applications/misc/jbidwatcher/default.nix |  4 ++--
 .../misc/multibootusb/default.nix             |  4 ++--
 pkgs/applications/misc/nix-tour/default.nix   |  4 ++--
 pkgs/applications/misc/openjump/default.nix   |  4 ++--
 .../misc/terminal-notifier/default.nix        |  4 ++--
 .../misc/thinking-rock/default.nix            | 10 ++++-----
 pkgs/applications/misc/vue/default.nix        |  4 ++--
 .../browsers/firefox-bin/update.nix           |  3 ++-
 .../networking/browsers/firefox/update.nix    |  3 ++-
 .../telegram/telegram-cli/default.nix         |  7 ++++---
 .../networking/irc/weechat/wrapper.nix        |  4 ++--
 .../mailreaders/msgviewer/default.nix         |  4 ++--
 .../mailreaders/neomutt/default.nix           |  4 ++--
 pkgs/applications/office/mendeley/default.nix |  3 ++-
 pkgs/applications/office/mendeley/update.nix  |  4 ++--
 .../applications/office/moneyplex/default.nix |  5 +++--
 .../science/electronics/alliance/default.nix  |  6 +++---
 .../science/electronics/eagle/eagle7.nix      |  4 ++--
 .../science/logic/hol_light/default.nix       |  4 ++--
 .../science/logic/leo3/binary.nix             |  4 ++--
 .../science/math/sage/sage-src.nix            |  3 ++-
 .../video/dvd-slideshow/default.nix           |  4 ++--
 pkgs/build-support/docker/default.nix         |  7 ++++---
 pkgs/build-support/release/ant-build.nix      |  2 +-
 .../singularity-tools/default.nix             |  3 ++-
 pkgs/build-support/trivial-builders.nix       | 14 ++++++-------
 .../vm/windows/controller/default.nix         |  3 ++-
 .../elementary-session-settings/default.nix   |  4 ++--
 pkgs/development/compilers/ccl/default.nix    |  4 ++--
 .../development/compilers/cmdstan/default.nix |  4 ++--
 pkgs/development/compilers/go/1.11.nix        |  6 +++---
 .../libraries/safefile/default.nix            |  4 ++--
 .../misc/avr8-burn-omat/default.nix           |  6 +++---
 pkgs/development/pharo/vm/build-vm.nix        |  3 ++-
 pkgs/development/pharo/vm/vms.nix             |  2 +-
 pkgs/development/python-modules/cmd2/old.nix  |  5 +++--
 .../python-modules/pandas/default.nix         |  5 +++--
 .../tools/analysis/jdepend/default.nix        |  4 ++--
 .../tools/build-managers/bazel/default.nix    |  5 +++--
 .../tools/build-managers/gup/default.nix      |  5 +++--
 .../tools/haskell/hyper-haskell/default.nix   |  5 +++--
 pkgs/development/tools/misc/ccls/default.nix  |  4 ++--
 .../development/tools/misc/cquery/default.nix |  5 +++--
 pkgs/development/tools/misc/ctags/wrapped.nix |  2 +-
 .../development/tools/misc/distcc/default.nix |  5 +++--
 .../tools/misc/opengrok/default.nix           |  4 ++--
 .../tools/misc/saleae-logic/default.nix       |  4 ++--
 .../tools/rust/bindgen/default.nix            |  5 +++--
 pkgs/development/tools/wp-cli/default.nix     |  4 ++--
 pkgs/development/tools/xcbuild/wrapper.nix    |  5 +++--
 pkgs/development/web/nodejs/update.nix        |  3 ++-
 pkgs/games/andyetitmoves/default.nix          |  4 ++--
 pkgs/games/astromenace/default.nix            |  4 ++--
 pkgs/games/cataclysm-dda/common.nix           |  4 ++--
 pkgs/games/eduke32/default.nix                |  4 ++--
 pkgs/games/fsg/default.nix                    |  5 +++--
 pkgs/games/oilrush/default.nix                |  8 +++----
 pkgs/games/openttd/default.nix                |  4 ++--
 pkgs/games/sauerbraten/default.nix            |  6 +++---
 pkgs/games/scummvm/games.nix                  |  4 ++--
 pkgs/games/simutrans/default.nix              |  5 ++---
 pkgs/games/speed-dreams/default.nix           |  4 ++--
 pkgs/games/steam/chrootenv.nix                |  9 ++++----
 pkgs/games/steam/steam.nix                    |  4 ++--
 pkgs/games/tcl2048/default.nix                |  4 ++--
 pkgs/games/tibia/default.nix                  |  4 ++--
 pkgs/games/tremulous/default.nix              |  4 ++--
 pkgs/games/trigger/default.nix                |  5 +++--
 pkgs/games/vessel/default.nix                 | 18 ++++++++--------
 pkgs/games/worldofgoo/default.nix             | 17 ++++++++-------
 .../cups/drivers/mfcj6510dwlpr/default.nix    |  4 ++--
 pkgs/misc/emulators/higan/default.nix         |  9 ++++----
 pkgs/misc/emulators/kega-fusion/default.nix   |  4 ++--
 .../retroarch/kodi-advanced-launchers.nix     |  4 ++--
 pkgs/misc/emulators/retrofe/default.nix       |  4 ++--
 pkgs/misc/vim-plugins/vim-utils.nix           |  4 ++--
 .../vscode-extensions/cpptools/default.nix    |  4 ++--
 pkgs/os-specific/darwin/stubs/default.nix     |  4 ++--
 pkgs/os-specific/linux/bbswitch/default.nix   |  6 +++---
 pkgs/os-specific/linux/pam_usb/default.nix    |  4 ++--
 pkgs/os-specific/linux/rfkill/udev.nix        |  4 ++--
 pkgs/os-specific/linux/tp_smapi/update.nix    |  4 ++--
 pkgs/servers/mail/system-sendmail/default.nix |  4 ++--
 pkgs/servers/meteor/default.nix               |  4 ++--
 pkgs/servers/monitoring/plugins/default.nix   |  5 +++--
 pkgs/servers/nosql/eventstore/default.nix     |  4 ++--
 pkgs/servers/web-apps/codimd/default.nix      |  4 ++--
 pkgs/servers/xmpp/pyIRCt/default.nix          |  4 ++--
 pkgs/servers/xmpp/pyMAILt/default.nix         |  4 ++--
 pkgs/servers/zookeeper/default.nix            |  4 ++--
 pkgs/tools/X11/primus/default.nix             |  3 ++-
 pkgs/tools/audio/beets/default.nix            |  3 ++-
 pkgs/tools/backup/flockit/default.nix         |  4 ++--
 pkgs/tools/compression/mozlz4a/default.nix    |  4 ++--
 pkgs/tools/graphics/briss/default.nix         |  4 ++--
 pkgs/tools/graphics/mscgen/default.nix        |  4 ++--
 pkgs/tools/inputmethods/ibus/default.nix      |  4 ++--
 pkgs/tools/misc/fzf/default.nix               |  4 ++--
 .../misc/odroid-xu3-bootloader/default.nix    |  4 ++--
 pkgs/tools/misc/smc/default.nix               |  4 ++--
 pkgs/tools/misc/umlet/default.nix             |  4 ++--
 pkgs/tools/misc/xdummy/default.nix            |  4 ++--
 pkgs/tools/misc/xfstests/default.nix          |  4 ++--
 pkgs/tools/misc/zsh-autoenv/default.nix       |  4 ++--
 pkgs/tools/networking/airfield/default.nix    |  4 ++--
 pkgs/tools/networking/burpsuite/default.nix   |  4 ++--
 pkgs/tools/networking/easyrsa/default.nix     |  4 ++--
 pkgs/tools/networking/slack-cli/default.nix   |  4 ++--
 pkgs/tools/networking/zap/default.nix         |  6 +++---
 .../appimage-run/default.nix                  |  4 ++--
 .../package-management/nix-pin/default.nix    |  5 +++--
 .../nix-update-source/default.nix             |  5 +++--
 pkgs/tools/security/jd-gui/default.nix        |  4 ++--
 pkgs/tools/security/keybase/gui.nix           |  5 +++--
 pkgs/tools/text/esh/default.nix               |  6 +++---
 pkgs/tools/typesetting/fop/default.nix        |  4 ++--
 pkgs/top-level/stage.nix                      |  1 +
 131 files changed, 326 insertions(+), 294 deletions(-)

diff --git a/doc/functions/dockertools.xml b/doc/functions/dockertools.xml
index ff446cbfffdd..2c8eb2cb7743 100644
--- a/doc/functions/dockertools.xml
+++ b/doc/functions/dockertools.xml
@@ -47,7 +47,7 @@ buildImage {
 
   contents = pkgs.redis; <co xml:id='ex-dockerTools-buildImage-6' />
   runAsRoot = '' <co xml:id='ex-dockerTools-buildImage-runAsRoot' />
-    #!${stdenv.shell}
+    #!${pkgs.runtimeShell}
     mkdir -p /data
   '';
 
@@ -544,7 +544,7 @@ buildImage {
   name = "shadow-basic";
 
   runAsRoot = ''
-    #!${stdenv.shell}
+    #!${pkgs.runtimeShell}
     ${shadowSetup}
     groupadd -r redis
     useradd -r -g redis redis
diff --git a/pkgs/applications/audio/spotifywm/default.nix b/pkgs/applications/audio/spotifywm/default.nix
index f22340b69412..64ae9491535a 100644
--- a/pkgs/applications/audio/spotifywm/default.nix
+++ b/pkgs/applications/audio/spotifywm/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, spotify, xorg }:
+{ stdenv, fetchFromGitHub, spotify, xorg, runtimeShell }:
 stdenv.mkDerivation rec {
   name = "spotifywm-unstable-${version}";
   version = "2016-11-28";
@@ -15,7 +15,7 @@ stdenv.mkDerivation rec {
   propagatedBuildInputs = [ spotify ];
 
   installPhase = ''
-    echo "#!${stdenv.shell}" > spotifywm
+    echo "#!${runtimeShell}" > spotifywm
     echo "LD_PRELOAD="$out/lib/spotifywm.so" ${spotify}/bin/spotify \$*" >> spotifywm
     install -Dm644 spotifywm.so $out/lib/spotifywm.so
     install -Dm755 spotifywm $out/bin/spotifywm
diff --git a/pkgs/applications/editors/standardnotes/default.nix b/pkgs/applications/editors/standardnotes/default.nix
index 5b0560e5831d..454b7ad554ba 100644
--- a/pkgs/applications/editors/standardnotes/default.nix
+++ b/pkgs/applications/editors/standardnotes/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, appimage-run, fetchurl }:
+{ stdenv, appimage-run, fetchurl, runtimeShell }:
 
 let
   version = "3.0.6";
@@ -29,7 +29,7 @@ stdenv.mkDerivation rec {
   installPhase = ''
     mkdir -p $out/{bin,share}
     cp $src $out/share/standardNotes.AppImage
-    echo "#!${stdenv.shell}" > $out/bin/standardnotes
+    echo "#!${runtimeShell}" > $out/bin/standardnotes
     echo "${appimage-run}/bin/appimage-run $out/share/standardNotes.AppImage" >> $out/bin/standardnotes
     chmod +x $out/bin/standardnotes $out/share/standardNotes.AppImage
   '';
diff --git a/pkgs/applications/editors/sublime/3/common.nix b/pkgs/applications/editors/sublime/3/common.nix
index 71bd3544febb..937764f0ab8b 100644
--- a/pkgs/applications/editors/sublime/3/common.nix
+++ b/pkgs/applications/editors/sublime/3/common.nix
@@ -1,6 +1,6 @@
 {buildVersion, x32sha256, x64sha256}:
 
-{ fetchurl, stdenv, glib, xorg, cairo, gtk2, gtk3, pango, makeWrapper, wrapGAppsHook, openssl, bzip2,
+{ fetchurl, stdenv, glib, xorg, cairo, gtk2, gtk3, pango, makeWrapper, wrapGAppsHook, openssl, bzip2, runtimeShell,
   pkexecPath ? "/run/wrappers/bin/pkexec", libredirect,
   gksuSupport ? false, gksu, unzip, zip, bash}:
 
@@ -115,7 +115,7 @@ in stdenv.mkDerivation (rec {
     mkdir -p $out/bin
 
     cat > $out/bin/subl <<-EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     exec $sublime/sublime_text "\$@"
     EOF
     chmod +x $out/bin/subl
diff --git a/pkgs/applications/editors/vim/configurable.nix b/pkgs/applications/editors/vim/configurable.nix
index 7ee28adfa47f..3b75d08787e8 100644
--- a/pkgs/applications/editors/vim/configurable.nix
+++ b/pkgs/applications/editors/vim/configurable.nix
@@ -7,6 +7,7 @@
 , vimPlugins
 , makeWrapper
 , wrapGAppsHook
+, runtimeShell
 
 # apple frameworks
 , CoreServices, CoreData, Cocoa, Foundation, libobjc, cf-private
@@ -157,22 +158,22 @@ in stdenv.mkDerivation rec {
 
     rewrap () {
       rm -f "$out/bin/$1"
-      echo -e '#!${stdenv.shell}\n"'"$out/bin/vim"'" '"$2"' "$@"' > "$out/bin/$1"
+      echo -e '#!${runtimeShell}\n"'"$out/bin/vim"'" '"$2"' "$@"' > "$out/bin/$1"
       chmod a+x "$out/bin/$1"
     }
 
-    rewrap ex -e	
-    rewrap view -R	
-    rewrap gvim -g	
-    rewrap gex -eg	
-    rewrap gview -Rg	
-    rewrap rvim -Z	
-    rewrap rview -RZ	
-    rewrap rgvim -gZ	
+    rewrap ex -e
+    rewrap view -R
+    rewrap gvim -g
+    rewrap gex -eg
+    rewrap gview -Rg
+    rewrap rvim -Z
+    rewrap rview -RZ
+    rewrap rgvim -gZ
     rewrap rgview -RgZ
     rewrap evim    -y
     rewrap eview   -yR
-    rewrap vimdiff -d	
+    rewrap vimdiff -d
     rewrap gvimdiff -gd
   '';
 
diff --git a/pkgs/applications/graphics/alchemy/default.nix b/pkgs/applications/graphics/alchemy/default.nix
index 30223658f535..09664889f35b 100644
--- a/pkgs/applications/graphics/alchemy/default.nix
+++ b/pkgs/applications/graphics/alchemy/default.nix
@@ -1,4 +1,4 @@
-{stdenv, fetchurl, jre}:
+{ stdenv, fetchurl, jre, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "alchemy-${version}";
@@ -15,7 +15,7 @@ stdenv.mkDerivation rec {
     mkdir -p $out/bin $out/share
     cp -a . $out/share/alchemy
     cat >> $out/bin/alchemy << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     cd $out/share/alchemy
     ${jre}/bin/java -jar Alchemy.jar "$@"
     EOF
diff --git a/pkgs/applications/graphics/autotrace/default.nix b/pkgs/applications/graphics/autotrace/default.nix
index 7af7c8986f2f..cc08dce906c3 100644
--- a/pkgs/applications/graphics/autotrace/default.nix
+++ b/pkgs/applications/graphics/autotrace/default.nix
@@ -1,5 +1,6 @@
-{ stdenv, fetchurl, callPackage, libpng12, imagemagick,
-  autoreconfHook, glib, pstoedit, pkgconfig, gettext, gd, darwin }:
+{ stdenv, fetchurl, callPackage, libpng12, imagemagick
+, autoreconfHook, glib, pstoedit, pkgconfig, gettext, gd, darwin
+, runtimeShell }:
 
 # TODO: Figure out why the resultant binary is somehow linked against
 # libpng16.so.16 rather than libpng12.
@@ -51,7 +52,7 @@ stdenv.mkDerivation rec {
     # pstoedit-config no longer exists, it was replaced with pkg-config
     mkdir wrappers
     cat >wrappers/pstoedit-config <<'EOF'
-    #!${stdenv.shell}
+    #!${runtimeShell}
     # replace --version with --modversion for pkg-config
     args=''${@/--version/--modversion}
     exec pkg-config pstoedit "''${args[@]}"
diff --git a/pkgs/applications/graphics/swingsane/default.nix b/pkgs/applications/graphics/swingsane/default.nix
index ac3de4a4e141..d0f2a48c589d 100644
--- a/pkgs/applications/graphics/swingsane/default.nix
+++ b/pkgs/applications/graphics/swingsane/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, makeDesktopItem, unzip, jre }:
+{ stdenv, fetchurl, makeDesktopItem, unzip, jre, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "swingsane-${version}";
@@ -16,7 +16,7 @@ stdenv.mkDerivation rec {
   installPhase = let
 
     execWrapper = ''
-      #!${stdenv.shell}
+      #!${runtimeShell}
       exec ${jre}/bin/java -jar $out/share/java/swingsane/swingsane-${version}.jar "$@"
     '';
 
diff --git a/pkgs/applications/graphics/wings/default.nix b/pkgs/applications/graphics/wings/default.nix
index e27f074b21ba..891f3586fa59 100644
--- a/pkgs/applications/graphics/wings/default.nix
+++ b/pkgs/applications/graphics/wings/default.nix
@@ -1,4 +1,4 @@
-{ fetchurl, stdenv, erlang, cl, libGL, libGLU }:
+{ fetchurl, stdenv, erlang, cl, libGL, libGLU, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "wings-2.2.1";
@@ -28,7 +28,7 @@ stdenv.mkDerivation rec {
     cp ebin/* $out/lib/${name}/ebin
     cp -R textures shaders plugins $out/lib/$name
     cat << EOF > $out/bin/wings
-    #!${stdenv.shell}
+    #!${runtimeShell}
     ${erlang}/bin/erl \
       -pa $out/lib/${name}/ebin -run wings_start start_halt "$@"
     EOF
@@ -43,4 +43,3 @@ stdenv.mkDerivation rec {
     platforms = with stdenv.lib.platforms; linux;
   };
 }
-
diff --git a/pkgs/applications/graphics/zgrviewer/default.nix b/pkgs/applications/graphics/zgrviewer/default.nix
index c60d4b7b904d..ac3d303a7a60 100644
--- a/pkgs/applications/graphics/zgrviewer/default.nix
+++ b/pkgs/applications/graphics/zgrviewer/default.nix
@@ -1,4 +1,4 @@
-{stdenv, fetchurl, jre, unzip}:
+{ stdenv, fetchurl, jre, unzip, runtimeShell }:
 stdenv.mkDerivation rec {
   version = "0.9.0";
   pname = "zgrviewer";
@@ -16,7 +16,7 @@ stdenv.mkDerivation rec {
 
     cp -r target/* "$out/share/java/zvtm/"
 
-    echo '#!${stdenv.shell}' > "$out/bin/zgrviewer"
+    echo '#!${runtimeShell}' > "$out/bin/zgrviewer"
     echo "${jre}/lib/openjdk/jre/bin/java -jar '$out/share/java/zvtm/zgrviewer-${version}.jar' \"\$@\"" >> "$out/bin/zgrviewer"
     chmod a+x "$out/bin/zgrviewer"
   '';
diff --git a/pkgs/applications/misc/avrdudess/default.nix b/pkgs/applications/misc/avrdudess/default.nix
index 1144d5152847..4227b4155acf 100644
--- a/pkgs/applications/misc/avrdudess/default.nix
+++ b/pkgs/applications/misc/avrdudess/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, unzip, mono, avrdude, gtk2, xdg_utils }:
+{ stdenv, runtimeShell, fetchurl, unzip, mono, avrdude, gtk2, xdg_utils }:
 
 stdenv.mkDerivation rec {
   name = "avrdudess-2.2.20140102";
@@ -19,7 +19,7 @@ stdenv.mkDerivation rec {
     unzip "$src" -d "$out/avrdudess"
 
     cat >> "$out/bin/avrdudess" << __EOF__
-    #!${stdenv.shell}
+    #!${runtimeShell}
     export LD_LIBRARY_PATH="${stdenv.lib.makeLibraryPath [gtk2 mono]}"
     # We need PATH from user env for xdg-open to find its tools, which
     # typically depend on the currently running desktop environment.
diff --git a/pkgs/applications/misc/curabydagoma/default.nix b/pkgs/applications/misc/curabydagoma/default.nix
index 1ea106d6a18b..4e620425d5c3 100644
--- a/pkgs/applications/misc/curabydagoma/default.nix
+++ b/pkgs/applications/misc/curabydagoma/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, lib, fetchurl, python, pythonPackages, unzip }:
+{ stdenv, runtimeShell, lib, fetchurl, python, pythonPackages, unzip }:
 
 # This package uses a precompiled "binary" distribution of CuraByDagoma,
 # distributed by the editor.
@@ -44,7 +44,7 @@ stdenv.mkDerivation rec {
 
     mkdir $out/bin
     cat > $out/bin/curabydago <<EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     export PYTHONPATH=$PYTHONPATH
     ${python.out}/bin/python $out/curabydago/cura.py
     EOF
diff --git a/pkgs/applications/misc/glava/default.nix b/pkgs/applications/misc/glava/default.nix
index 0cac0e6fd3f5..8432ddee0073 100644
--- a/pkgs/applications/misc/glava/default.nix
+++ b/pkgs/applications/misc/glava/default.nix
@@ -1,12 +1,12 @@
 { stdenv, writeScript, fetchFromGitHub
 , libGL, libX11, libXext, python3, libXrandr, libXrender, libpulseaudio, libXcomposite
-, enableGlfw ? false, glfw }:
+, enableGlfw ? false, glfw, runtimeShell }:
 
 let
   inherit (stdenv.lib) optional makeLibraryPath;
 
   wrapperScript = writeScript "glava" ''
-    #!${stdenv.shell}
+    #!${runtimeShell}
     case "$1" in
       --copy-config)
         # The binary would symlink it, which won't work in Nix because the
diff --git a/pkgs/applications/misc/hello-unfree/default.nix b/pkgs/applications/misc/hello-unfree/default.nix
index c055bddec694..1647a09edfd6 100644
--- a/pkgs/applications/misc/hello-unfree/default.nix
+++ b/pkgs/applications/misc/hello-unfree/default.nix
@@ -1,4 +1,4 @@
-{ stdenv }:
+{ stdenv, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "example-unfree-package-${version}";
@@ -9,7 +9,7 @@ stdenv.mkDerivation rec {
   installPhase = ''
     mkdir -p $out/bin
     cat > $out/bin/hello-unfree << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     echo "Hello, you are running an unfree system!"
     EOF
     chmod +x $out/bin/hello-unfree
diff --git a/pkgs/applications/misc/jbidwatcher/default.nix b/pkgs/applications/misc/jbidwatcher/default.nix
index 2c46ea3ac9a6..72a24026a89d 100644
--- a/pkgs/applications/misc/jbidwatcher/default.nix
+++ b/pkgs/applications/misc/jbidwatcher/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, java }:
+{ stdenv, fetchurl, java, runtimeShell }:
 
 stdenv.mkDerivation rec {
   pname = "jbidwatcher";
@@ -21,7 +21,7 @@ stdenv.mkDerivation rec {
 
   installPhase = ''
     mkdir -p "$out/bin"
-    echo > "$out/bin/${pname}" "#!${stdenv.shell}"
+    echo > "$out/bin/${pname}" "#!${runtimeShell}"
     echo >>"$out/bin/${pname}" "${java}/bin/java -Xmx512m -jar ${jarfile}"
     chmod +x "$out/bin/${pname}"
     install -D -m644 ${src} ${jarfile}
diff --git a/pkgs/applications/misc/multibootusb/default.nix b/pkgs/applications/misc/multibootusb/default.nix
index 88a7b94897dc..a2678d6067f9 100644
--- a/pkgs/applications/misc/multibootusb/default.nix
+++ b/pkgs/applications/misc/multibootusb/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, python36Packages, fetchFromGitHub, libxcb, mtools, p7zip, parted, procps, utillinux, qt5 }:
+{ stdenv, python36Packages, fetchFromGitHub, libxcb, mtools, p7zip, parted, procps, utillinux, qt5, runtimeShell }:
 python36Packages.buildPythonApplication rec {
   pname = "multibootusb";
   name = "${pname}-${version}";
@@ -40,7 +40,7 @@ python36Packages.buildPythonApplication rec {
 
     mkdir "$out/bin"
     cat > "$out/bin/${pname}" <<EOF
-      #!${stdenv.shell}
+      #!${runtimeShell}
       cd "$share"
       export PYTHONPATH="$PYTHONPATH:$share"
       export PATH="$PATH:${parted}/bin:${procps}/bin"
diff --git a/pkgs/applications/misc/nix-tour/default.nix b/pkgs/applications/misc/nix-tour/default.nix
index 7e5a3a7840d7..1cc7f419aa2e 100644
--- a/pkgs/applications/misc/nix-tour/default.nix
+++ b/pkgs/applications/misc/nix-tour/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchgit, electron } :
+{ stdenv, fetchgit, electron, runtimeShell } :
 
 stdenv.mkDerivation rec {
   name = "nix-tour-${version}";
@@ -19,7 +19,7 @@ stdenv.mkDerivation rec {
     mkdir -p $out/share
     cp -R * $out/share
     chmod 0755 $out/share/ -R
-    echo "#!${stdenv.shell}" > $out/bin/nix-tour
+    echo "#!${runtimeShell}" > $out/bin/nix-tour
     echo "cd $out/share/" >> $out/bin/nix-tour
     echo "${electron}/bin/electron $out/share/electron-main.js" >> $out/bin/nix-tour
     chmod 0755 $out/bin/nix-tour
diff --git a/pkgs/applications/misc/openjump/default.nix b/pkgs/applications/misc/openjump/default.nix
index 09ec34338628..c98defee57a0 100644
--- a/pkgs/applications/misc/openjump/default.nix
+++ b/pkgs/applications/misc/openjump/default.nix
@@ -1,4 +1,4 @@
-{stdenv, fetchurl, unzip}:
+{ stdenv, fetchurl, unzip, runtimeShell }:
 
 stdenv.mkDerivation {
   name = "openjump-1.3.1";
@@ -17,7 +17,7 @@ stdenv.mkDerivation {
     s=$out/bin/OpenJump
     dir=$(echo $out/openjump-*)
     cat >> $s << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     cd $dir/bin
     exec ${stdenv.shell} openjump.sh
     EOF
diff --git a/pkgs/applications/misc/terminal-notifier/default.nix b/pkgs/applications/misc/terminal-notifier/default.nix
index b05c8a803996..3599bd0030b9 100644
--- a/pkgs/applications/misc/terminal-notifier/default.nix
+++ b/pkgs/applications/misc/terminal-notifier/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, lib, fetchzip }:
+{ stdenv, runtimeShell, lib, fetchzip }:
 
 stdenv.mkDerivation rec {
   name = "terminal-notifier-${version}";
@@ -18,7 +18,7 @@ stdenv.mkDerivation rec {
     mkdir -p $out/bin
     cp -r terminal-notifier.app $out/Applications
     cat >$out/bin/terminal-notifier <<EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     cd $out/Applications/terminal-notifier.app
     exec ./Contents/MacOS/terminal-notifier "\$@"
     EOF
diff --git a/pkgs/applications/misc/thinking-rock/default.nix b/pkgs/applications/misc/thinking-rock/default.nix
index ab9611326cae..c057fde95d7f 100644
--- a/pkgs/applications/misc/thinking-rock/default.nix
+++ b/pkgs/applications/misc/thinking-rock/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl }:
+{ stdenv, fetchurl, runtimeShell }:
 
 stdenv.mkDerivation {
   name = "thinkingrock-binary-2.2.1";
@@ -11,7 +11,7 @@ stdenv.mkDerivation {
   /* it would be a really bad idea to put thinkingrock tr executable in PATH!
      the tr.sh script does use the coreutils tr itself
      That's why I've renamed the wrapper and called it thinkingrock
-     However you may not rename the bin/tr script cause it will notice and throw an 
+     However you may not rename the bin/tr script cause it will notice and throw an
      "java.lang.IllegalArgumentException: Malformed branding token: thinkingrock"
      exception. I hope that's fine
   */
@@ -24,15 +24,15 @@ stdenv.mkDerivation {
     mkdir -p $out/{nix-support/tr-files,bin}
     cp -r . $out/nix-support/tr-files
     cat >> $out/bin/thinkingrock << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     exec $out/nix-support/tr-files/bin/tr "$@"
     EOF
     chmod +x $out/bin/thinkingrock
   '';
-  
+
   installPhase = ":";
 
-  meta = { 
+  meta = {
     description = "Task management system";
     homepage = http://www.thinkingrock.com.au/;
     license = "CDDL"; # Common Development and Distribution License
diff --git a/pkgs/applications/misc/vue/default.nix b/pkgs/applications/misc/vue/default.nix
index a448d35485a7..1feec9e45509 100644
--- a/pkgs/applications/misc/vue/default.nix
+++ b/pkgs/applications/misc/vue/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, jre }:
+{ stdenv, fetchurl, jre, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "vue-${version}";
@@ -13,7 +13,7 @@ stdenv.mkDerivation rec {
   installPhase = ''
     mkdir -p "$out"/{share/vue,bin}
     cp ${src} "$out/share/vue/vue.jar"
-    echo '#!${stdenv.shell}' >> "$out/bin/vue"
+    echo '#!${runtimeShell}' >> "$out/bin/vue"
     echo '${jre}/bin/java -jar "'"$out/share/vue/vue.jar"'" "$@"' >> "$out/bin/vue"
     chmod a+x "$out/bin/vue"
   '';
diff --git a/pkgs/applications/networking/browsers/firefox-bin/update.nix b/pkgs/applications/networking/browsers/firefox-bin/update.nix
index ee022e329f9f..97163ffa7620 100644
--- a/pkgs/applications/networking/browsers/firefox-bin/update.nix
+++ b/pkgs/applications/networking/browsers/firefox-bin/update.nix
@@ -8,6 +8,7 @@
 , gnugrep
 , curl
 , gnupg
+, runtimeShell
 , baseName ? "firefox"
 , basePath ? "pkgs/applications/networking/browsers/firefox-bin"
 , baseUrl
@@ -18,7 +19,7 @@ let
     channel != "release";
 
 in writeScript "update-${name}" ''
-  #!${stdenv.shell}
+  #!${runtimeShell}
   PATH=${coreutils}/bin:${gnused}/bin:${gnugrep}/bin:${xidel}/bin:${curl}/bin:${gnupg}/bin
   set -eux
   pushd ${basePath}
diff --git a/pkgs/applications/networking/browsers/firefox/update.nix b/pkgs/applications/networking/browsers/firefox/update.nix
index 07ae2c040e6e..b03720f988a6 100644
--- a/pkgs/applications/networking/browsers/firefox/update.nix
+++ b/pkgs/applications/networking/browsers/firefox/update.nix
@@ -8,13 +8,14 @@
 , gnugrep
 , curl
 , attrPath
+, runtimeShell
 , baseUrl ? "http://archive.mozilla.org/pub/firefox/releases/"
 , versionSuffix ? ""
 , versionKey ? "version"
 }:
 
 writeScript "update-${attrPath}" ''
-  #!${stdenv.shell}
+  #!${runtimeShell}
   PATH=${lib.makeBinPath [ common-updater-scripts coreutils curl gnugrep gnused xidel ]}
 
   url=${baseUrl}
diff --git a/pkgs/applications/networking/instant-messengers/telegram/telegram-cli/default.nix b/pkgs/applications/networking/instant-messengers/telegram/telegram-cli/default.nix
index 3c9157081d3d..2b744c61ccb5 100644
--- a/pkgs/applications/networking/instant-messengers/telegram/telegram-cli/default.nix
+++ b/pkgs/applications/networking/instant-messengers/telegram/telegram-cli/default.nix
@@ -1,5 +1,6 @@
-{ stdenv, fetchgit, bash, libconfig, libevent, openssl,
-  readline, zlib, lua5_2, python, pkgconfig, jansson
+{ stdenv, fetchgit, bash, libconfig, libevent, openssl
+, readline, zlib, lua5_2, python, pkgconfig, jansson
+, runtimeShell
 }:
 
 stdenv.mkDerivation rec {
@@ -20,7 +21,7 @@ stdenv.mkDerivation rec {
     cp ./bin/telegram-cli $out/bin/telegram-wo-key
     cp ./tg-server.pub $out/
     cat > $out/bin/telegram-cli <<EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     $out/bin/telegram-wo-key -k $out/tg-server.pub "\$@"
     EOF
     chmod +x $out/bin/telegram-cli
diff --git a/pkgs/applications/networking/irc/weechat/wrapper.nix b/pkgs/applications/networking/irc/weechat/wrapper.nix
index faf069cebf14..81073222c2b4 100644
--- a/pkgs/applications/networking/irc/weechat/wrapper.nix
+++ b/pkgs/applications/networking/irc/weechat/wrapper.nix
@@ -1,5 +1,5 @@
 { stdenv, lib, runCommand, writeScriptBin, buildEnv
-, pythonPackages, perlPackages
+, pythonPackages, perlPackages, runtimeShell
 }:
 
 weechat:
@@ -60,7 +60,7 @@ let
     in "${scripts};${init}";
 
     mkWeechat = bin: (writeScriptBin bin ''
-      #!${stdenv.shell}
+      #!${runtimeShell}
       export WEECHAT_EXTRA_LIBDIR=${pluginsDir}
       ${lib.concatMapStringsSep "\n" (p: lib.optionalString (p ? extraEnv) p.extraEnv) plugins}
       exec ${weechat}/bin/${bin} "$@" --run-command ${lib.escapeShellArg init}
diff --git a/pkgs/applications/networking/mailreaders/msgviewer/default.nix b/pkgs/applications/networking/mailreaders/msgviewer/default.nix
index 6e2e8d58c676..88e573f6cf42 100644
--- a/pkgs/applications/networking/mailreaders/msgviewer/default.nix
+++ b/pkgs/applications/networking/mailreaders/msgviewer/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, makeWrapper, unzip, jre }:
+{ stdenv, fetchurl, makeWrapper, unzip, jre, runtimeShell }:
 
 let
   version = "1.9";
@@ -20,7 +20,7 @@ in stdenv.mkDerivation rec {
     mv $dir/${uname}/* $dir
     rmdir $dir/${uname}
     cat <<_EOF > $out/bin/msgviewer
-    #!${stdenv.shell} -eu
+    #!${runtimeShell} -eu
     exec ${stdenv.lib.getBin jre}/bin/java -jar $dir/MSGViewer.jar "\$@"
     _EOF
     chmod 755 $out/bin/msgviewer
diff --git a/pkgs/applications/networking/mailreaders/neomutt/default.nix b/pkgs/applications/networking/mailreaders/neomutt/default.nix
index a8c322b42b83..0da451ed51e2 100644
--- a/pkgs/applications/networking/mailreaders/neomutt/default.nix
+++ b/pkgs/applications/networking/mailreaders/neomutt/default.nix
@@ -1,11 +1,11 @@
 { stdenv, fetchFromGitHub, gettext, makeWrapper, tcl, which, writeScript
 , ncurses, perl , cyrus_sasl, gss, gpgme, kerberos, libidn, libxml2, notmuch, openssl
-, lmdb, libxslt, docbook_xsl, docbook_xml_dtd_42, mailcap
+, lmdb, libxslt, docbook_xsl, docbook_xml_dtd_42, mailcap, runtimeShell
 }:
 
 let
   muttWrapper = writeScript "mutt" ''
-    #!${stdenv.shell} -eu
+    #!${runtimeShell} -eu
 
     echo 'The neomutt project has renamed the main binary from `mutt` to `neomutt`.'
     echo ""
diff --git a/pkgs/applications/office/mendeley/default.nix b/pkgs/applications/office/mendeley/default.nix
index 9c4c22dabe97..892ebcee99f9 100644
--- a/pkgs/applications/office/mendeley/default.nix
+++ b/pkgs/applications/office/mendeley/default.nix
@@ -32,6 +32,7 @@
 , autorunLinkHandler ? true
 # Update script
 , writeScript
+, runtimeShell
 }:
 
 let
@@ -130,7 +131,7 @@ stdenv.mkDerivation {
   dontStrip = true;
   dontPatchElf = true;
 
-  updateScript = import ./update.nix { inherit stdenv writeScript; };
+  updateScript = import ./update.nix { inherit stdenv writeScript runtimeShell; };
 
   meta = with stdenv.lib; {
     homepage = http://www.mendeley.com;
diff --git a/pkgs/applications/office/mendeley/update.nix b/pkgs/applications/office/mendeley/update.nix
index 147c95b8e7bd..c174e7dc668d 100644
--- a/pkgs/applications/office/mendeley/update.nix
+++ b/pkgs/applications/office/mendeley/update.nix
@@ -1,7 +1,7 @@
-{ stdenv, writeScript }:
+{ stdenv, writeScript, runtimeShell }:
 
 writeScript "update-mendeley" ''
-  #!${stdenv.shell}
+  #!${runtimeShell}
   function follow() {
     local URL=$1
     while true; do
diff --git a/pkgs/applications/office/moneyplex/default.nix b/pkgs/applications/office/moneyplex/default.nix
index 9f4ed80e6d80..ad621303d7d3 100644
--- a/pkgs/applications/office/moneyplex/default.nix
+++ b/pkgs/applications/office/moneyplex/default.nix
@@ -1,5 +1,6 @@
 { stdenv, fetchurl, patchelf, coreutils, pcsclite
-, zlib, glib, gdk_pixbuf, gtk2, cairo, pango, libX11, atk, openssl }:
+, zlib, glib, gdk_pixbuf, gtk2, cairo, pango, libX11, atk, openssl
+, runtimeShell }:
 
 let
   libPath = stdenv.lib.makeLibraryPath [
@@ -38,7 +39,7 @@ stdenv.mkDerivation rec {
     mkdir "$out/bin"
 
     cat > $out/bin/moneyplex <<EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
 
     if [ -z "\$XDG_DATA_HOME" ]; then
         MDIR=\$HOME/.local/share/moneyplex
diff --git a/pkgs/applications/science/electronics/alliance/default.nix b/pkgs/applications/science/electronics/alliance/default.nix
index 57e1e219b9aa..63bc0911d251 100644
--- a/pkgs/applications/science/electronics/alliance/default.nix
+++ b/pkgs/applications/science/electronics/alliance/default.nix
@@ -1,6 +1,6 @@
 { stdenv, fetchurl
 , xorgproto, motif, libX11, libXt, libXpm, bison
-, flex, automake, autoconf, libtool
+, flex, automake, autoconf, libtool, runtimeShell
 }:
 
 stdenv.mkDerivation rec {
@@ -39,12 +39,12 @@ stdenv.mkDerivation rec {
   '';
 
   allianceInstaller = ''
-    #!${stdenv.shell}
+    #!${runtimeShell}
     cp -v -r -n --no-preserve=mode  $out/etc/* /etc/ > /etc/alliance-install.log
   '';
 
   allianceUnInstaller = ''
-    #!${stdenv.shell}
+    #!${runtimeShell}
     awk '{print \$3}' /etc/alliance-install.log | xargs rm
     awk '{print \$3}' /etc/alliance-install.log | xargs rmdir
     rm /etc/alliance-install.log
diff --git a/pkgs/applications/science/electronics/eagle/eagle7.nix b/pkgs/applications/science/electronics/eagle/eagle7.nix
index d5720440f7cc..69b111562ab2 100644
--- a/pkgs/applications/science/electronics/eagle/eagle7.nix
+++ b/pkgs/applications/science/electronics/eagle/eagle7.nix
@@ -1,6 +1,6 @@
 { stdenv, fetchurl, makeDesktopItem, patchelf, zlib, freetype, fontconfig
 , openssl, libXrender, libXrandr, libXcursor, libX11, libXext, libXi
-, libxcb, cups, xkeyboardconfig
+, libxcb, cups, xkeyboardconfig, runtimeShell
 }:
 
 let
@@ -72,7 +72,7 @@ stdenv.mkDerivation rec {
     dynlinker="$(cat $NIX_CC/nix-support/dynamic-linker)"
     mkdir -p "$out"/bin
     cat > "$out"/bin/eagle << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     export LD_LIBRARY_PATH="${stdenv.cc.cc.lib}/lib:${libPath}"
     export LD_PRELOAD="$out/lib/eagle_fixer.so"
     export QT_XKB_CONFIG_ROOT="${xkeyboardconfig}/share/X11/xkb"
diff --git a/pkgs/applications/science/logic/hol_light/default.nix b/pkgs/applications/science/logic/hol_light/default.nix
index ffd25b6238b7..099e2102c517 100644
--- a/pkgs/applications/science/logic/hol_light/default.nix
+++ b/pkgs/applications/science/logic/hol_light/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, ocaml, num, camlp5 }:
+{ stdenv, runtimeShell, fetchFromGitHub, ocaml, num, camlp5 }:
 
 let
   load_num =
@@ -11,7 +11,7 @@ let
 
   start_script =
     ''
-      #!${stdenv.shell}
+      #!${runtimeShell}
       cd $out/lib/hol_light
       exec ${ocaml}/bin/ocaml \
         -I \`${camlp5}/bin/camlp5 -where\` \
diff --git a/pkgs/applications/science/logic/leo3/binary.nix b/pkgs/applications/science/logic/leo3/binary.nix
index a3834dc70b6d..dcea9c27acb4 100644
--- a/pkgs/applications/science/logic/leo3/binary.nix
+++ b/pkgs/applications/science/logic/leo3/binary.nix
@@ -1,4 +1,4 @@
-{stdenv, fetchurl, openjdk}:
+{stdenv, fetchurl, openjdk, runtimeShell}:
 stdenv.mkDerivation rec {
   pname = "leo3";
   version = "1.2";
@@ -13,7 +13,7 @@ stdenv.mkDerivation rec {
   installPhase = ''
     mkdir -p "$out"/{bin,lib/java/leo3}
     cp "${jar}" "$out/lib/java/leo3/leo3.jar"
-    echo "#!${stdenv.shell}" > "$out/bin/leo3"
+    echo "#!${runtimeShell}" > "$out/bin/leo3"
     echo "'${openjdk}/bin/java' -jar '$out/lib/java/leo3/leo3.jar' \"\$@\""  > "$out/bin/leo3"
     chmod a+x "$out/bin/leo3"
   '';
diff --git a/pkgs/applications/science/math/sage/sage-src.nix b/pkgs/applications/science/math/sage/sage-src.nix
index b9d0a9ef4486..7302b5e337d0 100644
--- a/pkgs/applications/science/math/sage/sage-src.nix
+++ b/pkgs/applications/science/math/sage/sage-src.nix
@@ -1,6 +1,7 @@
 { stdenv
 , fetchFromGitHub
 , fetchpatch
+, runtimeShell
 }:
 
 # This file is responsible for fetching the sage source and adding necessary patches.
@@ -121,7 +122,7 @@ stdenv.mkDerivation rec {
       -e 's/sage-python23/python/g' \
       -i {} \;
 
-    echo '#!${stdenv.shell}
+    echo '#!${runtimeShell}
     python "$@"' > build/bin/sage-python23
 
     # Do not use sage-env-config (generated by ./configure).
diff --git a/pkgs/applications/video/dvd-slideshow/default.nix b/pkgs/applications/video/dvd-slideshow/default.nix
index a329321b70c6..344c47eb934a 100644
--- a/pkgs/applications/video/dvd-slideshow/default.nix
+++ b/pkgs/applications/video/dvd-slideshow/default.nix
@@ -1,10 +1,10 @@
-{ stdenv, lib, fetchurl, writeScript, cdrtools, dvdauthor, ffmpeg, imagemagick, lame, mjpegtools, sox, transcode, vorbis-tools }:
+{ stdenv, lib, fetchurl, writeScript, cdrtools, dvdauthor, ffmpeg, imagemagick, lame, mjpegtools, sox, transcode, vorbis-tools, runtimeShell }:
 
 let
   binPath = lib.makeBinPath [ cdrtools dvdauthor ffmpeg imagemagick lame mjpegtools sox transcode vorbis-tools ];
 
   wrapper = writeScript "dvd-slideshow.sh" ''
-      #!${stdenv.shell}
+      #!${runtimeShell}
       # wrapper script for dvd-slideshow programs
       export PATH=${binPath}:$PATH
 
diff --git a/pkgs/build-support/docker/default.nix b/pkgs/build-support/docker/default.nix
index 4b20c5624607..011ff3685093 100644
--- a/pkgs/build-support/docker/default.nix
+++ b/pkgs/build-support/docker/default.nix
@@ -23,7 +23,8 @@
   writeScript,
   writeText,
   closureInfo,
-  substituteAll
+  substituteAll,
+  runtimeShell
 }:
 
 # WARNING: this API is unstable and may be subject to backwards-incompatible changes in the future.
@@ -119,7 +120,7 @@ rec {
     export PATH=${shadow}/bin:$PATH
     mkdir -p /etc/pam.d
     if [[ ! -f /etc/passwd ]]; then
-      echo "root:x:0:0::/root:${stdenv.shell}" > /etc/passwd
+      echo "root:x:0:0::/root:${runtimeShell}" > /etc/passwd
       echo "root:!x:::::::" > /etc/shadow
     fi
     if [[ ! -f /etc/group ]]; then
@@ -261,7 +262,7 @@ rec {
   # things like `ls` or `echo` will be missing.
   shellScript = name: text:
     writeScript name ''
-      #!${stdenv.shell}
+      #!${runtimeShell}
       set -e
       export PATH=${coreutils}/bin:/bin
       ${text}
diff --git a/pkgs/build-support/release/ant-build.nix b/pkgs/build-support/release/ant-build.nix
index 5ab24132290a..2d24d5bd7041 100644
--- a/pkgs/build-support/release/ant-build.nix
+++ b/pkgs/build-support/release/ant-build.nix
@@ -69,7 +69,7 @@ stdenv.mkDerivation (
 
       mkdir -p $out/bin
       cat >> $out/bin/${w.name} <<EOF
-      #!${stdenv.shell}
+      #!${pkgs.runtimeShell}
       export JAVA_HOME=$jre
       $jre/bin/java ${cp w} ${if w ? mainClass then w.mainClass else "-jar ${w.jar}"} \$@
       EOF
diff --git a/pkgs/build-support/singularity-tools/default.nix b/pkgs/build-support/singularity-tools/default.nix
index 4206b0f33ff9..3fc10c8b0b4b 100644
--- a/pkgs/build-support/singularity-tools/default.nix
+++ b/pkgs/build-support/singularity-tools/default.nix
@@ -8,12 +8,13 @@
 , vmTools
 , gawk
 , utillinux
+, runtimeShell
 , e2fsprogs }:
 
 rec {
   shellScript = name: text:
     writeScript name ''
-      #!${stdenv.shell}
+      #!${runtimeShell}
       set -e
       ${text}
     '';
diff --git a/pkgs/build-support/trivial-builders.nix b/pkgs/build-support/trivial-builders.nix
index 454ef8912b3b..58bc7147ede4 100644
--- a/pkgs/build-support/trivial-builders.nix
+++ b/pkgs/build-support/trivial-builders.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, stdenvNoCC, lndir }:
+{ lib, stdenv, stdenvNoCC, lndir, runtimeShell }:
 
 let
 
@@ -15,12 +15,12 @@ rec {
   /* Run the shell command `buildCommand' to produce a store path named
   * `name'.  The attributes in `env' are added to the environment
   * prior to running the command. By default `runCommand' runs using
-  * stdenv with no compiler environment. `runCommandCC` 
+  * stdenv with no compiler environment. `runCommandCC`
   *
   * Examples:
   * runCommand "name" {envVariable = true;} ''echo hello''
   * runCommandNoCC "name" {envVariable = true;} ''echo hello'' # equivalent to prior
-  * runCommandCC "name" {} ''gcc -o myfile myfile.c; cp myfile $out''; 
+  * runCommandCC "name" {} ''gcc -o myfile myfile.c; cp myfile $out'';
   */
   runCommand = runCommandNoCC;
   runCommandNoCC = runCommand' stdenvNoCC;
@@ -145,11 +145,11 @@ rec {
       executable = true;
       destination = "/bin/${name}";
       text = ''
-        #!${stdenv.shell}
+        #!${runtimeShell}
         ${text}
         '';
       checkPhase = ''
-        ${stdenv.shell} -n $out/bin/${name}
+        ${runtimeShell} -n $out/bin/${name}
       '';
     };
 
@@ -215,7 +215,7 @@ rec {
    * myhellohook = makeSetupHook { deps = [ hello ]; } ./myscript.sh;
    *
    * # wrotes a setup hook where @bash@ myscript.sh is substituted for the
-   * # bash interpreter. 
+   * # bash interpreter.
    * myhellohookSub = makeSetupHook {
    *                 deps = [ hello ];
    *                 substitutions = { bash = "${pkgs.bash}/bin/bash"; };
@@ -278,7 +278,7 @@ rec {
    * packages that cannot be built automatically.
    *
    * Examples:
-   * 
+   *
    * requireFile {
    *   name = "my-file";
    *   url = "http://example.com/download/";
diff --git a/pkgs/build-support/vm/windows/controller/default.nix b/pkgs/build-support/vm/windows/controller/default.nix
index e000308bed8f..08b93aaf1174 100644
--- a/pkgs/build-support/vm/windows/controller/default.nix
+++ b/pkgs/build-support/vm/windows/controller/default.nix
@@ -1,5 +1,6 @@
 { stdenv, writeScript, vmTools, makeInitrd
 , samba, vde2, openssh, socat, netcat-gnu, coreutils, gnugrep, gzip
+, runtimeShell
 }:
 
 { sshKey
@@ -74,7 +75,7 @@ let
   loopForever = "while :; do ${coreutils}/bin/sleep 1; done";
 
   initScript = writeScript "init.sh" (''
-    #!${stdenv.shell}
+    #!${runtimeShell}
     ${coreutils}/bin/cp -L "${sshKey}" /ssh.key
     ${coreutils}/bin/chmod 600 /ssh.key
   '' + (if installMode then ''
diff --git a/pkgs/desktops/pantheon/desktop/elementary-session-settings/default.nix b/pkgs/desktops/pantheon/desktop/elementary-session-settings/default.nix
index e12ed98cb46e..a6ec70a6b584 100644
--- a/pkgs/desktops/pantheon/desktop/elementary-session-settings/default.nix
+++ b/pkgs/desktops/pantheon/desktop/elementary-session-settings/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, substituteAll, writeScript, pantheon, gnome-keyring, gnome-session, wingpanel, orca, at-spi2-core, elementary-default-settings, writeTextFile, writeShellScriptBin, elementary-settings-daemon }:
+{ stdenv, fetchFromGitHub, substituteAll, writeScript, pantheon, gnome-keyring, gnome-session, wingpanel, orca, at-spi2-core, elementary-default-settings, writeTextFile, writeShellScriptBin, elementary-settings-daemon, runtimeShell }:
 
 let
 
@@ -15,7 +15,7 @@ let
   #
 
   dockitems-script = writeScript "dockitems-script" ''
-    #!${stdenv.shell}
+    #!${runtimeShell}
 
     elementary_default_settings="${elementary-default-settings}"
     dock_items="$elementary_default_settings/share/elementary/config/plank/dock1/launchers"/*
diff --git a/pkgs/development/compilers/ccl/default.nix b/pkgs/development/compilers/ccl/default.nix
index ae6af8dd4b4f..646963eedbca 100644
--- a/pkgs/development/compilers/ccl/default.nix
+++ b/pkgs/development/compilers/ccl/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, bootstrap_cmds, coreutils, glibc, m4 }:
+{ stdenv, fetchurl, bootstrap_cmds, coreutils, glibc, m4, runtimeShell }:
 
 let
   options = rec {
@@ -76,7 +76,7 @@ stdenv.mkDerivation rec {
     cp -r .  "$out/share/ccl-installation"
 
     mkdir -p "$out/bin"
-    echo -e '#!${stdenv.shell}\n'"$out/share/ccl-installation/${CCL_RUNTIME}"' "$@"\n' > "$out"/bin/"${CCL_RUNTIME}"
+    echo -e '#!${runtimeShell}\n'"$out/share/ccl-installation/${CCL_RUNTIME}"' "$@"\n' > "$out"/bin/"${CCL_RUNTIME}"
     chmod a+x "$out"/bin/"${CCL_RUNTIME}"
     ln -s "$out"/bin/"${CCL_RUNTIME}" "$out"/bin/ccl
   '';
diff --git a/pkgs/development/compilers/cmdstan/default.nix b/pkgs/development/compilers/cmdstan/default.nix
index 3d9fe3a59cfd..960a54489da4 100644
--- a/pkgs/development/compilers/cmdstan/default.nix
+++ b/pkgs/development/compilers/cmdstan/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, python }:
+{ stdenv, fetchurl, python, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "cmdstan-2.17.1";
@@ -21,7 +21,7 @@ stdenv.mkDerivation rec {
     ln -s $out/opt/cmdstan/bin/stanc $out/bin/stanc
     ln -s $out/opt/cmdstan/bin/stansummary $out/bin/stansummary
     cat > $out/bin/stan <<EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     make -C $out/opt/cmdstan "\$(realpath "\$1")"
     EOF
     chmod a+x $out/bin/stan
diff --git a/pkgs/development/compilers/go/1.11.nix b/pkgs/development/compilers/go/1.11.nix
index 1c9bc0a30091..db3731c2fcaa 100644
--- a/pkgs/development/compilers/go/1.11.nix
+++ b/pkgs/development/compilers/go/1.11.nix
@@ -1,6 +1,6 @@
 { stdenv, fetchFromGitHub, tzdata, iana-etc, go_bootstrap, runCommand, writeScriptBin
 , perl, which, pkgconfig, patch, procps, pcre, cacert, llvm, Security, Foundation
-, mailcap
+, mailcap, runtimeShell
 , buildPackages, targetPackages }:
 
 let
@@ -55,7 +55,7 @@ stdenv.mkDerivation rec {
     # This source produces shell script at run time,
     # and thus it is not corrected by patchShebangs.
     substituteInPlace misc/cgo/testcarchive/carchive_test.go \
-      --replace '#!/usr/bin/env bash' '#!${stdenv.shell}'
+      --replace '#!/usr/bin/env bash' '#!${runtimeShell}'
 
     # Patch the mimetype database location which is missing on NixOS.
     substituteInPlace src/mime/type_unix.go \
@@ -100,7 +100,7 @@ stdenv.mkDerivation rec {
   '' + optionalString stdenv.isLinux ''
     sed -i 's,/usr/share/zoneinfo/,${tzdata}/share/zoneinfo/,' src/time/zoneinfo_unix.go
   '' + optionalString stdenv.isAarch32 ''
-    echo '#!${stdenv.shell}' > misc/cgo/testplugin/test.bash
+    echo '#!${runtimeShell}' > misc/cgo/testplugin/test.bash
   '' + optionalString stdenv.isDarwin ''
     substituteInPlace src/race.bash --replace \
       "sysctl machdep.cpu.extfeatures | grep -qv EM64T" true
diff --git a/pkgs/development/libraries/safefile/default.nix b/pkgs/development/libraries/safefile/default.nix
index d09e45a2d1e9..daa499061c5b 100644
--- a/pkgs/development/libraries/safefile/default.nix
+++ b/pkgs/development/libraries/safefile/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, path }:
+{ stdenv, fetchurl, path, runtimeShell }:
 stdenv.mkDerivation rec {
   name = "${pname}-${version}";
   pname = "safefile";
@@ -13,7 +13,7 @@ stdenv.mkDerivation rec {
 
   passthru = {
     updateScript = ''
-      #!${stdenv.shell}
+      #!${runtimeShell}
       cd ${toString ./.}
       ${toString path}/pkgs/build-support/upstream-updater/update-walker.sh default.nix
     '';
diff --git a/pkgs/development/misc/avr8-burn-omat/default.nix b/pkgs/development/misc/avr8-burn-omat/default.nix
index 4ddab380825d..c88b780d164a 100644
--- a/pkgs/development/misc/avr8-burn-omat/default.nix
+++ b/pkgs/development/misc/avr8-burn-omat/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, unzip }:
+{ stdenv, fetchurl, unzip, runtimeShell }:
 
 stdenv.mkDerivation {
   name = "avr8-burn-omat-2.1.2";
@@ -18,8 +18,8 @@ stdenv.mkDerivation {
     mkdir -p $out/{nix-support,bin}
     mv *.jar license_gpl-3.0.txt lib *.xml *.png $out/nix-support
     cat >> $out/bin/avr8-burn-omat << EOF
-      #!${stdenv.shell}
-      cd $out/nix-support; exec java -jar AVR8_Burn_O_Mat.jar
+    #!${runtimeShell}
+    cd $out/nix-support; exec java -jar AVR8_Burn_O_Mat.jar
     EOF
     chmod +x $out/bin/avr8-burn-omat
   '';
diff --git a/pkgs/development/pharo/vm/build-vm.nix b/pkgs/development/pharo/vm/build-vm.nix
index 76e52692383f..47773c209545 100644
--- a/pkgs/development/pharo/vm/build-vm.nix
+++ b/pkgs/development/pharo/vm/build-vm.nix
@@ -13,6 +13,7 @@
 , libuuid
 , autoreconfHook
 , gcc48
+, runtimeShell
 , ... }:
 
 { name, src, version, source-date, source-url, ... }:
@@ -114,7 +115,7 @@ stdenv.mkDerivation rec {
 
     # Create the script
     cat > "$out/bin/${cmd}" <<EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     set -f
     LD_LIBRARY_PATH="\$LD_LIBRARY_PATH:$libs" exec $out/pharo "\$@"
     EOF
diff --git a/pkgs/development/pharo/vm/vms.nix b/pkgs/development/pharo/vm/vms.nix
index 19065f972a41..0e65a9b990ed 100644
--- a/pkgs/development/pharo/vm/vms.nix
+++ b/pkgs/development/pharo/vm/vms.nix
@@ -17,6 +17,7 @@
 , gcc48
 , fetchFromGitHub
 , makeWrapper
+, runtimeShell
 } @args:
 
 let
@@ -62,4 +63,3 @@ rec {
   };
 
 }
-
diff --git a/pkgs/development/python-modules/cmd2/old.nix b/pkgs/development/python-modules/cmd2/old.nix
index c6ee1eaf8bc0..8f75170c58c4 100644
--- a/pkgs/development/python-modules/cmd2/old.nix
+++ b/pkgs/development/python-modules/cmd2/old.nix
@@ -2,6 +2,7 @@
 , pyperclip, six, pyparsing, vim
 , contextlib2 ? null, subprocess32 ? null
 , pytest, mock, which, fetchFromGitHub, glibcLocales
+, runtimeShell
 }:
 buildPythonPackage rec {
   pname = "cmd2";
@@ -19,8 +20,8 @@ buildPythonPackage rec {
   postPatch = stdenv.lib.optional stdenv.isDarwin ''
     # Fake the impure dependencies pbpaste and pbcopy
     mkdir bin
-    echo '#${stdenv.shell}' > bin/pbpaste
-    echo '#${stdenv.shell}' > bin/pbcopy
+    echo '#${runtimeShell}' > bin/pbpaste
+    echo '#${runtimeShell}' > bin/pbcopy
     chmod +x bin/{pbcopy,pbpaste}
     export PATH=$(realpath bin):$PATH
   '';
diff --git a/pkgs/development/python-modules/pandas/default.nix b/pkgs/development/python-modules/pandas/default.nix
index 2562975e5922..1944e23f4b49 100644
--- a/pkgs/development/python-modules/pandas/default.nix
+++ b/pkgs/development/python-modules/pandas/default.nix
@@ -20,6 +20,7 @@
 , openpyxl
 , tables
 , xlwt
+, runtimeShell
 , libcxx ? null
 }:
 
@@ -97,8 +98,8 @@ in buildPythonPackage rec {
   #       Until then we disable the tests.
   + optionalString isDarwin ''
     # Fake the impure dependencies pbpaste and pbcopy
-    echo "#!${stdenv.shell}" > pbcopy
-    echo "#!${stdenv.shell}" > pbpaste
+    echo "#!${runtimeShell}" > pbcopy
+    echo "#!${runtimeShell}" > pbpaste
     chmod a+x pbcopy pbpaste
     export PATH=$(pwd):$PATH
   '' + ''
diff --git a/pkgs/development/tools/analysis/jdepend/default.nix b/pkgs/development/tools/analysis/jdepend/default.nix
index d2c27b9f4853..939ae10c089d 100644
--- a/pkgs/development/tools/analysis/jdepend/default.nix
+++ b/pkgs/development/tools/analysis/jdepend/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, ant, jdk }:
+{ stdenv, fetchFromGitHub, ant, jdk, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "jdepend-${version}";
@@ -19,7 +19,7 @@ stdenv.mkDerivation rec {
     install dist/${name}.jar $out/share
 
     cat > "$out/bin/jdepend" <<EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     exec ${jdk.jre}/bin/java -classpath "$out/share/*" "\$@"
     EOF
     chmod a+x $out/bin/jdepend
diff --git a/pkgs/development/tools/build-managers/bazel/default.nix b/pkgs/development/tools/build-managers/bazel/default.nix
index 32d35f1421c8..eb9f0d106d10 100644
--- a/pkgs/development/tools/build-managers/bazel/default.nix
+++ b/pkgs/development/tools/build-managers/bazel/default.nix
@@ -6,6 +6,7 @@
 # Allow to independently override the jdks used to build and run respectively
 , buildJdk, runJdk
 , buildJdkName
+, runtimeShell
 # Always assume all markers valid (don't redownload dependencies).
 # Also, don't clean up environment variables.
 , enableNixHacks ? false
@@ -281,7 +282,7 @@ stdenv.mkDerivation rec {
 
     mkdir -p tools
     cat > tools/bazel <<"EOF"
-    #!${stdenv.shell} -e
+    #!${runtimeShell} -e
     exit 1
     EOF
     chmod +x tools/bazel
@@ -290,7 +291,7 @@ stdenv.mkDerivation rec {
     ! hello_test
 
     cat > tools/bazel <<"EOF"
-    #!${stdenv.shell} -e
+    #!${runtimeShell} -e
     exec "$BAZEL_REAL" "$@"
     EOF
 
diff --git a/pkgs/development/tools/build-managers/gup/default.nix b/pkgs/development/tools/build-managers/gup/default.nix
index 7fd801488311..baa658b522a3 100644
--- a/pkgs/development/tools/build-managers/gup/default.nix
+++ b/pkgs/development/tools/build-managers/gup/default.nix
@@ -1,4 +1,5 @@
-{ stdenv, fetchFromGitHub, nix-update-source, lib, python, which, pychecker ? null }:
+{ stdenv, fetchFromGitHub, nix-update-source, lib, python
+, which, runtimeShell, pychecker ? null }:
 stdenv.mkDerivation rec {
   version = "0.7.0";
   src = fetchFromGitHub {
@@ -16,7 +17,7 @@ stdenv.mkDerivation rec {
     cp -r python/bin $out/bin
   '';
   passthru.updateScript = ''
-    #!${stdenv.shell}
+    #!${runtimeShell}
     set -e
     echo
     cd ${toString ./.}
diff --git a/pkgs/development/tools/haskell/hyper-haskell/default.nix b/pkgs/development/tools/haskell/hyper-haskell/default.nix
index a2a049615b78..3b52c262c0c6 100644
--- a/pkgs/development/tools/haskell/hyper-haskell/default.nix
+++ b/pkgs/development/tools/haskell/hyper-haskell/default.nix
@@ -1,4 +1,5 @@
-{ stdenv, fetchFromGitHub, jshon, electron, hyper-haskell-server, extra-packages ? [] }:
+{ stdenv, fetchFromGitHub, jshon, electron
+, runtimeShell, hyper-haskell-server, extra-packages ? [] }:
 
 let
   binPath = stdenv.lib.makeBinPath ([ hyper-haskell-server ] ++ extra-packages);
@@ -34,7 +35,7 @@ in stdenv.mkDerivation rec {
 
     # install electron wrapper script
     cat > $out/bin/hyper-haskell <<EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     export PATH="${binPath}:\$PATH"
     exec ${electron}/bin/electron $out/app "\$@"
     EOF
diff --git a/pkgs/development/tools/misc/ccls/default.nix b/pkgs/development/tools/misc/ccls/default.nix
index 11e02eba5663..c8ab6b0fd963 100644
--- a/pkgs/development/tools/misc/ccls/default.nix
+++ b/pkgs/development/tools/misc/ccls/default.nix
@@ -1,5 +1,5 @@
 { stdenv, fetchFromGitHub, makeWrapper
-, cmake, llvmPackages, rapidjson }:
+, cmake, llvmPackages, rapidjson, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name    = "ccls-${version}";
@@ -17,7 +17,7 @@ stdenv.mkDerivation rec {
 
   cmakeFlags = [ "-DSYSTEM_CLANG=ON" ];
 
-  shell = stdenv.shell;
+  shell = runtimeShell;
   postFixup = ''
     # We need to tell ccls where to find the standard library headers.
 
diff --git a/pkgs/development/tools/misc/cquery/default.nix b/pkgs/development/tools/misc/cquery/default.nix
index f032a85173a5..54ad7ca7071c 100644
--- a/pkgs/development/tools/misc/cquery/default.nix
+++ b/pkgs/development/tools/misc/cquery/default.nix
@@ -1,5 +1,6 @@
 { fetchFromGitHub, makeWrapper
-, cmake, llvmPackages, ncurses }:
+, cmake, llvmPackages, ncurses
+, runtimeShell }:
 
 let
   src = fetchFromGitHub {
@@ -28,7 +29,7 @@ stdenv.mkDerivation rec {
     "-DCMAKE_OSX_DEPLOYMENT_TARGET=10.12"
   ];
 
-  shell = stdenv.shell;
+  shell = runtimeShell;
   postFixup = ''
     # We need to tell cquery where to find the standard library headers.
 
diff --git a/pkgs/development/tools/misc/ctags/wrapped.nix b/pkgs/development/tools/misc/ctags/wrapped.nix
index ed265b4489b3..869e7a61121d 100644
--- a/pkgs/development/tools/misc/ctags/wrapped.nix
+++ b/pkgs/development/tools/misc/ctags/wrapped.nix
@@ -14,7 +14,7 @@ with pkgs.stdenv.lib;
 
   # the derivation. use language extensions specified by args
   ctagsWrapped = makeOverridable ( {args, name} :  pkgs.writeScriptBin name ''
-  #!${stdenv.shell}
+  #!${pkgs.runtimeShell}
   exec ${pkgs.ctags}/bin/ctags ${concatStringsSep " " (map escapeShellArg args)} "$@"
   '') {
     args = let x = pkgs.ctagsWrapped; in concatLists [
diff --git a/pkgs/development/tools/misc/distcc/default.nix b/pkgs/development/tools/misc/distcc/default.nix
index e80a37d37dd5..9891726c6633 100644
--- a/pkgs/development/tools/misc/distcc/default.nix
+++ b/pkgs/development/tools/misc/distcc/default.nix
@@ -1,5 +1,6 @@
 { stdenv, fetchFromGitHub, popt, avahi, pkgconfig, python, gtk2, runCommand
 , gcc, autoconf, automake, which, procps, libiberty_static
+, runtimeShell
 , sysconfDir ? ""   # set this parameter to override the default value $out/etc
 , static ? false
 }:
@@ -53,7 +54,7 @@ let
           mkdir -p $out/bin
           if [ -x "${gcc.cc}/bin/gcc" ]; then
             cat > $out/bin/gcc << EOF
-            #!${stdenv.shell}
+            #!${runtimeShell}
             ${extraConfig}
             exec ${distcc}/bin/distcc gcc "\$@"
           EOF
@@ -61,7 +62,7 @@ let
           fi
           if [ -x "${gcc.cc}/bin/g++" ]; then
             cat > $out/bin/g++ << EOF
-            #!${stdenv.shell}
+            #!${runtimeShell}
             ${extraConfig}
             exec ${distcc}/bin/distcc g++ "\$@"
           EOF
diff --git a/pkgs/development/tools/misc/opengrok/default.nix b/pkgs/development/tools/misc/opengrok/default.nix
index f7b77cdc418d..874aa04157dc 100644
--- a/pkgs/development/tools/misc/opengrok/default.nix
+++ b/pkgs/development/tools/misc/opengrok/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, jre, ctags, makeWrapper, coreutils, git }:
+{ stdenv, fetchurl, jre, ctags, makeWrapper, coreutils, git, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "opengrok-${version}";
@@ -16,7 +16,7 @@ stdenv.mkDerivation rec {
     mkdir -p $out
     cp -a * $out/
     substituteInPlace $out/bin/OpenGrok --replace "/bin/uname" "${coreutils}/bin/uname"
-    substituteInPlace $out/bin/Messages --replace "#!/bin/ksh" "#!${stdenv.shell}"
+    substituteInPlace $out/bin/Messages --replace "#!/bin/ksh" "#!${runtimeShell}"
     wrapProgram $out/bin/OpenGrok \
       --prefix PATH : "${stdenv.lib.makeBinPath [ ctags git ]}" \
       --set JAVA_HOME "${jre}" \
diff --git a/pkgs/development/tools/misc/saleae-logic/default.nix b/pkgs/development/tools/misc/saleae-logic/default.nix
index c89492287db4..18ffc238f0d1 100644
--- a/pkgs/development/tools/misc/saleae-logic/default.nix
+++ b/pkgs/development/tools/misc/saleae-logic/default.nix
@@ -9,7 +9,7 @@
 { stdenv, fetchurl, unzip, glib, libSM, libICE, gtk2, libXext, libXft
 , fontconfig, libXrender, libXfixes, libX11, libXi, libXrandr, libXcursor
 , freetype, libXinerama, libxcb, zlib, pciutils
-, makeDesktopItem, xkeyboardconfig
+, makeDesktopItem, xkeyboardconfig, runtimeShell
 }:
 
 let
@@ -70,7 +70,7 @@ stdenv.mkDerivation rec {
     # Make wrapper script that uses the LD_PRELOAD library
     mkdir -p "$out/bin"
     cat > "$out/bin/saleae-logic" << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     export LD_PRELOAD="$out/lib/preload.so"
     export QT_XKB_CONFIG_ROOT="${xkeyboardconfig}/share/X11/xkb"
     export PATH="${pciutils}/bin:\$PATH"
diff --git a/pkgs/development/tools/rust/bindgen/default.nix b/pkgs/development/tools/rust/bindgen/default.nix
index 5a1c2364a777..a1f5e5769ae8 100644
--- a/pkgs/development/tools/rust/bindgen/default.nix
+++ b/pkgs/development/tools/rust/bindgen/default.nix
@@ -1,4 +1,5 @@
-{ stdenv, fetchFromGitHub, rustPlatform, clang, llvmPackages, rustfmt, writeScriptBin }:
+{ stdenv, fetchFromGitHub, rustPlatform, clang, llvmPackages, rustfmt, writeScriptBin,
+  runtimeShell }:
 
 rustPlatform.buildRustPackage rec {
   name = "rust-bindgen-${version}";
@@ -32,7 +33,7 @@ rustPlatform.buildRustPackage rec {
   doCheck = true;
   checkInputs =
     let fakeRustup = writeScriptBin "rustup" ''
-      #!${stdenv.shell}
+      #!${runtimeShell}
       shift
       shift
       exec "$@"
diff --git a/pkgs/development/tools/wp-cli/default.nix b/pkgs/development/tools/wp-cli/default.nix
index e2250297c8eb..211f71693976 100644
--- a/pkgs/development/tools/wp-cli/default.nix
+++ b/pkgs/development/tools/wp-cli/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, lib, fetchurl, writeScript, writeText, php }:
+{ stdenv, lib, fetchurl, writeScript, writeText, php, runtimeShell }:
 
 let
   version = "2.0.1";
@@ -22,7 +22,7 @@ in stdenv.mkDerivation rec {
     mkdir -p $out/bin $dir
 
     cat <<_EOF > $out/bin/wp
-#!${stdenv.shell}
+#!${runtimeShell}
 
 set -euo pipefail
 
diff --git a/pkgs/development/tools/xcbuild/wrapper.nix b/pkgs/development/tools/xcbuild/wrapper.nix
index b2455095a01a..959c79405e86 100644
--- a/pkgs/development/tools/xcbuild/wrapper.nix
+++ b/pkgs/development/tools/xcbuild/wrapper.nix
@@ -1,5 +1,6 @@
 { stdenv, lib, buildPackages, makeWrapper, writeText, runCommand
 , CoreServices, ImageIO, CoreGraphics
+, runtimeShell
 , xcodePlatform ? stdenv.targetPlatform.xcodePlatform or "MacOSX"
 , xcodeVer ? stdenv.targetPlatform.xcodeVer or "9.4.1"
 , sdkVer ? stdenv.targetPlatform.sdkVer or "10.10" }:
@@ -35,7 +36,7 @@ let
   '';
 
   xcode-select = writeText "xcode-select" ''
-#!${stdenv.shell}
+#!${runtimeShell}
 while [ $# -gt 0 ]; do
    case "$1" in
          -h | --help) ;; # noop
@@ -50,7 +51,7 @@ done
   '';
 
   xcrun = writeText "xcrun" ''
-#!${stdenv.shell}
+#!${runtimeShell}
 while [ $# -gt 0 ]; do
    case "$1" in
          --sdk | -sdk) shift ;;
diff --git a/pkgs/development/web/nodejs/update.nix b/pkgs/development/web/nodejs/update.nix
index bf6951dc6882..7b5a4710aa09 100644
--- a/pkgs/development/web/nodejs/update.nix
+++ b/pkgs/development/web/nodejs/update.nix
@@ -9,10 +9,11 @@
 , common-updater-scripts
 , majorVersion
 , nix
+, runtimeShell
 }:
 
 writeScript "update-nodejs" ''
-  #!${stdenv.shell}
+  #!${runtimeShell}
   PATH=${lib.makeBinPath [ common-updater-scripts coreutils curl gnugrep jq gnupg nix ]}
 
   HOME=`mktemp -d`
diff --git a/pkgs/games/andyetitmoves/default.nix b/pkgs/games/andyetitmoves/default.nix
index 123d4296c973..d4ff86c424b1 100644
--- a/pkgs/games/andyetitmoves/default.nix
+++ b/pkgs/games/andyetitmoves/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, libvorbis, libogg, libtheora, SDL, libXft, SDL_image, zlib, libX11, libpng, openal, requireFile, commercialVersion ? false }:
+{ stdenv, fetchurl, libvorbis, libogg, libtheora, SDL, libXft, SDL_image, zlib, libX11, libpng, openal, runtimeShell, requireFile, commercialVersion ? false }:
 
 let plainName = "andyetitmoves";
     version   = "1.2.2";
@@ -50,7 +50,7 @@ stdenv.mkDerivation rec {
 
     patchelf --set-interpreter $(cat $NIX_CC/nix-support/dynamic-linker) --set-rpath $fullPath $out/opt/andyetitmoves/lib/$binName
     cat > $out/bin/$binName << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     cd $out/opt/andyetitmoves
     exec ./lib/$binName
     EOF
diff --git a/pkgs/games/astromenace/default.nix b/pkgs/games/astromenace/default.nix
index 91959a1c942b..b2c86222f2b0 100644
--- a/pkgs/games/astromenace/default.nix
+++ b/pkgs/games/astromenace/default.nix
@@ -1,4 +1,4 @@
-{ fetchurl, stdenv, cmake, xlibsWrapper, libGLU_combined, SDL, openal, freealut, libogg, libvorbis }:
+{ fetchurl, stdenv, cmake, xlibsWrapper, libGLU_combined, SDL, openal, freealut, libogg, libvorbis, runtimeShell }:
 
 stdenv.mkDerivation rec {
   version = "1.3.2";
@@ -21,7 +21,7 @@ stdenv.mkDerivation rec {
     cp AstroMenace $out
     cp gamedata.vfs $out
     cat > $out/bin/AstroMenace << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     $out/AstroMenace --dir=$out
     EOF
     chmod 755 $out/bin/AstroMenace
diff --git a/pkgs/games/cataclysm-dda/common.nix b/pkgs/games/cataclysm-dda/common.nix
index 2c55911defcb..b39eb987e00d 100644
--- a/pkgs/games/cataclysm-dda/common.nix
+++ b/pkgs/games/cataclysm-dda/common.nix
@@ -1,6 +1,6 @@
 { stdenv, fetchFromGitHub, pkgconfig, gettext, lua, ncurses
 , tiles, SDL2, SDL2_image, SDL2_mixer, SDL2_ttf, freetype, Cocoa
-, debug
+, debug, runtimeShell
 }:
 
 let
@@ -86,7 +86,7 @@ let
       mkdir $app/Contents/MacOS
       launcher=$app/Contents/MacOS/Cataclysm.sh
       cat << EOF > $launcher
-      #!${stdenv.shell}
+      #!${runtimeShell}
       $out/bin/cataclysm-tiles
       EOF
       chmod 555 $launcher
diff --git a/pkgs/games/eduke32/default.nix b/pkgs/games/eduke32/default.nix
index 859e503e1017..c51f014b179d 100644
--- a/pkgs/games/eduke32/default.nix
+++ b/pkgs/games/eduke32/default.nix
@@ -1,5 +1,5 @@
 { stdenv, fetchurl, flac, gtk2, libvorbis, libvpx, makeDesktopItem, libGLU_combined, nasm
-, pkgconfig, SDL2, SDL2_mixer }:
+, pkgconfig, SDL2, SDL2_mixer, runtimeShell }:
 
 let
   year = "2015";
@@ -45,7 +45,7 @@ in stdenv.mkDerivation rec {
   installPhase = ''
     # Make wrapper script
     cat > eduke32-wrapper <<EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
 
     if [ "$EDUKE32_DATA_DIR" = "" ]; then
         EDUKE32_DATA_DIR=/var/lib/games/eduke32
diff --git a/pkgs/games/fsg/default.nix b/pkgs/games/fsg/default.nix
index 5c8d2541a01c..d334439cafb8 100644
--- a/pkgs/games/fsg/default.nix
+++ b/pkgs/games/fsg/default.nix
@@ -1,4 +1,5 @@
-{ stdenv, fetchurl, gtk2, glib, pkgconfig, libGLU_combined, wxGTK, libX11, xorgproto }:
+{ stdenv, fetchurl, gtk2, glib, pkgconfig, libGLU_combined, wxGTK, libX11, xorgproto
+, runtimeShell }:
 
 stdenv.mkDerivation {
   name = "fsg-4.4";
@@ -24,7 +25,7 @@ stdenv.mkDerivation {
   installPhase = ''
     mkdir -p $out/bin $out/libexec
     cp sand $out/libexec
-    echo -e '#!${stdenv.shell}\nLC_ALL=C '$out'/libexec/sand "$@"' >$out/bin/fsg
+    echo -e '#!${runtimeShell}\nLC_ALL=C '$out'/libexec/sand "$@"' >$out/bin/fsg
     chmod a+x $out/bin/fsg
   '';
 
diff --git a/pkgs/games/oilrush/default.nix b/pkgs/games/oilrush/default.nix
index 2d636064205b..1d56d86f5301 100644
--- a/pkgs/games/oilrush/default.nix
+++ b/pkgs/games/oilrush/default.nix
@@ -1,11 +1,11 @@
 { stdenv, config, fetchurl, libX11, libXext, libXinerama, libXrandr
-, libXrender, fontconfig, freetype, openal }:
+, libXrender, fontconfig, freetype, openal, runtimeShell }:
 
 let inherit (stdenv.lib) makeLibraryPath; in
 
 stdenv.mkDerivation {
   name = "oilrush";
-  src = 
+  src =
   let
     url = config.oilrush.url or null;
     sha256 = config.oilrush.sha256 or null;
@@ -54,7 +54,7 @@ stdenv.mkDerivation {
     cp -r * "$out/opt/oilrush"
     mkdir -p "$out/bin"
     cat << EOF > "$out/bin/oilrush"
-    #!${stdenv.shell}
+    #!${runtimeShell}
     LD_LIBRARY_PATH=.:${makeLibraryPath [ openal ]}:\$LD_LIBRARY_PATH
     cd "$out/opt/oilrush"
     exec ./launcher_$arch.sh "\$@"
@@ -66,7 +66,7 @@ stdenv.mkDerivation {
     longDescription = ''
       Oil Rush is a real-time naval strategy game based on group control. It
       combines the strategic challenge of a classical RTS with the sheer fun
-      of Tower Defense. 
+      of Tower Defense.
     '';
     homepage = http://oilrush-game.com/;
     license = stdenv.lib.licenses.unfree;
diff --git a/pkgs/games/openttd/default.nix b/pkgs/games/openttd/default.nix
index e5243aef2d3e..237021e98010 100644
--- a/pkgs/games/openttd/default.nix
+++ b/pkgs/games/openttd/default.nix
@@ -1,7 +1,7 @@
 { stdenv, fetchurl, fetchzip, pkgconfig, SDL, libpng, zlib, xz, freetype, fontconfig
 , withOpenGFX ? true, withOpenSFX ? true, withOpenMSX ? true
 , withFluidSynth ? true, audioDriver ? "alsa", fluidsynth, soundfont-fluid, procps
-, writeScriptBin, makeWrapper
+, writeScriptBin, makeWrapper, runtimeShell
 }:
 
 let
@@ -21,7 +21,7 @@ let
   };
 
   playmidi = writeScriptBin "playmidi" ''
-    #!${stdenv.shell}
+    #!${runtimeShell}
     trap "${procps}/bin/pkill fluidsynth" EXIT
     ${fluidsynth}/bin/fluidsynth -a ${audioDriver} -i ${soundfont-fluid}/share/soundfonts/FluidR3_GM2-2.sf2 $*
   '';
diff --git a/pkgs/games/sauerbraten/default.nix b/pkgs/games/sauerbraten/default.nix
index f48736a56d60..3a9635f67124 100644
--- a/pkgs/games/sauerbraten/default.nix
+++ b/pkgs/games/sauerbraten/default.nix
@@ -1,5 +1,5 @@
 { stdenv, fetchsvn, libGLU_combined, SDL, SDL_image, SDL_mixer
-, libpng, zlib, libjpeg, imagemagick, libX11
+, libpng, zlib, libjpeg, imagemagick, libX11, runtimeShell
 }:
 
 stdenv.mkDerivation rec {
@@ -30,12 +30,12 @@ stdenv.mkDerivation rec {
     cp -rv packages $out/share/sauerbraten/
     cp -rv data $out/share/sauerbraten/
     cat > $out/bin/sauerbraten_server <<EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     cd $out/share/sauerbraten
     ./sauer_server "\$@"
     EOF
     cat > $out/bin/sauerbraten_client <<EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     cd $out/share/sauerbraten
     ./sauer_client "\$@"
     EOF
diff --git a/pkgs/games/scummvm/games.nix b/pkgs/games/scummvm/games.nix
index 1ccb351af4ab..a96250e7acb6 100644
--- a/pkgs/games/scummvm/games.nix
+++ b/pkgs/games/scummvm/games.nix
@@ -1,5 +1,5 @@
 { stdenv, lib, fetchurl, makeDesktopItem, unzip, writeText
-, scummvm }:
+, scummvm, runtimeShell }:
 
 let
   desktopItem = name: short: long: description: makeDesktopItem {
@@ -13,7 +13,7 @@ let
   };
 
   run = name: short: code: writeText "${short}.sh" ''
-    #!${stdenv.shell} -eu
+    #!${runtimeShell} -eu
 
     exec ${scummvm}/bin/scummvm \
       --path=@out@/share/${name} \
diff --git a/pkgs/games/simutrans/default.nix b/pkgs/games/simutrans/default.nix
index ccece066cdc2..a97338f6f204 100644
--- a/pkgs/games/simutrans/default.nix
+++ b/pkgs/games/simutrans/default.nix
@@ -1,5 +1,5 @@
 { stdenv, fetchurl, pkgconfig, unzip, zlib, libpng, bzip2, SDL, SDL_mixer
-, buildEnv, config
+, buildEnv, config, runtimeShell
 }:
 
 let
@@ -95,7 +95,7 @@ let
     postBuild = ''
       rm "$out/bin" && mkdir "$out/bin"
       cat > "$out/bin/simutrans" <<EOF
-      #!${stdenv.shell}
+      #!${runtimeShell}
       cd "$out"/share/simutrans
       exec "${binaries}/bin/simutrans" -use_workdir "\''${extraFlagsArray[@]}" "\$@"
       EOF
@@ -167,4 +167,3 @@ let
   };
 
 in result
-
diff --git a/pkgs/games/speed-dreams/default.nix b/pkgs/games/speed-dreams/default.nix
index ad0c7441d7f4..2556aa49b4c1 100644
--- a/pkgs/games/speed-dreams/default.nix
+++ b/pkgs/games/speed-dreams/default.nix
@@ -1,6 +1,6 @@
 { fetchurl, stdenv, libGLU_combined, freeglut, libX11, plib, openal, freealut, libXrandr, xorgproto,
 libXext, libSM, libICE, libXi, libXt, libXrender, libXxf86vm, openscenegraph, expat,
-libpng, zlib, bash, SDL2, enet, libjpeg, cmake, pkgconfig, libvorbis}:
+libpng, zlib, bash, SDL2, enet, libjpeg, cmake, pkgconfig, libvorbis, runtimeShell }:
 
 stdenv.mkDerivation rec {
   version = "2.2.1-r6404";
@@ -52,7 +52,7 @@ stdenv.mkDerivation rec {
   postInstall = ''
     mkdir "$out/bin"
     for i in "$out"/games/*; do
-      echo '#!${stdenv.shell}' >> "$out/bin/$(basename "$i")"
+      echo '#!${runtimeShell}' >> "$out/bin/$(basename "$i")"
       echo "$i"' "$@"' >> "$out/bin/$(basename "$i")"
       chmod a+x "$out/bin/$(basename "$i")"
     done
diff --git a/pkgs/games/steam/chrootenv.nix b/pkgs/games/steam/chrootenv.nix
index 17f25e25e3f7..4661cf7d8035 100644
--- a/pkgs/games/steam/chrootenv.nix
+++ b/pkgs/games/steam/chrootenv.nix
@@ -4,6 +4,7 @@
 , extraProfile ? "" # string to append to profile
 , nativeOnly ? false
 , runtimeOnly ? false
+, runtimeShell
 
 # DEPRECATED
 , withJava ? config.steam.java or false
@@ -35,7 +36,7 @@ let
            ++ lib.optionals (steam-runtime-wrapped-i686 != null) (map (x: "/steamrt/${steam-runtime-wrapped-i686.arch}/" + x) steam-runtime-wrapped-i686.libs);
 
   runSh = writeScript "run.sh" ''
-    #!${stdenv.shell}
+    #!${runtimeShell}
     runtime_paths="${lib.concatStringsSep ":" ldPath}"
     if [ "$1" == "--print-steam-runtime-library-paths" ]; then
       echo "$runtime_paths"
@@ -195,7 +196,7 @@ in buildFHSUserEnv rec {
   '' + extraProfile;
 
   runScript = writeScript "steam-wrapper.sh" ''
-    #!${stdenv.shell}
+    #!${runtimeShell}
     if [ -f /host/etc/NIXOS ]; then   # Check only useful on NixOS
       ${glxinfo-i686}/bin/glxinfo >/dev/null 2>&1
       # If there was an error running glxinfo, we know something is wrong with the configuration
@@ -204,7 +205,7 @@ in buildFHSUserEnv rec {
     **
     WARNING: Steam is not set up. Add the following options to /etc/nixos/configuration.nix
     and then run \`sudo nixos-rebuild switch\`:
-    { 
+    {
       hardware.opengl.driSupport32Bit = true;
       hardware.pulseaudio.support32Bit = true;
     }
@@ -226,7 +227,7 @@ in buildFHSUserEnv rec {
     inherit multiPkgs extraBuildCommands;
 
     runScript = writeScript "steam-run" ''
-      #!${stdenv.shell}
+      #!${runtimeShell}
       run="$1"
       if [ "$run" = "" ]; then
         echo "Usage: steam-run command-to-run args..." >&2
diff --git a/pkgs/games/steam/steam.nix b/pkgs/games/steam/steam.nix
index dd6e9a070b19..95493498f5e7 100644
--- a/pkgs/games/steam/steam.nix
+++ b/pkgs/games/steam/steam.nix
@@ -1,4 +1,4 @@
-{stdenv, fetchurl, traceDeps ? false}:
+{stdenv, fetchurl, runtimeShell, traceDeps ? false}:
 
 let
   traceLog = "/tmp/steam-trace-dependencies.log";
@@ -18,7 +18,7 @@ in stdenv.mkDerivation rec {
     rm $out/bin/steamdeps
     ${stdenv.lib.optionalString traceDeps ''
       cat > $out/bin/steamdeps <<EOF
-      #!${stdenv.shell}
+      #!${runtimeShell}
       echo \$1 >> ${traceLog}
       cat \$1 >> ${traceLog}
       echo >> ${traceLog}
diff --git a/pkgs/games/tcl2048/default.nix b/pkgs/games/tcl2048/default.nix
index 48fa0a3b5181..57e7217fcdbd 100644
--- a/pkgs/games/tcl2048/default.nix
+++ b/pkgs/games/tcl2048/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, tcl, tcllib }:
+{ stdenv, fetchurl, tcl, tcllib, runtimeShell }:
 
 stdenv.mkDerivation {
   name = "tcl2048-0.4.0";
@@ -14,7 +14,7 @@ stdenv.mkDerivation {
     mkdir -pv $out/bin
     cp $src $out/2048.tcl
     cat > $out/bin/2048 << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
 
     # wrapper for tcl2048
     export TCLLIBPATH="${tcllib}/lib/tcllib${tcllib.version}"
diff --git a/pkgs/games/tibia/default.nix b/pkgs/games/tibia/default.nix
index cfef1ed0db2b..02c55d7d06c9 100644
--- a/pkgs/games/tibia/default.nix
+++ b/pkgs/games/tibia/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, glibc, libX11, libGLU_combined }:
+{ stdenv, fetchurl, glibc, libX11, runtimeShell, libGLU_combined }:
 
 with stdenv.lib;
 stdenv.mkDerivation {
@@ -38,7 +38,7 @@ stdenv.mkDerivation {
 
     # The wrapper script itself. We use $LD_LIBRARY_PATH for libGL.
     cat << EOF > "$out/bin/Tibia"
-    #!${stdenv.shell}
+    #!${runtimeShell}
     cd $out/res
     ${glibc.out}/lib/ld-linux.so.2 --library-path \$LD_LIBRARY_PATH ./Tibia "\$@"
     EOF
diff --git a/pkgs/games/tremulous/default.nix b/pkgs/games/tremulous/default.nix
index fd1a3e178d59..470856af90e6 100644
--- a/pkgs/games/tremulous/default.nix
+++ b/pkgs/games/tremulous/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, unzip, libGLU_combined, libX11, SDL, openal }:
+{ stdenv, fetchurl, unzip, libGLU_combined, libX11, SDL, openal, runtimeShell }:
 stdenv.mkDerivation rec {
   name = "tremulous-${version}";
   version = "1.1.0";
@@ -49,7 +49,7 @@ stdenv.mkDerivation rec {
     for b in tremulous tremded
     do
         cat << EOF > $out/bin/$b
-    #!${stdenv.shell}
+    #!${runtimeShell}
     cd $out/opt/tremulous
     exec ./$b.$arch "\$@"
     EOF
diff --git a/pkgs/games/trigger/default.nix b/pkgs/games/trigger/default.nix
index 85157d8df189..ae16510fdc53 100644
--- a/pkgs/games/trigger/default.nix
+++ b/pkgs/games/trigger/default.nix
@@ -1,4 +1,5 @@
-{ fetchurl, stdenv, SDL2, freealut, SDL2_image, openal, physfs, zlib, libGLU_combined, glew }:
+{ fetchurl, stdenv, runtimeShell
+, SDL2, freealut, SDL2_image, openal, physfs, zlib, libGLU_combined, glew }:
 
 stdenv.mkDerivation rec {
   name = "trigger-rally-0.6.5";
@@ -23,7 +24,7 @@ stdenv.mkDerivation rec {
   postInstall = ''
     mkdir -p $out/bin
     cat <<EOF > $out/bin/trigger-rally
-    #!${stdenv.shell}
+    #!${runtimeShell}
     exec $out/games/trigger-rally "$@"
     EOF
     chmod +x $out/bin/trigger-rally
diff --git a/pkgs/games/vessel/default.nix b/pkgs/games/vessel/default.nix
index 83fce64ca329..483912bab45e 100644
--- a/pkgs/games/vessel/default.nix
+++ b/pkgs/games/vessel/default.nix
@@ -1,14 +1,14 @@
-{ stdenv, requireFile, SDL, libpulseaudio, alsaLib }:
+{ stdenv, requireFile, SDL, libpulseaudio, alsaLib, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "vessel-12082012";
 
-  goBuyItNow = '' 
+  goBuyItNow = ''
     We cannot download the full version automatically, as you require a license.
     Once you bought a license, you need to add your downloaded version to the nix store.
     You can do this by using "nix-prefetch-url file://\$PWD/${name}-bin" in the
     directory where you saved it.
-  ''; 
+  '';
 
   src = if (stdenv.isi686) then
     requireFile {
@@ -20,7 +20,7 @@ stdenv.mkDerivation rec {
   phases = "installPhase";
   ld_preload = ./isatty.c;
 
-  libPath = stdenv.lib.makeLibraryPath [ stdenv.cc.cc stdenv.cc.libc ] 
+  libPath = stdenv.lib.makeLibraryPath [ stdenv.cc.cc stdenv.cc.libc ]
     + ":" + stdenv.lib.makeLibraryPath [ SDL libpulseaudio alsaLib ] ;
 
   installPhase = ''
@@ -30,11 +30,11 @@ stdenv.mkDerivation rec {
     # allow scripting of the mojoinstaller
     gcc -fPIC -shared -o isatty.so $ld_preload
 
-    echo @@@ 
+    echo @@@
     echo @@@ this next step appears to hang for a while
-    echo @@@ 
+    echo @@@
 
-    # if we call ld.so $(bin) we don't need to set the ELF interpreter, and save a patchelf step. 
+    # if we call ld.so $(bin) we don't need to set the ELF interpreter, and save a patchelf step.
     LD_PRELOAD=./isatty.so $(cat $NIX_CC/nix-support/dynamic-linker) $src << IM_A_BOT
     n
     $out/libexec/strangeloop/vessel/
@@ -45,7 +45,7 @@ stdenv.mkDerivation rec {
     rm $out/libexec/strangeloop/vessel/x86/libstdc++*
 
     # props to Ethan Lee (the Vessel porter) for understanding
-    # how $ORIGIN works in rpath. There is hope for humanity. 
+    # how $ORIGIN works in rpath. There is hope for humanity.
     patchelf \
       --interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" \
       --set-rpath $libPath:$out/libexec/strangeloop/vessel/x86/ \
@@ -59,7 +59,7 @@ stdenv.mkDerivation rec {
     done
 
     cat > $out/bin/Vessel << EOW
-    #!${stdenv.shell}
+    #!${runtimeShell}
     cd $out/libexec/strangeloop/vessel/
     exec ./x86/vessel.x86
     EOW
diff --git a/pkgs/games/worldofgoo/default.nix b/pkgs/games/worldofgoo/default.nix
index 97bfd9e730c7..7d8380e9f7ce 100644
--- a/pkgs/games/worldofgoo/default.nix
+++ b/pkgs/games/worldofgoo/default.nix
@@ -1,25 +1,26 @@
 { stdenv, requireFile
 , libX11, libXext, libXau, libxcb, libXdmcp , SDL, SDL_mixer, libvorbis, libGLU_combined
+, runtimeShell
 , demo ? false }:
 
 # TODO: add i686 support
 
 stdenv.mkDerivation rec {
-  name = if demo 
+  name = if demo
     then "WorldOfGooDemo-1.41"
     else "WorldofGoo-1.41";
 
   arch = if stdenv.hostPlatform.system == "x86_64-linux" then "supported"
     else throw "Sorry. World of Goo only is only supported on x86_64 now.";
 
-  goBuyItNow = '' 
+  goBuyItNow = ''
     We cannot download the full version automatically, as you require a license.
     Once you bought a license, you need to add your downloaded version to the nix store.
     You can do this by using "nix-prefetch-url file://\$PWD/WorldOfGooSetup.1.41.tar.gz" in the
     directory where you saved it.
 
-    Or you can install the demo version: 'nix-env -i -A pkgs.worldofgoo_demo'. 
-  ''; 
+    Or you can install the demo version: 'nix-env -i -A pkgs.worldofgoo_demo'.
+  '';
 
   getTheDemo = ''
     We cannot download the demo version automatically. Please go to
@@ -28,8 +29,8 @@ stdenv.mkDerivation rec {
     directory where you saved it.
   '';
 
-  src = if demo 
-    then 
+  src = if demo
+    then
       requireFile {
          message = getTheDemo;
          name = "WorldOfGooDemo.1.41.tar.gz";
@@ -45,7 +46,7 @@ stdenv.mkDerivation rec {
   phases = "unpackPhase installPhase";
 
   # XXX: stdenv.lib.makeLibraryPath doesn't pick up /lib64
-  libPath = stdenv.lib.makeLibraryPath [ stdenv.cc.cc stdenv.cc.libc ] 
+  libPath = stdenv.lib.makeLibraryPath [ stdenv.cc.cc stdenv.cc.libc ]
     + ":" + stdenv.lib.makeLibraryPath [libX11 libXext libXau libxcb libXdmcp SDL SDL_mixer libvorbis libGLU_combined ]
     + ":" + stdenv.cc.cc + "/lib64";
 
@@ -60,7 +61,7 @@ stdenv.mkDerivation rec {
     #makeWrapper doesn't do cd. :(
 
     cat > $out/bin/WorldofGoo << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     cd $out/libexec/2dboy/WorldOfGoo
     exec ./WorldOfGoo.bin64
     EOF
diff --git a/pkgs/misc/cups/drivers/mfcj6510dwlpr/default.nix b/pkgs/misc/cups/drivers/mfcj6510dwlpr/default.nix
index 607e3c2c57f9..e22a159c428a 100644
--- a/pkgs/misc/cups/drivers/mfcj6510dwlpr/default.nix
+++ b/pkgs/misc/cups/drivers/mfcj6510dwlpr/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, pkgsi686Linux, dpkg, makeWrapper, coreutils, gnused, gawk, file, cups, utillinux, xxd
+{ stdenv, fetchurl, pkgsi686Linux, dpkg, makeWrapper, coreutils, gnused, gawk, file, cups, utillinux, xxd, runtimeShell
 , ghostscript, a2ps }:
 
 # Why:
@@ -34,7 +34,7 @@ stdenv.mkDerivation rec {
   unpackPhase = "true";
 
   brprintconf_mfcj6510dw_script = ''
-    #!${stdenv.shell}
+    #!${runtimeShell}
     cd $(mktemp -d)
     ln -s @out@/usr/bin/brprintconf_mfcj6510dw_patched brprintconf_mfcj6510dw_patched
     ln -s @out@/opt/brother/Printers/mfcj6510dw/inf/brmfcj6510dwfunc brmfcj6510dwfunc
diff --git a/pkgs/misc/emulators/higan/default.nix b/pkgs/misc/emulators/higan/default.nix
index 45b2f79146c1..1349fa0f0b6f 100644
--- a/pkgs/misc/emulators/higan/default.nix
+++ b/pkgs/misc/emulators/higan/default.nix
@@ -4,7 +4,8 @@
 , udev
 , libGLU_combined, SDL
 , libao, openal, libpulseaudio
-, gtk2, gtksourceview }:
+, gtk2, gtksourceview
+, runtimeShell }:
 
 with stdenv.lib;
 stdenv.mkDerivation rec {
@@ -55,7 +56,7 @@ stdenv.mkDerivation rec {
     # stuff needed by higan at runtime
 
     cat <<EOF > $out/bin/higan-init.sh
-    #!${stdenv.shell}
+    #!${runtimeShell}
 
     cp --recursive --update $out/share/higan/*.sys \$HOME/.local/share/higan/
 
@@ -69,8 +70,8 @@ stdenv.mkDerivation rec {
     longDescription = ''
       higan (formerly bsnes) is a multi-system game console emulator.
       It currently supports the following systems:
-        - Nintendo's Famicom, Super Famicom (with subsystems: 
-          Super Game Boy, BS-X Satellaview, Sufami Turbo); 
+        - Nintendo's Famicom, Super Famicom (with subsystems:
+          Super Game Boy, BS-X Satellaview, Sufami Turbo);
           Game Boy, Game Boy Color, Game Boy Advance;
         - Sega's Master System, Game Gear, Mega Drive;
         - NEC's PC Engine, SuperGrafx;
diff --git a/pkgs/misc/emulators/kega-fusion/default.nix b/pkgs/misc/emulators/kega-fusion/default.nix
index bdc372c7ca23..4f40c10b0377 100644
--- a/pkgs/misc/emulators/kega-fusion/default.nix
+++ b/pkgs/misc/emulators/kega-fusion/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, lib, writeText, fetchurl, upx, libGLU, glib, gtk2, alsaLib, libSM, libX11, gdk_pixbuf, pango, libXinerama, mpg123 }:
+{ stdenv, lib, writeText, fetchurl, upx, libGLU, glib, gtk2, alsaLib, libSM, libX11, gdk_pixbuf, pango, libXinerama, mpg123, runtimeShell }:
 
 let
   libPath = lib.makeLibraryPath [ stdenv.cc.cc libGLU glib gtk2 alsaLib libSM libX11 gdk_pixbuf pango libXinerama ];
@@ -18,7 +18,7 @@ in stdenv.mkDerivation rec {
   };
 
   runner = writeText "kega-fusion" ''
-    #!${stdenv.shell} -ex
+    #!${runtimeShell} -ex
 
     kega_libdir="@out@/lib/kega-fusion"
     kega_localdir="$HOME/.Kega Fusion"
diff --git a/pkgs/misc/emulators/retroarch/kodi-advanced-launchers.nix b/pkgs/misc/emulators/retroarch/kodi-advanced-launchers.nix
index 6b708cccc8f8..1072a66e0eac 100644
--- a/pkgs/misc/emulators/retroarch/kodi-advanced-launchers.nix
+++ b/pkgs/misc/emulators/retroarch/kodi-advanced-launchers.nix
@@ -1,4 +1,4 @@
-{ stdenv, pkgs, cores }:
+{ stdenv, pkgs, cores, runtimeShell }:
 
 assert cores != [];
 
@@ -7,7 +7,7 @@ with pkgs.lib;
 let
 
   script = exec: ''
-    #!${stdenv.shell}
+    #!${runtimeShell}
     nohup sh -c "pkill -SIGTSTP kodi" &
     # https://forum.kodi.tv/showthread.php?tid=185074&pid=1622750#pid1622750
     nohup sh -c "sleep 10 && ${exec} '$@' -f;pkill -SIGCONT kodi"
diff --git a/pkgs/misc/emulators/retrofe/default.nix b/pkgs/misc/emulators/retrofe/default.nix
index fa2121f5bc16..2537669bbc42 100644
--- a/pkgs/misc/emulators/retrofe/default.nix
+++ b/pkgs/misc/emulators/retrofe/default.nix
@@ -1,5 +1,5 @@
 { stdenv, fetchhg, cmake, glib, gst_all_1, makeWrapper, pkgconfig
-, python, SDL2, SDL2_image, SDL2_mixer, SDL2_ttf, sqlite, zlib
+, python, SDL2, SDL2_image, SDL2_mixer, SDL2_ttf, sqlite, zlib, runtimeShell
 }:
 
 stdenv.mkDerivation rec {
@@ -38,7 +38,7 @@ stdenv.mkDerivation rec {
     mv $out/share/retrofe/example/retrofe $out/bin/
 
     cat > $out/bin/retrofe-init << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
 
     echo "This will install retrofe's example files into this directory"
     echo "Example files location: $out/share/retrofe/example/"
diff --git a/pkgs/misc/vim-plugins/vim-utils.nix b/pkgs/misc/vim-plugins/vim-utils.nix
index 3e9fb7facf71..14332c700780 100644
--- a/pkgs/misc/vim-plugins/vim-utils.nix
+++ b/pkgs/misc/vim-plugins/vim-utils.nix
@@ -1,6 +1,6 @@
 { stdenv, vim, vimPlugins, vim_configurable, neovim, buildEnv, writeText, writeScriptBin
 , nix-prefetch-hg, nix-prefetch-git
-, fetchFromGitHub
+, fetchFromGitHub, runtimeShell
 }:
 
 /*
@@ -374,7 +374,7 @@ rec {
     let
       rcOption = o: file: stdenv.lib.optionalString (file != null) "-${o} ${file}";
       vimWrapperScript = writeScriptBin vimExecutableName ''
-        #!${stdenv.shell}
+        #!${runtimeShell}
         exec ${vimExecutable} ${rcOption "u" vimrcFile} ${rcOption "U" gvimrcFile} "$@"
       '';
       gvimWrapperScript = writeScriptBin gvimExecutableName ''
diff --git a/pkgs/misc/vscode-extensions/cpptools/default.nix b/pkgs/misc/vscode-extensions/cpptools/default.nix
index e5b83511e72e..2a9eeae3d579 100644
--- a/pkgs/misc/vscode-extensions/cpptools/default.nix
+++ b/pkgs/misc/vscode-extensions/cpptools/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchzip, vscode-utils, jq, mono, clang-tools, writeScript
+{ stdenv, fetchzip, vscode-utils, jq, mono, clang-tools, writeScript, runtimeShell
 , gdbUseFixed ? true, gdb # The gdb default setting will be fixed to specified. Use version from `PATH` otherwise.
 }:
 
@@ -53,7 +53,7 @@ let
   };
 
   openDebugAD7Script = writeScript "OpenDebugAD7" ''
-    #!${stdenv.shell}
+    #!${runtimeShell}
     BIN_DIR="$(cd "$(dirname "$0")" && pwd -P)"
     ${if gdbUseFixed
         then ''
diff --git a/pkgs/os-specific/darwin/stubs/default.nix b/pkgs/os-specific/darwin/stubs/default.nix
index ec8f948a00b1..6fedf0a451e3 100644
--- a/pkgs/os-specific/darwin/stubs/default.nix
+++ b/pkgs/os-specific/darwin/stubs/default.nix
@@ -1,7 +1,7 @@
-{ stdenv, writeScriptBin }:
+{ stdenv, writeScriptBin, runtimeShell }:
 
 let fake = name: stdenv.lib.overrideDerivation (writeScriptBin name ''
-  #!${stdenv.shell}
+  #!${runtimeShell}
   echo >&2 "Faking call to ${name} with arguments:"
   echo >&2 "$@"
 '') (drv: {
diff --git a/pkgs/os-specific/linux/bbswitch/default.nix b/pkgs/os-specific/linux/bbswitch/default.nix
index f5bc7458240a..4b9148600686 100644
--- a/pkgs/os-specific/linux/bbswitch/default.nix
+++ b/pkgs/os-specific/linux/bbswitch/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, fetchpatch, kernel }:
+{ stdenv, fetchurl, fetchpatch, kernel, runtimeShell }:
 
 let
   baseName = "bbswitch";
@@ -36,12 +36,12 @@ stdenv.mkDerivation {
 
     mkdir -p $out/bin
     tee $out/bin/discrete_vga_poweroff << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
 
     echo -n OFF > /proc/acpi/bbswitch
     EOF
     tee $out/bin/discrete_vga_poweron << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
 
     echo -n ON > /proc/acpi/bbswitch
     EOF
diff --git a/pkgs/os-specific/linux/pam_usb/default.nix b/pkgs/os-specific/linux/pam_usb/default.nix
index baf24b275626..9b9157255142 100644
--- a/pkgs/os-specific/linux/pam_usb/default.nix
+++ b/pkgs/os-specific/linux/pam_usb/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, makeWrapper, dbus, libxml2, pam, pkgconfig, pmount, pythonPackages, writeScript }:
+{ stdenv, fetchurl, makeWrapper, dbus, libxml2, pam, pkgconfig, pmount, pythonPackages, writeScript, runtimeShell }:
 
 let
 
@@ -11,7 +11,7 @@ let
       bin = "${drv}${path}";
     in assert name != "";
       writeScript "setUID-${name}" ''
-        #!${stdenv.shell}
+        #!${runtimeShell}
         inode=$(stat -Lc %i ${bin})
         for file in $(type -ap ${name}); do
           case $(stat -Lc %a $file) in
diff --git a/pkgs/os-specific/linux/rfkill/udev.nix b/pkgs/os-specific/linux/rfkill/udev.nix
index de23071f0260..6ba4f8d4b064 100644
--- a/pkgs/os-specific/linux/rfkill/udev.nix
+++ b/pkgs/os-specific/linux/rfkill/udev.nix
@@ -10,7 +10,7 @@
 # Add a hook script in the managed etc directory, e.g.:
 #   etc = [
 #     { source = pkgs.writeScript "rtfkill.hook" ''
-#         #!${stdenv.shell}
+#         #!${pkgs.runtimeShell}
 #
 #         if [ "$RFKILL_STATE" -eq "1" ]; then
 #           exec ${config.system.build.upstart}/sbin/initctl emit -n antenna-on
@@ -39,7 +39,7 @@ in stdenv.mkDerivation {
   installPhase = ''
     mkdir -p "$out/etc/udev/rules.d/";
     cat > "$out/etc/udev/rules.d/90-rfkill.rules" << EOF
-      SUBSYSTEM=="rfkill", ATTR{type}=="wlan", RUN+="$out/bin/rfkill-hook.sh" 
+      SUBSYSTEM=="rfkill", ATTR{type}=="wlan", RUN+="$out/bin/rfkill-hook.sh"
     EOF
 
     mkdir -p "$out/bin/";
diff --git a/pkgs/os-specific/linux/tp_smapi/update.nix b/pkgs/os-specific/linux/tp_smapi/update.nix
index 1b6dfd90b1e4..b89912434ec5 100644
--- a/pkgs/os-specific/linux/tp_smapi/update.nix
+++ b/pkgs/os-specific/linux/tp_smapi/update.nix
@@ -1,7 +1,7 @@
-{ stdenv, lib, writeScript, coreutils, curl, gnugrep, jq, common-updater-scripts }:
+{ stdenv, lib, writeScript, coreutils, curl, gnugrep, jq, common-updater-scripts, runtimeShell }:
 
 writeScript "update-tp_smapi" ''
-#!${stdenv.shell}
+#!${runtimeShell}
 PATH=${lib.makeBinPath [ common-updater-scripts coreutils curl gnugrep jq ]}
 
 tags=`curl -s https://api.github.com/repos/evgeni/tp_smapi/tags`
diff --git a/pkgs/servers/mail/system-sendmail/default.nix b/pkgs/servers/mail/system-sendmail/default.nix
index fe0aeb5b3afa..7bd7df849244 100644
--- a/pkgs/servers/mail/system-sendmail/default.nix
+++ b/pkgs/servers/mail/system-sendmail/default.nix
@@ -1,7 +1,7 @@
-{ stdenv, writeText }:
+{ stdenv, writeText, runtimeShell }:
 
 let script = writeText "script" ''
-  #!${stdenv.shell}
+  #!${runtimeShell}
 
   if command -v sendmail > /dev/null 2>&1 && [ "$(command -v sendmail)" != "{{MYPATH}}" ]; then
     exec sendmail "$@"
diff --git a/pkgs/servers/meteor/default.nix b/pkgs/servers/meteor/default.nix
index 132f812bf464..b677b6080d1f 100644
--- a/pkgs/servers/meteor/default.nix
+++ b/pkgs/servers/meteor/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, lib, fetchurl, zlib, patchelf }:
+{ stdenv, lib, fetchurl, zlib, patchelf, runtimeShell }:
 
 let
   bootstrap = fetchurl {
@@ -69,7 +69,7 @@ stdenv.mkDerivation rec {
     # otherwise it fails spectacularly.
     mkdir -p $out/bin
     cat << EOF > $out/bin/meteor
-    #!${stdenv.shell}
+    #!${runtimeShell}
 
     if [[ ! -f \$HOME/.meteor/package-metadata/v2.0.1/packages.data.db ]]; then
       mkdir -p \$HOME/.meteor/package-metadata/v2.0.1
diff --git a/pkgs/servers/monitoring/plugins/default.nix b/pkgs/servers/monitoring/plugins/default.nix
index a0f7341571ce..3e6eaa4766e8 100644
--- a/pkgs/servers/monitoring/plugins/default.nix
+++ b/pkgs/servers/monitoring/plugins/default.nix
@@ -1,5 +1,6 @@
 { stdenv, fetchFromGitHub, autoreconfHook
-, coreutils, gnugrep, gnused, lm_sensors, net_snmp, openssh, openssl, perl }:
+, coreutils, gnugrep, gnused, lm_sensors, net_snmp, openssh, openssl, perl
+, runtimeShell }:
 
 with stdenv.lib;
 
@@ -50,7 +51,7 @@ in stdenv.mkDerivation rec {
   preBuild = ''
     mkdir -p $out
     cat <<_EOF > $out/share
-#!${stdenv.shell}
+#!${runtimeShell}
 exit 0
 _EOF
     chmod 755 $out/share
diff --git a/pkgs/servers/nosql/eventstore/default.nix b/pkgs/servers/nosql/eventstore/default.nix
index 1d3240fae716..7880c6db7fd9 100644
--- a/pkgs/servers/nosql/eventstore/default.nix
+++ b/pkgs/servers/nosql/eventstore/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, git, mono, v8 }:
+{ stdenv, fetchFromGitHub, git, mono, v8, runtimeShell }:
 
 # There are some similarities with the pinta derivation. We should
 # have a helper to make it easy to package these Mono apps.
@@ -27,7 +27,7 @@ stdenv.mkDerivation rec {
     mkdir -p $out/{bin,lib/eventstore/clusternode}
     cp -r bin/clusternode/* $out/lib/eventstore/clusternode/
     cat > $out/bin/clusternode << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     exec ${mono}/bin/mono $out/lib/eventstore/clusternode/EventStore.ClusterNode.exe "\$@"
     EOF
     chmod +x $out/bin/clusternode
diff --git a/pkgs/servers/web-apps/codimd/default.nix b/pkgs/servers/web-apps/codimd/default.nix
index c48a9f938bdf..d5e84cce9849 100644
--- a/pkgs/servers/web-apps/codimd/default.nix
+++ b/pkgs/servers/web-apps/codimd/default.nix
@@ -1,5 +1,5 @@
 { stdenv, pkgs, buildEnv, fetchFromGitHub, makeWrapper
-, fetchpatch, nodejs-6_x, phantomjs2 }:
+, fetchpatch, nodejs-6_x, phantomjs2, runtimeShell }:
 let
   nodePackages = import ./node.nix {
     inherit pkgs;
@@ -126,7 +126,7 @@ stdenv.mkDerivation rec {
   installPhase = ''
     mkdir -p $out/bin
     cat > $out/bin/codimd <<EOF
-      #!${stdenv.shell}
+      #!${runtimeShell}
       ${nodejs-6_x}/bin/node $out/app.js
     EOF
     cp -R {app.js,bin,lib,locales,package.json,public} $out/
diff --git a/pkgs/servers/xmpp/pyIRCt/default.nix b/pkgs/servers/xmpp/pyIRCt/default.nix
index 535fb061de9b..8df8499d11eb 100644
--- a/pkgs/servers/xmpp/pyIRCt/default.nix
+++ b/pkgs/servers/xmpp/pyIRCt/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, xmpppy, pythonIRClib, python, pythonPackages } :
+{ stdenv, fetchurl, xmpppy, pythonIRClib, python, pythonPackages, runtimeShell } :
 
 stdenv.mkDerivation rec {
   name = "pyIRCt-${version}";
@@ -26,7 +26,7 @@ stdenv.mkDerivation rec {
     sed -e '/configFiles/iimport os' -i config.py
     cp * $out/share/${name}
     cat > $out/bin/pyIRCt <<EOF
-      #!${stdenv.shell}
+      #!${runtimeShell}
       cd $out/share/${name}
       ./irc.py \"$@\"
     EOF
diff --git a/pkgs/servers/xmpp/pyMAILt/default.nix b/pkgs/servers/xmpp/pyMAILt/default.nix
index b1c4d93f687e..76861ab842b5 100644
--- a/pkgs/servers/xmpp/pyMAILt/default.nix
+++ b/pkgs/servers/xmpp/pyMAILt/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, python, xmpppy, pythonPackages, fetchcvs } :
+{ stdenv, python, xmpppy, pythonPackages, fetchcvs, runtimeShell } :
 
 stdenv.mkDerivation rec {
   name = "pyMAILt-${version}";
@@ -24,7 +24,7 @@ stdenv.mkDerivation rec {
     sed -e '/configFiles/iimport os' -i config.py
     cp * $out/share/$name
     cat > $out/bin/pyMAILt <<EOF
-      #!${stdenv.shell}
+      #!${runtimeShell}
       cd $out/share/${name}
       ./mail.py \"$@\"
     EOF
diff --git a/pkgs/servers/zookeeper/default.nix b/pkgs/servers/zookeeper/default.nix
index 780a77c5caaf..5bacaf2982e9 100644
--- a/pkgs/servers/zookeeper/default.nix
+++ b/pkgs/servers/zookeeper/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, jre, makeWrapper, bash, coreutils }:
+{ stdenv, fetchurl, jre, makeWrapper, bash, coreutils, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "zookeeper-${version}";
@@ -38,7 +38,7 @@ stdenv.mkDerivation rec {
     done
 
     cat << EOF > $out/bin/zooInspector.sh
-    #!${stdenv.shell}
+    #!${runtimeShell}
     cd $out/share/zooinspector
     exec ${jre}/bin/java -cp $classpath org.apache.zookeeper.inspector.ZooInspector
     EOF
diff --git a/pkgs/tools/X11/primus/default.nix b/pkgs/tools/X11/primus/default.nix
index 669cb4b4844a..bf3ff7c65281 100644
--- a/pkgs/tools/X11/primus/default.nix
+++ b/pkgs/tools/X11/primus/default.nix
@@ -8,6 +8,7 @@
 , lib
 , primusLib
 , writeScriptBin
+, runtimeShell
 , primusLib_i686 ? null
 , useNvidia ? true
 }:
@@ -25,7 +26,7 @@ let
   ));
 
 in writeScriptBin "primusrun" ''
-  #!${stdenv.shell}
+  #!${runtimeShell}
   export LD_LIBRARY_PATH=${ldPath}:$LD_LIBRARY_PATH
   exec "$@"
 ''
diff --git a/pkgs/tools/audio/beets/default.nix b/pkgs/tools/audio/beets/default.nix
index 8d2bf5e634c3..c9a369ce4f7d 100644
--- a/pkgs/tools/audio/beets/default.nix
+++ b/pkgs/tools/audio/beets/default.nix
@@ -1,5 +1,6 @@
 { stdenv, fetchFromGitHub, writeScript, glibcLocales, diffPlugins
 , pythonPackages, imagemagick, gobject-introspection, gst_all_1
+, runtimeShell
 , fetchpatch
 
 # Attributes needed for tests of the external plugins
@@ -227,7 +228,7 @@ in pythonPackages.buildPythonApplication rec {
     tmphome="$(mktemp -d)"
 
     EDITOR="${writeScript "beetconfig.sh" ''
-      #!${stdenv.shell}
+      #!${runtimeShell}
       cat > "$1" <<CFG
       plugins: ${concatStringsSep " " allEnabledPlugins}
       CFG
diff --git a/pkgs/tools/backup/flockit/default.nix b/pkgs/tools/backup/flockit/default.nix
index e6284aa42d75..224b8b12906e 100644
--- a/pkgs/tools/backup/flockit/default.nix
+++ b/pkgs/tools/backup/flockit/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub }:
+{ stdenv, fetchFromGitHub, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "flockit-${version}";
@@ -16,7 +16,7 @@ stdenv.mkDerivation rec {
     cp ./libflockit.so $out/lib
 
     (cat <<EOI
-    #!${stdenv.shell}
+    #!${runtimeShell}
     env LD_PRELOAD="$out/lib/libflockit.so" FLOCKIT_FILE_PREFIX=\$1 \''${@:2}
     EOI
     ) > $out/bin/flockit
diff --git a/pkgs/tools/compression/mozlz4a/default.nix b/pkgs/tools/compression/mozlz4a/default.nix
index ce2aa9520d67..19c160c62f5a 100644
--- a/pkgs/tools/compression/mozlz4a/default.nix
+++ b/pkgs/tools/compression/mozlz4a/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, python3 }:
+{ stdenv, fetchurl, python3, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "${pname}-${version}";
@@ -16,7 +16,7 @@ stdenv.mkDerivation rec {
     mkdir -p "$out/bin" "$out/${python3.sitePackages}/"
     cp "${src}" "$out/${python3.sitePackages}/mozlz4a.py"
 
-    echo "#!${stdenv.shell}" >> "$out/bin/mozlz4a"
+    echo "#!${runtimeShell}" >> "$out/bin/mozlz4a"
     echo "export PYTHONPATH='$PYTHONPATH'" >> "$out/bin/mozlz4a"
     echo "'${python3}/bin/python' '$out/${python3.sitePackages}/mozlz4a.py' \"\$@\"" >> "$out/bin/mozlz4a"
     chmod a+x "$out/bin/mozlz4a"
diff --git a/pkgs/tools/graphics/briss/default.nix b/pkgs/tools/graphics/briss/default.nix
index fb9b7cae488f..a6b3ba01bfa4 100644
--- a/pkgs/tools/graphics/briss/default.nix
+++ b/pkgs/tools/graphics/briss/default.nix
@@ -1,6 +1,6 @@
 # The releases of this project are apparently precompiled to .jar files.
 
-{ stdenv, fetchurl, jre }:
+{ stdenv, fetchurl, jre, runtimeShell }:
 
 let
 
@@ -18,7 +18,7 @@ in stdenv.mkDerivation {
     mkdir -p "$out/bin";
     mkdir -p "$out/share";
     install -D -m444 -t "$out/share" *.jar
-    echo "#!${stdenv.shell}" > "$out/bin/briss"
+    echo "#!${runtimeShell}" > "$out/bin/briss"
     echo "${jre}/bin/java -Xms128m -Xmx1024m -cp \"$out/share/\" -jar \"$out/share/briss-${version}.jar\"" >> "$out/bin/briss"
     chmod +x "$out/bin/briss"
   '';
diff --git a/pkgs/tools/graphics/mscgen/default.nix b/pkgs/tools/graphics/mscgen/default.nix
index e9301731e856..666cdae131e2 100644
--- a/pkgs/tools/graphics/mscgen/default.nix
+++ b/pkgs/tools/graphics/mscgen/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, flex, bison, gd, libpng, libjpeg, freetype, zlib, libwebp }:
+{ stdenv, fetchurl, flex, bison, gd, libpng, libjpeg, freetype, zlib, libwebp, runtimeShell }:
 
 let
   version = "0.20";
@@ -15,7 +15,7 @@ stdenv.mkDerivation {
 
   doCheck = true;
   preCheck = ''
-    sed -i -e "s|#!/bin/bash|#!${stdenv.shell}|" test/renderercheck.sh
+    sed -i -e "s|#!/bin/bash|#!${runtimeShell}|" test/renderercheck.sh
   '';
 
   meta = {
diff --git a/pkgs/tools/inputmethods/ibus/default.nix b/pkgs/tools/inputmethods/ibus/default.nix
index 16580aac9557..b561fb626e05 100644
--- a/pkgs/tools/inputmethods/ibus/default.nix
+++ b/pkgs/tools/inputmethods/ibus/default.nix
@@ -2,7 +2,7 @@
 , vala, wrapGAppsHook, dbus, dconf ? null, glib, gdk_pixbuf, gobject-introspection, gtk2
 , gtk3, gtk-doc, isocodes, python3, json-glib, libnotify ? null, enablePythonLibrary ? true
 , enableUI ? true, withWayland ? false, libxkbcommon ? null, wayland ? null
-, buildPackages }:
+, buildPackages, runtimeShell }:
 
 assert withWayland -> wayland != null && libxkbcommon != null;
 
@@ -76,7 +76,7 @@ stdenv.mkDerivation rec {
     substituteInPlace setup/ibus-setup.in --subst-var-by PYTHON ${python3Runtime.interpreter}
     substituteInPlace data/dconf/Makefile.am --replace "dconf update" true
     substituteInPlace configure.ac --replace '$python2dir/ibus' $out/${python3.sitePackages}/ibus
-    echo \#!${stdenv.shell} > data/dconf/make-dconf-override-db.sh
+    echo \#!${runtimeShell} > data/dconf/make-dconf-override-db.sh
     cp ${buildPackages.gtk-doc}/share/gtk-doc/data/gtk-doc.make .
   '';
 
diff --git a/pkgs/tools/misc/fzf/default.nix b/pkgs/tools/misc/fzf/default.nix
index 08efe3e6e18b..de211d1d1c4e 100644
--- a/pkgs/tools/misc/fzf/default.nix
+++ b/pkgs/tools/misc/fzf/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, ncurses, buildGoPackage, fetchFromGitHub, writeText }:
+{ stdenv, ncurses, buildGoPackage, fetchFromGitHub, writeText, runtimeShell }:
 
 buildGoPackage rec {
   name = "fzf-${version}";
@@ -47,7 +47,7 @@ buildGoPackage rec {
 
     cp -R $src/shell $bin/share/fzf
     cat <<SCRIPT > $bin/bin/fzf-share
-    #!${stdenv.shell}
+    #!${runtimeShell}
     # Run this script to find the fzf shared folder where all the shell
     # integration scripts are living.
     echo $bin/share/fzf
diff --git a/pkgs/tools/misc/odroid-xu3-bootloader/default.nix b/pkgs/tools/misc/odroid-xu3-bootloader/default.nix
index 3cce28df4f3d..3896ca77a166 100644
--- a/pkgs/tools/misc/odroid-xu3-bootloader/default.nix
+++ b/pkgs/tools/misc/odroid-xu3-bootloader/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, lib, fetchFromGitHub, coreutils, ubootOdroidXU3 }:
+{ stdenv, lib, fetchFromGitHub, coreutils, ubootOdroidXU3, runtimeShell }:
 
 stdenv.mkDerivation {
   name = "odroid-xu3-bootloader-2015-12-04";
@@ -17,7 +17,7 @@ stdenv.mkDerivation {
 
     install -Dm755 $src/sd_fuse/hardkernel_1mb_uboot/sd_fusing.1M.sh $out/bin/sd_fuse-xu3
     sed -i \
-      -e '1i#!${stdenv.shell}' \
+      -e '1i#!${runtimeShell}' \
       -e '1iPATH=${lib.makeBinPath [ coreutils ]}:$PATH' \
       -e '/set -x/d' \
       -e 's,.\/sd_fusing\.sh,sd_fuse-xu3,g' \
diff --git a/pkgs/tools/misc/smc/default.nix b/pkgs/tools/misc/smc/default.nix
index e0d1ff208353..0e84f738e80e 100644
--- a/pkgs/tools/misc/smc/default.nix
+++ b/pkgs/tools/misc/smc/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, jre }:
+{ stdenv, fetchurl, jre, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "smc-6.6.3";
@@ -22,7 +22,7 @@ stdenv.mkDerivation rec {
     cp misc/smc.ico "$out/share/icons/"
 
     cat > "$out/bin/smc" << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     ${jre}/bin/java -jar "$out/share/java/Smc.jar" "\$@"
     EOF
     chmod a+x "$out/bin/smc"
diff --git a/pkgs/tools/misc/umlet/default.nix b/pkgs/tools/misc/umlet/default.nix
index c04c16ae1cc7..9ac60ca8b13a 100644
--- a/pkgs/tools/misc/umlet/default.nix
+++ b/pkgs/tools/misc/umlet/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, jre, unzip }:
+{ stdenv, fetchurl, jre, unzip, runtimeShell }:
 
 stdenv.mkDerivation rec {
   major = "14";
@@ -20,7 +20,7 @@ stdenv.mkDerivation rec {
     cp -R * "$out/lib"
 
     cat > "$out/bin/umlet" << EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
 
     programDir="$out/lib"
     cd "\$programDir"
diff --git a/pkgs/tools/misc/xdummy/default.nix b/pkgs/tools/misc/xdummy/default.nix
index 8a411d89da5d..bdcdc47ea131 100644
--- a/pkgs/tools/misc/xdummy/default.nix
+++ b/pkgs/tools/misc/xdummy/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, writeText, writeScriptBin, xorg, xkeyboard_config }:
+{ stdenv, writeText, writeScriptBin, xorg, xkeyboard_config, runtimeShell }:
 
 let
   xorgConfig = writeText "dummy-xorg.conf" ''
@@ -73,7 +73,7 @@ let
   '';
 
 in writeScriptBin "xdummy" ''
-  #!${stdenv.shell}
+  #!${runtimeShell}
   exec ${xorg.xorgserver.out}/bin/Xorg \
     -noreset \
     -logfile /dev/null \
diff --git a/pkgs/tools/misc/xfstests/default.nix b/pkgs/tools/misc/xfstests/default.nix
index 7305013eb8b7..3be931a362be 100644
--- a/pkgs/tools/misc/xfstests/default.nix
+++ b/pkgs/tools/misc/xfstests/default.nix
@@ -1,7 +1,7 @@
 { stdenv, acl, attr, autoconf, automake, bash, bc, coreutils, e2fsprogs
 , fetchgit, fio, gawk, keyutils, killall, lib, libaio, libcap, libtool
 , libuuid, libxfs, lvm2, openssl, perl, procps, quota
-, time, utillinux, which, writeScript, xfsprogs }:
+, time, utillinux, which, writeScript, xfsprogs, runtimeShell }:
 
 stdenv.mkDerivation {
   name = "xfstests-2018-04-11";
@@ -78,7 +78,7 @@ stdenv.mkDerivation {
   # wants to write temporary files there. So create a temporary
   # to run from and symlink the runtime files to it.
   wrapperScript = writeScript "xfstests-check" ''
-    #!${stdenv.shell}
+    #!${runtimeShell}
     set -e
     export RESULT_BASE="$(pwd)/results"
 
diff --git a/pkgs/tools/misc/zsh-autoenv/default.nix b/pkgs/tools/misc/zsh-autoenv/default.nix
index 589e6717c7dc..61945266e13b 100644
--- a/pkgs/tools/misc/zsh-autoenv/default.nix
+++ b/pkgs/tools/misc/zsh-autoenv/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub }:
+{ stdenv, fetchFromGitHub, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "zsh-autoenv-${version}";
@@ -18,7 +18,7 @@ stdenv.mkDerivation rec {
     cp -R $src $out/share/zsh-autoenv
 
     cat <<SCRIPT > $out/bin/zsh-autoenv-share
-    #!${stdenv.shell}
+    #!${runtimeShell}
     # Run this script to find the fzf shared folder where all the shell
     # integration scripts are living.
     echo $out/share/zsh-autoenv
diff --git a/pkgs/tools/networking/airfield/default.nix b/pkgs/tools/networking/airfield/default.nix
index 94a606f9c782..c53cce49a388 100644
--- a/pkgs/tools/networking/airfield/default.nix
+++ b/pkgs/tools/networking/airfield/default.nix
@@ -1,6 +1,6 @@
 { stdenv, fetchFromGitHub
 , pkgs, makeWrapper, buildEnv
-, nodejs
+, nodejs, runtimeShell
 }:
 
 let
@@ -49,7 +49,7 @@ in stdenv.mkDerivation {
   installPhase = ''
     mkdir -p $out/bin
     cat >$out/bin/airfield <<EOF
-      #!${stdenv.shell}
+      #!${runtimeShell}
       ${nodejs}/bin/node ${src}/airfield.js
     EOF
   '';
diff --git a/pkgs/tools/networking/burpsuite/default.nix b/pkgs/tools/networking/burpsuite/default.nix
index 194758905ff2..6c440fc48fd8 100644
--- a/pkgs/tools/networking/burpsuite/default.nix
+++ b/pkgs/tools/networking/burpsuite/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, jre }:
+{ stdenv, fetchurl, jre, runtimeShell }:
 
 let
   version = "1.7.23";
@@ -8,7 +8,7 @@ let
     sha256 = "1y83qisn9pkn88vphpli7h8nacv8jv3sq0h04zbri25nfkgvl4an";
   };
   launcher = ''
-    #!${stdenv.shell}
+    #!${runtimeShell}
     exec ${jre}/bin/java -jar ${jar} "$@"
   '';
 in stdenv.mkDerivation {
diff --git a/pkgs/tools/networking/easyrsa/default.nix b/pkgs/tools/networking/easyrsa/default.nix
index 8205bddc7fe0..d7ff19612eb1 100644
--- a/pkgs/tools/networking/easyrsa/default.nix
+++ b/pkgs/tools/networking/easyrsa/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, openssl }:
+{ stdenv, fetchFromGitHub, openssl, runtimeShell }:
 
 let
   version = "3.0.0";
@@ -24,7 +24,7 @@ in stdenv.mkDerivation rec {
 
     # Helper utility
     cat > $out/bin/easyrsa-init <<EOF
-    #!${stdenv.shell} -e
+    #!${runtimeShell} -e
     cp -r $out/share/easyrsa/* .
     EOF
     chmod +x $out/bin/easyrsa-init
diff --git a/pkgs/tools/networking/slack-cli/default.nix b/pkgs/tools/networking/slack-cli/default.nix
index 18f192d3535f..44b262861e1c 100644
--- a/pkgs/tools/networking/slack-cli/default.nix
+++ b/pkgs/tools/networking/slack-cli/default.nix
@@ -5,7 +5,7 @@
 # for token storage, except that it would make the Nix package inconsistent with
 # upstream and other distributions.
 
-{ stdenv, lib, writeShellScriptBin, fetchFromGitHub, curl, jq }:
+{ stdenv, lib, writeShellScriptBin, fetchFromGitHub, curl, jq, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "slack-cli-${version}";
@@ -25,7 +25,7 @@ stdenv.mkDerivation rec {
     cp src/slack "$out/bin/.slack-wrapped"
 
     cat <<-WRAPPER > "$out/bin/slack"
-    #!${stdenv.shell}
+    #!${runtimeShell}
     [ "\$1" = "init" -a -z "\$SLACK_CLI_TOKEN" ] && cat <<-'MESSAGE' >&2
     WARNING: slack-cli must be configured using the SLACK_CLI_TOKEN
     environment variable. Using \`slack init\` will not work because it tries
diff --git a/pkgs/tools/networking/zap/default.nix b/pkgs/tools/networking/zap/default.nix
index 56b54fed61b9..7cac5a455700 100644
--- a/pkgs/tools/networking/zap/default.nix
+++ b/pkgs/tools/networking/zap/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, jdk, ant }:
+{ stdenv, fetchFromGitHub, jdk, ant, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "zap-${version}";
@@ -22,7 +22,7 @@ stdenv.mkDerivation rec {
     mkdir -p "$out/share"
     tar xvf  "ZAP_${version}_Linux.tar.gz" -C "$out/share/"
     mkdir -p "$out/bin"
-    echo "#!${stdenv.shell}" > "$out/bin/zap"
+    echo "#!${runtimeShell}" > "$out/bin/zap"
     echo \"$out/share/ZAP_${version}/zap.sh\" >> "$out/bin/zap"
     chmod +x "$out/bin/zap"
   '';
@@ -30,7 +30,7 @@ stdenv.mkDerivation rec {
   meta = with stdenv.lib; {
     homepage = https://www.owasp.org/index.php/ZAP;
     description = "Java application for web penetration testing";
-    maintainers = with maintainers; [ mog ]; 
+    maintainers = with maintainers; [ mog ];
     platforms = platforms.linux;
     license = licenses.asl20;
   };
diff --git a/pkgs/tools/package-management/appimage-run/default.nix b/pkgs/tools/package-management/appimage-run/default.nix
index 75ce3344e203..49fe307667af 100644
--- a/pkgs/tools/package-management/appimage-run/default.nix
+++ b/pkgs/tools/package-management/appimage-run/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, writeScript, buildFHSUserEnv, coreutils, file, libarchive
+{ stdenv, writeScript, buildFHSUserEnv, coreutils, file, libarchive, runtimeShell
 , extraPkgs ? pkgs: [], appimageTools }:
 
 let
@@ -9,7 +9,7 @@ in buildFHSUserEnv (fhsArgs // {
   targetPkgs = pkgs: fhsArgs.targetPkgs pkgs ++ extraPkgs pkgs;
 
   runScript = writeScript "appimage-exec" ''
-    #!${stdenv.shell}
+    #!${runtimeShell}
     APPIMAGE="$(realpath "$1")"
 
     if [ ! -x "$APPIMAGE" ]; then
diff --git a/pkgs/tools/package-management/nix-pin/default.nix b/pkgs/tools/package-management/nix-pin/default.nix
index 063c173a4019..efedb8b67da9 100644
--- a/pkgs/tools/package-management/nix-pin/default.nix
+++ b/pkgs/tools/package-management/nix-pin/default.nix
@@ -1,4 +1,5 @@
-{ lib, pkgs, stdenv, fetchFromGitHub, mypy, python3, nix, git, makeWrapper }:
+{ lib, pkgs, stdenv, fetchFromGitHub, mypy, python3, nix, git, makeWrapper
+, runtimeShell }:
 let self = stdenv.mkDerivation rec {
   name = "nix-pin-${version}";
   version = "0.4.0";
@@ -26,7 +27,7 @@ let self = stdenv.mkDerivation rec {
         let impl = import "${self}/share/nix/api.nix" { inherit pkgs pinConfig; }; in
         { inherit (impl) augmentedPkgs pins callPackage; };
       updateScript = ''
-        #!${stdenv.shell}
+        #!${runtimeShell}
         set -e
         echo
         cd ${toString ./.}
diff --git a/pkgs/tools/package-management/nix-update-source/default.nix b/pkgs/tools/package-management/nix-update-source/default.nix
index 7584496f2585..cabd1e491a76 100644
--- a/pkgs/tools/package-management/nix-update-source/default.nix
+++ b/pkgs/tools/package-management/nix-update-source/default.nix
@@ -1,4 +1,5 @@
-{ stdenv, lib, pkgs, fetchFromGitHub, python3Packages, nix-prefetch-scripts }:
+{ stdenv, lib, pkgs, fetchFromGitHub, python3Packages, nix-prefetch-scripts
+, runtimeShell }:
 python3Packages.buildPythonApplication rec {
   version = "0.6.3";
   name = "nix-update-source-${version}";
@@ -28,7 +29,7 @@ python3Packages.buildPythonApplication rec {
         overrideSrc = drv: lib.overrideDerivation drv (orig: { inherit src; });
       };
     updateScript = ''
-      #!${stdenv.shell}
+      #!${runtimeShell}
       set -e
       echo
       cd ${toString ./.}
diff --git a/pkgs/tools/security/jd-gui/default.nix b/pkgs/tools/security/jd-gui/default.nix
index 62b92b7d0bdd..2eda19088376 100644
--- a/pkgs/tools/security/jd-gui/default.nix
+++ b/pkgs/tools/security/jd-gui/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, gradle_2_5, perl, makeWrapper, jre, makeDesktopItem, writeText }:
+{ stdenv, fetchurl, gradle_2_5, perl, makeWrapper, jre, makeDesktopItem, writeText, runtimeShell }:
 
 let
   version = "1.4.0";
@@ -81,7 +81,7 @@ in stdenv.mkDerivation rec {
     cp src/linux/resources/jd_icon_128.png $out/share/icons/hicolor/128x128/apps/jd-gui.png
 
     cat > $out/bin/jd-gui <<EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     export JAVA_HOME=${jre}
     ${jre}/bin/java -jar ${jar} $@
     EOF
diff --git a/pkgs/tools/security/keybase/gui.nix b/pkgs/tools/security/keybase/gui.nix
index 40385940a942..4207bac44732 100644
--- a/pkgs/tools/security/keybase/gui.nix
+++ b/pkgs/tools/security/keybase/gui.nix
@@ -1,6 +1,7 @@
 { stdenv, fetchurl, alsaLib, atk, cairo, cups, udev, hicolor-icon-theme
 , dbus, expat, fontconfig, freetype, gdk_pixbuf, glib, gtk3, gnome3
-, libnotify, nspr, nss, pango, systemd, xorg, autoPatchelfHook, wrapGAppsHook }:
+, libnotify, nspr, nss, pango, systemd, xorg, autoPatchelfHook, wrapGAppsHook
+, runtimeShell }:
 
 let
   versionSuffix = "20190205202117.6394d03e6c";
@@ -71,7 +72,7 @@ stdenv.mkDerivation rec {
     mv opt/keybase $out/share/
 
     cat > $out/bin/keybase-gui <<EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
 
     checkFailed() {
       if [ "\$NIX_SKIP_KEYBASE_CHECKS" = "1" ]; then
diff --git a/pkgs/tools/text/esh/default.nix b/pkgs/tools/text/esh/default.nix
index 2ca515358a67..b4418e04f03b 100644
--- a/pkgs/tools/text/esh/default.nix
+++ b/pkgs/tools/text/esh/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, asciidoctor, gawk, gnused }:
+{ stdenv, fetchFromGitHub, asciidoctor, gawk, gnused, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "esh-${version}";
@@ -20,11 +20,11 @@ stdenv.mkDerivation rec {
   postPatch = ''
     patchShebangs .
     substituteInPlace esh \
-        --replace '"/bin/sh"' '"${stdenv.shell}"' \
+        --replace '"/bin/sh"' '"${runtimeShell}"' \
         --replace '"awk"' '"${gawk}/bin/awk"' \
         --replace 'sed' '${gnused}/bin/sed'
     substituteInPlace tests/test-dump.exp \
-        --replace '#!/bin/sh' '#!${stdenv.shell}'
+        --replace '#!/bin/sh' '#!${runtimeShell}'
   '';
 
   doCheck = true;
diff --git a/pkgs/tools/typesetting/fop/default.nix b/pkgs/tools/typesetting/fop/default.nix
index 5f926f771c5f..9240926ab5b4 100644
--- a/pkgs/tools/typesetting/fop/default.nix
+++ b/pkgs/tools/typesetting/fop/default.nix
@@ -1,4 +1,4 @@
-{ fetchurl, stdenv, ant, jdk }:
+{ fetchurl, stdenv, ant, jdk, runtimeShell }:
 
 stdenv.mkDerivation rec {
   name = "fop-${version}";
@@ -22,7 +22,7 @@ stdenv.mkDerivation rec {
     # There is a fop script in the source archive, but it has many impurities.
     # Instead of patching out 90 % of the script, we write our own.
     cat > "$out/bin/fop" <<EOF
-    #!${stdenv.shell}
+    #!${runtimeShell}
     java_exec_args="-Djava.awt.headless=true"
     exec ${jdk.jre}/bin/java \$java_exec_args -classpath "$out/lib/*" org.apache.fop.cli.Main "\$@"
     EOF
diff --git a/pkgs/top-level/stage.nix b/pkgs/top-level/stage.nix
index 6ca370e0b9b7..2aa07517fdfb 100644
--- a/pkgs/top-level/stage.nix
+++ b/pkgs/top-level/stage.nix
@@ -67,6 +67,7 @@ let
   trivialBuilders = self: super:
     import ../build-support/trivial-builders.nix {
       inherit lib; inherit (self) stdenv stdenvNoCC; inherit (self.xorg) lndir;
+      inherit (self) runtimeShell;
     };
 
   stdenvBootstappingAndPlatforms = self: super: {