summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorTor Hedin Brønner <torhedinbronner@gmail.com>2018-08-29 16:05:48 +0000
committerJan Malakhovski <oxij@oxij.org>2018-08-30 13:20:39 +0000
commitd273db48c6e95d6547f02845f1921211f113c0c0 (patch)
tree88d5d92de9181a818232a49db42ac9eb92b661cf /nixos
parent6e3d0efdc48d3388dc467d2bdd5d58706d00aa86 (diff)
downloadnixpkgs-d273db48c6e95d6547f02845f1921211f113c0c0.tar
nixpkgs-d273db48c6e95d6547f02845f1921211f113c0c0.tar.gz
nixpkgs-d273db48c6e95d6547f02845f1921211f113c0c0.tar.bz2
nixpkgs-d273db48c6e95d6547f02845f1921211f113c0c0.tar.lz
nixpkgs-d273db48c6e95d6547f02845f1921211f113c0c0.tar.xz
nixpkgs-d273db48c6e95d6547f02845f1921211f113c0c0.tar.zst
nixpkgs-d273db48c6e95d6547f02845f1921211f113c0c0.zip
nixos/shells: avoid overriding the environment for child shells
A shared exported guard `__NIXOS_SET_ENVIRONMENT_DONE` is introduced that can
be used to prevent child shells from sourcing `system.build.setEnvironment`
the second time.

This fixes e.g. `nix run derivation` when run from e.g. ZSH through the console or
ssh. Before this Bash would resource the common environment resetting the `PATH`
environment variable.

We also export `system.build.setEnvironment` to `/etc/set-environment` making it
easy to reset the common environment with `. /etc/set-environment` when
needed and to grep for environment variables in `/etc` (which was the
motivation of #30418).

This reverts changes made in b00a3fc6fd82834114771f2115a2b032f0ebfe29
(the original #30418).
Diffstat (limited to 'nixos')
-rw-r--r--nixos/modules/config/shells-environment.nix9
-rw-r--r--nixos/modules/programs/bash/bash.nix4
-rw-r--r--nixos/modules/programs/fish.nix4
-rw-r--r--nixos/modules/programs/zsh/zsh.nix4
4 files changed, 18 insertions, 3 deletions
diff --git a/nixos/modules/config/shells-environment.nix b/nixos/modules/config/shells-environment.nix
index 9dc4749b08d..41b1b32768f 100644
--- a/nixos/modules/config/shells-environment.nix
+++ b/nixos/modules/config/shells-environment.nix
@@ -162,8 +162,17 @@ in
         /bin/sh
       '';
 
+    # For resetting environment with `. /etc/set-environment` when needed
+    # and discoverability (see motivation of #30418).
+    environment.etc."set-environment".source = config.system.build.setEnvironment;
+
     system.build.setEnvironment = pkgs.writeText "set-environment"
        ''
+         # DO NOT EDIT -- this file has been generated automatically.
+
+         # Prevent this file from being sourced by child shells.
+         export __NIXOS_SET_ENVIRONMENT_DONE=1
+
          ${exportedEnvVars}
 
          ${cfg.extraInit}
diff --git a/nixos/modules/programs/bash/bash.nix b/nixos/modules/programs/bash/bash.nix
index 69a1a482d07..e5f5c8d0943 100644
--- a/nixos/modules/programs/bash/bash.nix
+++ b/nixos/modules/programs/bash/bash.nix
@@ -126,7 +126,9 @@ in
     programs.bash = {
 
       shellInit = ''
-        ${config.system.build.setEnvironment.text}
+        if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]; then
+            . ${config.system.build.setEnvironment}
+        fi
 
         ${cfge.shellInit}
       '';
diff --git a/nixos/modules/programs/fish.nix b/nixos/modules/programs/fish.nix
index c8d94a47be2..40b3ff37289 100644
--- a/nixos/modules/programs/fish.nix
+++ b/nixos/modules/programs/fish.nix
@@ -109,7 +109,9 @@ in
       set fish_function_path ${pkgs.fish-foreign-env}/share/fish-foreign-env/functions $__fish_datadir/functions
       
       # source the NixOS environment config
-      fenv source ${config.system.build.setEnvironment}
+      if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]
+          fenv source ${config.system.build.setEnvironment}
+      end
 
       # clear fish_function_path so that it will be correctly set when we return to $__fish_datadir/config.fish
       set -e fish_function_path
diff --git a/nixos/modules/programs/zsh/zsh.nix b/nixos/modules/programs/zsh/zsh.nix
index 42d4e1d4ada..c6ab637160d 100644
--- a/nixos/modules/programs/zsh/zsh.nix
+++ b/nixos/modules/programs/zsh/zsh.nix
@@ -103,7 +103,9 @@ in
         if [ -n "$__ETC_ZSHENV_SOURCED" ]; then return; fi
         export __ETC_ZSHENV_SOURCED=1
 
-        ${config.system.build.setEnvironment.text}
+        if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]; then
+            . ${config.system.build.setEnvironment}
+        fi
 
         ${cfge.shellInit}