From 61475c4779cd0327919ecc2e6f45df08a403d73c Mon Sep 17 00:00:00 2001 From: Robert Helgesson Date: Wed, 3 Feb 2021 23:24:43 +0100 Subject: [PATCH] experiment --- modules/home-environment.nix | 2 +- modules/lib/shell.nix | 17 +++++++++++++++++ .../session-variables-explicit.nix | 8 ++++---- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/modules/home-environment.nix b/modules/home-environment.nix index 85d18f63a..4e437ce9c 100644 --- a/modules/home-environment.nix +++ b/modules/home-environment.nix @@ -479,7 +479,7 @@ in if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi export __HM_SESS_VARS_SOURCED=1 - ${config.lib.shell.exportAll cfg.sessionVariables} + ${config.lib.shell.exportAll' { colonVars = ["NIX_PATH" "PATH"]; } cfg.sessionVariables} '' + lib.optionalString (cfg.sessionPath != [ ]) '' export PATH="$PATH''${PATH:+:}${concatStringsSep ":" cfg.sessionPath}" '' + cfg.sessionVariablesExtra; diff --git a/modules/lib/shell.nix b/modules/lib/shell.nix index 5e5743f51..cba97f476 100644 --- a/modules/lib/shell.nix +++ b/modules/lib/shell.nix @@ -4,8 +4,25 @@ rec { # Produces a Bourne shell like variable export statement. export = n: v: ''export ${n}="${toString v}"''; + export' = { colonVars ? [ ] }: + n: v: + let + replaceMatch = match: + lib.replaceStrings [ ":\$${match}:" ":\$${match}" "\$${match}:" ] [ + "\${${match}:+:\$${match}:}" + "\${${match}:+:\$${match}}" + "\${${match}:+\$${match}:}" + ]; + + mkValue = n: v: + if builtins.elem n colonVars then replaceMatch n v else toString v; + in ''export ${n}="${mkValue n v}"''; + # Given an attribute set containing shell variable names and their # assignment, this function produces a string containing an export # statement for each set entry. exportAll = vars: lib.concatStringsSep "\n" (lib.mapAttrsToList export vars); + + exportAll' = opts: vars: + lib.concatStringsSep "\n" (lib.mapAttrsToList (export' opts) vars); } diff --git a/tests/modules/home-environment/session-variables-explicit.nix b/tests/modules/home-environment/session-variables-explicit.nix index 8ee540716..2e74b4dac 100644 --- a/tests/modules/home-environment/session-variables-explicit.nix +++ b/tests/modules/home-environment/session-variables-explicit.nix @@ -13,8 +13,8 @@ let export __HM_SESS_VARS_SOURCED=1 export LOCALE_ARCHIVE_2_27="${pkgs.glibcLocales}/lib/locale/locale-archive" - export NIX_PATH="testpath=$HOME/testpath:$NIX_PATH" - export PATH="$PATH:$HOME/bin" + export NIX_PATH="testpath=$HOME/testpath''${NIX_PATH:+:$NIX_PATH}" + export PATH="''${PATH:+$PATH:}$HOME/bin" export XDG_CACHE_HOME="/home/hm-user/.cache" export XDG_CONFIG_HOME="/home/hm-user/.config" export XDG_DATA_HOME="/home/hm-user/.local/share" @@ -25,8 +25,8 @@ let if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi export __HM_SESS_VARS_SOURCED=1 - export NIX_PATH="testpath=$HOME/testpath:$NIX_PATH" - export PATH="$PATH:$HOME/bin" + export NIX_PATH="testpath=$HOME/testpath''${NIX_PATH:+:$NIX_PATH}" + export PATH="''${PATH:+$PATH:}$HOME/bin" export XDG_CACHE_HOME="/home/hm-user/.cache" export XDG_CONFIG_HOME="/home/hm-user/.config" export XDG_DATA_HOME="/home/hm-user/.local/share"