From 664086fa4c9eb05bbd986cc4d6f41325efa1491b Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Thu, 19 Oct 2023 01:02:01 +0100 Subject: [PATCH] lib/shell: add appendToVar function --- modules/home-environment.nix | 5 +++-- modules/lib/shell.nix | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/modules/home-environment.nix b/modules/home-environment.nix index 69d04a124..3533ea2c4 100644 --- a/modules/home-environment.nix +++ b/modules/home-environment.nix @@ -579,8 +579,9 @@ in ${config.lib.shell.exportAll cfg.sessionVariables} '' + concatStringsSep "\n" (mapAttrsToList - (env: values: '' - export ${env}="''$${env}''${${env}:+:}${concatStringsSep ":" values}"'') + (env: values: config.lib.shell.export + env + (config.lib.shell.appendToVar ":" env values)) cfg.sessionSearchVariables) + cfg.sessionVariablesExtra; }; diff --git a/modules/lib/shell.nix b/modules/lib/shell.nix index 5e5743f51..cdcfdefa5 100644 --- a/modules/lib/shell.nix +++ b/modules/lib/shell.nix @@ -1,6 +1,14 @@ { lib }: rec { + # Produces a Bourne shell like statement that appends new values to + # an possibly existing variable, using sep(ator). + # Example: + # appendToVar ":" "PATH" [ "$HOME/bin" "$HOME/.local/bin" ] + # => "$PATH\${PATH:+:}$HOME/bin:$HOME/.local/bin" + appendToVar = sep: n: v: + "\$${n}\${${n}:+${sep}}${lib.concatStringsSep sep v}"; + # Produces a Bourne shell like variable export statement. export = n: v: ''export ${n}="${toString v}"'';