1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2024-12-14 11:57:34 +00:00

Unify environment configuration and don't run in child shells

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.
This commit is contained in:
Andrew Childs 2018-10-25 00:30:34 +09:00
parent e6a698a701
commit 676ef10377
13 changed files with 56 additions and 51 deletions

View file

@ -184,6 +184,10 @@ in {
}; };
system.build.setEnvironment = pkgs.writeText "set-environment" '' 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} ${concatStringsSep "\n" exportVariables}
# Extra initialisation # Extra initialisation

View file

@ -61,8 +61,9 @@ in
# Don't execute this file when running in a pure nix-shell. # Don't execute this file when running in a pure nix-shell.
if test -n "$IN_NIX_SHELL"; then return; fi if test -n "$IN_NIX_SHELL"; then return; fi
export PATH=${config.environment.systemPath} if [ -z "$__NIX_DARWIN_SET_ENVIRONMENT_DONE" ]; then
${config.system.build.setEnvironment.text} . ${config.system.build.setEnvironment}
fi
# Return early if not running interactively, but after basic nix setup. # Return early if not running interactively, but after basic nix setup.
[[ $- != *i* ]] && return [[ $- != *i* ]] && return

View file

@ -109,8 +109,9 @@ in
set fish_function_path ${pkgs.fish-foreign-env}/share/fish-foreign-env/functions $__fish_datadir/functions set fish_function_path ${pkgs.fish-foreign-env}/share/fish-foreign-env/functions $__fish_datadir/functions
# source the NixOS environment config # source the NixOS environment config
fenv export PATH=${config.environment.systemPath} if [ -z "$__NIX_DARWIN_SET_ENVIRONMENT_DONE" ]
fenv source ${config.system.build.setEnvironment} 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 # clear fish_function_path so that it will be correctly set when we return to $__fish_datadir/config.fish
set -e fish_function_path set -e fish_function_path

View file

@ -120,8 +120,9 @@ in
# Don't execute this file when running in a pure nix-shell. # Don't execute this file when running in a pure nix-shell.
if test -n "$IN_NIX_SHELL"; then return; fi if test -n "$IN_NIX_SHELL"; then return; fi
export PATH=${config.environment.systemPath} if [ -z "$__NIX_DARWIN_SET_ENVIRONMENT_DONE" ]; then
${config.system.build.setEnvironment.text} . ${config.system.build.setEnvironment}
fi
${cfg.shellInit} ${cfg.shellInit}
@ -158,9 +159,6 @@ in
if [ -n "$__ETC_ZSHRC_SOURCED" -o -n "$NOSYSZSHRC" ]; then return; fi if [ -n "$__ETC_ZSHRC_SOURCED" -o -n "$NOSYSZSHRC" ]; then return; fi
__ETC_ZSHRC_SOURCED=1 __ETC_ZSHRC_SOURCED=1
# Also set to fix `nix run` shells.
__ETC_BASHRC_SOURCED=1
# history defaults # history defaults
SAVEHIST=2000 SAVEHIST=2000
HISTSIZE=2000 HISTSIZE=2000

View file

@ -108,10 +108,11 @@ let
tests.services-synergy = makeTest ./tests/services-synergy.nix; tests.services-synergy = makeTest ./tests/services-synergy.nix;
tests.services-privoxy = makeTest ./tests/services-privoxy.nix; tests.services-privoxy = makeTest ./tests/services-privoxy.nix;
tests.system-defaults-write = makeTest ./tests/system-defaults-write.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-keyboard-mapping = makeTest ./tests/system-keyboard-mapping.nix;
tests.system-packages = makeTest ./tests/system-packages.nix; tests.system-packages = makeTest ./tests/system-packages.nix;
tests.system-path-bash = makeTest ./tests/system-path-bash.nix; tests.system-path = makeTest ./tests/system-path.nix;
tests.system-path-fish = makeTest ./tests/system-path-fish.nix;
tests.system-shells = makeTest ./tests/system-shells.nix; tests.system-shells = makeTest ./tests/system-shells.nix;
tests.users-groups = makeTest ./tests/users-groups.nix; tests.users-groups = makeTest ./tests/users-groups.nix;
tests.fonts = makeTest ./tests/fonts.nix; tests.fonts = makeTest ./tests/fonts.nix;

