Skip to content

Commit

Permalink
Unify environment configuration and don't run in child shells
Browse files Browse the repository at this point in the history
This should enable `nix run` to work under shells like fish and zsh,
as well as making child shells not needlessly reset any environment
that should be inherited.

Implementation adapted from NixOS.
  • Loading branch information
thefloweringash committed Oct 24, 2018
1 parent e6a698a commit 676ef10
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 51 deletions.
4 changes: 4 additions & 0 deletions modules/environment/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions modules/programs/bash/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions modules/programs/fish.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions modules/programs/zsh/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions release.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 4 additions & 6 deletions tests/environment-path.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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}
'';
}
8 changes: 4 additions & 4 deletions tests/programs-zsh.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 2 additions & 6 deletions tests/services-nix-daemon.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<string>org.nixos.nix-daemon</string>" ${config.out}/Library/LaunchDaemons/org.nixos.nix-daemon.plist
Expand All @@ -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}
'';
}
10 changes: 10 additions & 0 deletions tests/system-environment-bash.nix
Original file line number Diff line number Diff line change
@@ -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
'';
}
10 changes: 10 additions & 0 deletions tests/system-environment-fish.nix
Original file line number Diff line number Diff line change
@@ -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
'';
}
12 changes: 0 additions & 12 deletions tests/system-path-bash.nix

This file was deleted.

12 changes: 0 additions & 12 deletions tests/system-path-fish.nix

This file was deleted.

10 changes: 10 additions & 0 deletions tests/system-path.nix
Original file line number Diff line number Diff line change
@@ -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}
'';
}

0 comments on commit 676ef10

Please sign in to comment.