diff --git a/modules/environment/default.nix b/modules/environment/default.nix index 6d77cd63..8ba0735e 100644 --- a/modules/environment/default.nix +++ b/modules/environment/default.nix @@ -184,6 +184,10 @@ in { }; system.build.setEnvironment = pkgs.writeText "set-environment" '' + # Prevent this file from being sourced by child shells. + export __NIX_DARWIN_SET_ENVIRONMENT_DONE=1 + + export PATH=${config.environment.systemPath} ${concatStringsSep "\n" exportVariables} # Extra initialisation diff --git a/modules/programs/bash/default.nix b/modules/programs/bash/default.nix index ea5afe26..6f5c8e7b 100644 --- a/modules/programs/bash/default.nix +++ b/modules/programs/bash/default.nix @@ -61,8 +61,9 @@ in # Don't execute this file when running in a pure nix-shell. if test -n "$IN_NIX_SHELL"; then return; fi - export PATH=${config.environment.systemPath} - ${config.system.build.setEnvironment.text} + if [ -z "$__NIX_DARWIN_SET_ENVIRONMENT_DONE" ]; then + . ${config.system.build.setEnvironment} + fi # Return early if not running interactively, but after basic nix setup. [[ $- != *i* ]] && return diff --git a/modules/programs/fish.nix b/modules/programs/fish.nix index 6ee53550..470dcace 100644 --- a/modules/programs/fish.nix +++ b/modules/programs/fish.nix @@ -109,8 +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 export PATH=${config.environment.systemPath} - fenv source ${config.system.build.setEnvironment} + if [ -z "$__NIX_DARWIN_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/modules/programs/zsh/default.nix b/modules/programs/zsh/default.nix index de904338..4d5559ae 100644 --- a/modules/programs/zsh/default.nix +++ b/modules/programs/zsh/default.nix @@ -120,8 +120,9 @@ in # Don't execute this file when running in a pure nix-shell. if test -n "$IN_NIX_SHELL"; then return; fi - export PATH=${config.environment.systemPath} - ${config.system.build.setEnvironment.text} + if [ -z "$__NIX_DARWIN_SET_ENVIRONMENT_DONE" ]; then + . ${config.system.build.setEnvironment} + fi ${cfg.shellInit} @@ -158,9 +159,6 @@ in if [ -n "$__ETC_ZSHRC_SOURCED" -o -n "$NOSYSZSHRC" ]; then return; fi __ETC_ZSHRC_SOURCED=1 - # Also set to fix `nix run` shells. - __ETC_BASHRC_SOURCED=1 - # history defaults SAVEHIST=2000 HISTSIZE=2000 diff --git a/release.nix b/release.nix index 6721a52a..a7016b71 100644 --- a/release.nix +++ b/release.nix @@ -108,10 +108,11 @@ let tests.services-synergy = makeTest ./tests/services-synergy.nix; tests.services-privoxy = makeTest ./tests/services-privoxy.nix; tests.system-defaults-write = makeTest ./tests/system-defaults-write.nix; + tests.system-environment-bash = makeTest ./tests/system-environment-bash.nix; + tests.system-environment-fish = makeTest ./tests/system-environment-fish.nix; tests.system-keyboard-mapping = makeTest ./tests/system-keyboard-mapping.nix; tests.system-packages = makeTest ./tests/system-packages.nix; - tests.system-path-bash = makeTest ./tests/system-path-bash.nix; - tests.system-path-fish = makeTest ./tests/system-path-fish.nix; + tests.system-path = makeTest ./tests/system-path.nix; tests.system-shells = makeTest ./tests/system-shells.nix; tests.users-groups = makeTest ./tests/users-groups.nix; tests.fonts = makeTest ./tests/fonts.nix; diff --git a/tests/environment-path.nix b/tests/environment-path.nix index d0f3d849..d347c7de 100644 --- a/tests/environment-path.nix +++ b/tests/environment-path.nix @@ -3,13 +3,11 @@ with lib; { - programs.bash.enable = true; - test = '' - echo checking /run/current-system/sw/bin in systemPath >&2 - grep 'export PATH=.*:/run/current-system/sw/bin' ${config.out}/etc/bashrc + echo checking /run/current-system/sw/bin in setEnvironment >&2 + grep 'export PATH=.*:/run/current-system/sw/bin' ${config.system.build.setEnvironment} - echo checking /bin and /sbin in systemPath >&2 - grep 'export PATH=.*:/usr/bin:/usr/sbin:/bin:/sbin' ${config.out}/etc/bashrc + echo checking /bin and /sbin in setEnvironment >&2 + grep 'export PATH=.*:/usr/bin:/usr/sbin:/bin:/sbin' ${config.system.build.setEnvironment} ''; } diff --git a/tests/programs-zsh.nix b/tests/programs-zsh.nix index 97afe47b..af1b3dd0 100644 --- a/tests/programs-zsh.nix +++ b/tests/programs-zsh.nix @@ -20,10 +20,10 @@ echo >&2 "checking for share/zsh in /sw" test -e ${config.out}/sw/share/zsh - echo >&2 "checking environment.systemPath in /etc/zshenv" - grep 'export PATH=${pkgs.hello}/bin' ${config.out}/etc/zshenv - echo >&2 "checking SHELL in /etc/zshenv" - grep 'export SHELL="${pkgs.zsh}/bin/zsh"' ${config.out}/etc/zshenv + echo >&2 "checking setEnvironment in /etc/zshenv" + fgrep '. ${config.system.build.setEnvironment}' ${config.out}/etc/zshenv + echo >&2 "checking SHELL in setEnvironment" + grep 'export SHELL="${pkgs.zsh}/bin/zsh"' ${config.system.build.setEnvironment} echo >&2 "checking nix-shell return /etc/zshenv" grep 'if test -n "$IN_NIX_SHELL"; then return; fi' ${config.out}/etc/zshenv echo >&2 "checking zshenv.d in /etc/zshenv" diff --git a/tests/services-nix-daemon.nix b/tests/services-nix-daemon.nix index 115ff446..6ca46dca 100644 --- a/tests/services-nix-daemon.nix +++ b/tests/services-nix-daemon.nix @@ -8,8 +8,6 @@ in services.nix-daemon.enable = true; nix.package = nix; - programs.zsh.enable = true; - test = '' echo checking nix-daemon service in /Library/LaunchDaemons >&2 grep "org.nixos.nix-daemon" ${config.out}/Library/LaunchDaemons/org.nixos.nix-daemon.plist @@ -20,9 +18,7 @@ in echo checking nix-daemon reload in /activate >&2 grep "pkill -HUP nix-daemon" ${config.out}/activate - echo checking NIX_REMOTE=daemon in /etc/bashrc >&2 - grep "NIX_REMOTE=daemon" ${config.out}/etc/bashrc - echo "checking NIX_REMOTE=daemon in /etc/zshenv" >&2 - grep 'export NIX_REMOTE=daemon' ${config.out}/etc/zshenv + echo checking NIX_REMOTE=daemon in setEnvironment >&2 + grep "NIX_REMOTE=daemon" ${config.system.build.setEnvironment} ''; } diff --git a/tests/system-environment-bash.nix b/tests/system-environment-bash.nix new file mode 100644 index 00000000..158bdbf9 --- /dev/null +++ b/tests/system-environment-bash.nix @@ -0,0 +1,10 @@ +{ config, pkgs, ... }: + +{ + programs.bash.enable = true; + + test = '' + echo checking setEnvironment in /etc/bashrc >&2 + fgrep '. ${config.system.build.setEnvironment}' ${config.out}/etc/bashrc + ''; +} diff --git a/tests/system-environment-fish.nix b/tests/system-environment-fish.nix new file mode 100644 index 00000000..e1873b35 --- /dev/null +++ b/tests/system-environment-fish.nix @@ -0,0 +1,10 @@ +{ config, pkgs, ... }: + +{ + programs.fish.enable = true; + + test = '' + echo checking setEnvironment in /etc/fish/config.fish >&2 + grep 'fenv source ${config.system.build.setEnvironment}' ${config.out}/etc/fish/nixos-env-preinit.fish + ''; +} diff --git a/tests/system-path-bash.nix b/tests/system-path-bash.nix deleted file mode 100644 index b70fdc9e..00000000 --- a/tests/system-path-bash.nix +++ /dev/null @@ -1,12 +0,0 @@ -{ config, pkgs, ... }: - -{ - environment.systemPath = [ pkgs.hello ]; - - programs.bash.enable = true; - - test = '' - echo checking systemPath in /etc/bashrc >&2 - grep 'export PATH=${pkgs.hello}/bin' ${config.out}/etc/bashrc - ''; -} diff --git a/tests/system-path-fish.nix b/tests/system-path-fish.nix deleted file mode 100644 index 9b6c46e6..00000000 --- a/tests/system-path-fish.nix +++ /dev/null @@ -1,12 +0,0 @@ -{ config, pkgs, ... }: - -{ - environment.systemPath = [ pkgs.hello ]; - - programs.fish.enable = true; - - test = '' - echo checking systemPath in /etc/fish/config.fish >&2 - grep 'fenv export PATH=${pkgs.hello}/bin' ${config.out}/etc/fish/nixos-env-preinit.fish - ''; -} diff --git a/tests/system-path.nix b/tests/system-path.nix new file mode 100644 index 00000000..9b8e315c --- /dev/null +++ b/tests/system-path.nix @@ -0,0 +1,10 @@ +{ config, pkgs, ... }: + +{ + environment.systemPath = [ pkgs.hello ]; + + test = '' + echo checking systemPath in setEnvironment >&2 + grep 'export PATH=${pkgs.hello}/bin' ${config.system.build.setEnvironment} + ''; +}