From 7d4f510f26444923741ab9554d1402f040978f62 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Wed, 18 Oct 2023 21:46:54 +0100 Subject: [PATCH] home-environment: add home.sessionSearchVariables This commit introduces `home.sessionSearchVariables` option, that is created to be a "generic" version of `home.sessionPath` for any environment variables that is similar to PATH (e.g.: MANPATH). This allows composition of those variables between multiple modules, avoiding issues like this one: https://github.com/nix-community/home-manager/pull/4579/files#r1364374048 This commit also reimplements `home.sessionPath` as terms of `home.sessionSearchVariables`, to reduce code duplication and show that the code is correct. --- modules/home-environment.nix | 34 +++++++++++++++++-- tests/modules/home-environment/default.nix | 3 +- .../session-search-variables.nix | 15 ++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 tests/modules/home-environment/session-search-variables.nix diff --git a/modules/home-environment.nix b/modules/home-environment.nix index 870464608..69d04a124 100644 --- a/modules/home-environment.nix +++ b/modules/home-environment.nix @@ -319,6 +319,28 @@ in ''; }; + home.sessionSearchVariables = mkOption { + default = { }; + type = with types; attrsOf (listOf str); + example = { + MANPATH = [ + "$HOME/.npm-packages/man" + "\${xdg.configHome}/.local/share/man" + ]; + }; + description = '' + Extra directories to add to arbitrary PATH-like environment + variables (e.g.: {env}`MANPATH`). The values will be concatenated + by `:`. + + These directories are added to the environment variable in a + double-quoted context, so expressions like `$HOME` are + expanded by the shell. However, since expressions like `~` or + `*` are escaped, they will end up in the environment + verbatim. + ''; + }; + home.sessionVariablesExtra = mkOption { type = types.lines; default = ""; @@ -555,11 +577,17 @@ in export __HM_SESS_VARS_SOURCED=1 ${config.lib.shell.exportAll cfg.sessionVariables} - '' + lib.optionalString (cfg.sessionPath != [ ]) '' - export PATH="$PATH''${PATH:+:}${concatStringsSep ":" cfg.sessionPath}" - '' + cfg.sessionVariablesExtra; + '' + concatStringsSep "\n" + (mapAttrsToList + (env: values: '' + export ${env}="''$${env}''${${env}:+:}${concatStringsSep ":" values}"'') + cfg.sessionSearchVariables) + + cfg.sessionVariablesExtra; }; + home.sessionSearchVariables.PATH = + mkIf (cfg.sessionPath != [ ]) cfg.sessionPath; + home.packages = [ config.home.sessionVariablesPackage ]; # A dummy entry acting as a boundary between the activation diff --git a/tests/modules/home-environment/default.nix b/tests/modules/home-environment/default.nix index e76e248a1..c38ad5ce5 100644 --- a/tests/modules/home-environment/default.nix +++ b/tests/modules/home-environment/default.nix @@ -1,4 +1,5 @@ { - home-session-variables = ./session-variables.nix; home-session-path = ./session-path.nix; + home-session-search-variables = ./session-search-variables.nix; + home-session-variables = ./session-variables.nix; } diff --git a/tests/modules/home-environment/session-search-variables.nix b/tests/modules/home-environment/session-search-variables.nix new file mode 100644 index 000000000..c260e6fee --- /dev/null +++ b/tests/modules/home-environment/session-search-variables.nix @@ -0,0 +1,15 @@ +{ config, lib, pkgs, ... }: + +{ + imports = [ + ({ ... }: { config.home.sessionSearchVariables.TEST = [ "foo" ]; }) + ({ ... }: { config.home.sessionSearchVariables.TEST = [ "bar" "baz" ]; }) + ]; + + nmt.script = '' + hmSessVars=home-path/etc/profile.d/hm-session-vars.sh + assertFileExists $hmSessVars + assertFileContains $hmSessVars \ + 'export TEST="$TEST''${TEST:+:}bar:baz:foo"' + ''; +}