View file

@ -3,13 +3,11 @@
with lib; with lib;
{ {
programs.bash.enable = true;
test = '' test = ''
echo checking /run/current-system/sw/bin in systemPath >&2 echo checking /run/current-system/sw/bin in setEnvironment >&2
grep 'export PATH=.*:/run/current-system/sw/bin' ${config.out}/etc/bashrc grep 'export PATH=.*:/run/current-system/sw/bin' ${config.system.build.setEnvironment}
echo checking /bin and /sbin in systemPath >&2 echo checking /bin and /sbin in setEnvironment >&2
grep 'export PATH=.*:/usr/bin:/usr/sbin:/bin:/sbin' ${config.out}/etc/bashrc grep 'export PATH=.*:/usr/bin:/usr/sbin:/bin:/sbin' ${config.system.build.setEnvironment}
''; '';
} }

View file

@ -20,10 +20,10 @@
echo >&2 "checking for share/zsh in /sw" echo >&2 "checking for share/zsh in /sw"
test -e ${config.out}/sw/share/zsh test -e ${config.out}/sw/share/zsh
echo >&2 "checking environment.systemPath in /etc/zshenv" echo >&2 "checking setEnvironment in /etc/zshenv"
grep 'export PATH=${pkgs.hello}/bin' ${config.out}/etc/zshenv fgrep '. ${config.system.build.setEnvironment}' ${config.out}/etc/zshenv
echo >&2 "checking SHELL in /etc/zshenv" echo >&2 "checking SHELL in setEnvironment"
grep 'export SHELL="${pkgs.zsh}/bin/zsh"' ${config.out}/etc/zshenv grep 'export SHELL="${pkgs.zsh}/bin/zsh"' ${config.system.build.setEnvironment}
echo >&2 "checking nix-shell return /etc/zshenv" echo >&2 "checking nix-shell return /etc/zshenv"
grep 'if test -n "$IN_NIX_SHELL"; then return; fi' ${config.out}/etc/zshenv grep 'if test -n "$IN_NIX_SHELL"; then return; fi' ${config.out}/etc/zshenv
echo >&2 "checking zshenv.d in /etc/zshenv" echo >&2 "checking zshenv.d in /etc/zshenv"

View file

@ -8,8 +8,6 @@ in
services.nix-daemon.enable = true; services.nix-daemon.enable = true;
nix.package = nix; nix.package = nix;
programs.zsh.enable = true;
test = '' test = ''
echo checking nix-daemon service in /Library/LaunchDaemons >&2 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 grep "<string>org.nixos.nix-daemon</string>" ${config.out}/Library/LaunchDaemons/org.nixos.nix-daemon.plist
@ -20,9 +18,7 @@ in
echo checking nix-daemon reload in /activate >&2 echo checking nix-daemon reload in /activate >&2
grep "pkill -HUP nix-daemon" ${config.out}/activate grep "pkill -HUP nix-daemon" ${config.out}/activate
echo checking NIX_REMOTE=daemon in /etc/bashrc >&2 echo checking NIX_REMOTE=daemon in setEnvironment >&2
grep "NIX_REMOTE=daemon" ${config.out}/etc/bashrc grep "NIX_REMOTE=daemon" ${config.system.build.setEnvironment}
echo "checking NIX_REMOTE=daemon in /etc/zshenv" >&2
grep 'export NIX_REMOTE=daemon' ${config.out}/etc/zshenv
''; '';
} }

View file

@ -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
'';
}

View file

@ -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
'';
}

View file

@ -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
'';
}

View file

@ -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
'';
}

10
tests/system-path.nix Normal file
View file

@ -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}
'';
}