wrapper-manager-fds/modules: add suffix and prefix for setting envvars

This commit is contained in:
Gabriel Arazas 2024-08-03 16:45:48 +08:00
parent e63db1620e
commit f5dca27dce
2 changed files with 35 additions and 17 deletions

View File

@ -8,7 +8,7 @@
let let
envConfig = config; envConfig = config;
toStringType = with lib.types; coercedTo anything (x: builtins.toString x) str; toStringType = with lib.types; coercedTo str (x: "${x}") str;
envSubmodule = envSubmodule =
{ {
config, config,
@ -23,6 +23,8 @@ let
"unset" "unset"
"set" "set"
"set-default" "set-default"
"prefix"
"suffix"
]; ];
description = '' description = ''
Sets the appropriate action for the environment variable. Sets the appropriate action for the environment variable.
@ -37,15 +39,29 @@ let
}; };
value = lib.mkOption { value = lib.mkOption {
type = toStringType; type = with lib.types; either toStringType (listOf toStringType);
description = '' description = ''
The value of the variable that is holding. The value of the variable that is holding.
::: {.note}
It accepts a list of values only for `prefix` and `suffix` action.
:::
''; '';
example = "HELLO THERE"; example = "HELLO THERE";
}; };
isEscaped = lib.mkEnableOption "escaping of the value" // { separator = lib.mkOption {
default = true; type = lib.types.str;
description = ''
Separator used to create a character-delimited list of the
environment variable holding a list of values.
::: {.note}
Only used for `prefix` and `suffix` action.
:::
'';
default = ":";
example = ";";
}; };
}; };
}; };
@ -142,19 +158,15 @@ let
pathAdd = envConfig.environment.pathAdd; pathAdd = envConfig.environment.pathAdd;
makeWrapperArgs = makeWrapperArgs =
[ lib.mapAttrsToList (
"--argv0"
config.arg0
]
++ (lib.mapAttrsToList (
n: v: n: v:
if v.action == "unset" then if v.action == "unset" then
"--${v.action} ${lib.escapeShellArg n}" "--${v.action} ${lib.escapeShellArg n}"
else if lib.elem v.action [ "prefix" "suffix" ] then
"--${v.action} ${lib.escapeShellArg n} ${lib.escapeShellArg v.separator} ${lib.escapeShellArg (lib.concatStringsSep v.separator v.value)}"
else else
"--${v.action} ${lib.escapeShellArg n} ${ "--${v.action} ${lib.escapeShellArg n} ${lib.escapeShellArg v.value}"
if v.isEscaped then lib.escapeShellArg v.value else v.value ) config.env
}"
) config.env)
++ (builtins.map (v: "--add-flags ${lib.escapeShellArg v}") config.prependArgs) ++ (builtins.map (v: "--add-flags ${lib.escapeShellArg v}") config.prependArgs)
++ (builtins.map (v: "--append-flags ${lib.escapeShellArg v}") config.appendArgs) ++ (builtins.map (v: "--append-flags ${lib.escapeShellArg v}") config.appendArgs)
++ (lib.optionals (!envConfig.build.isBinary && config.preScript != "") ( ++ (lib.optionals (!envConfig.build.isBinary && config.preScript != "") (
@ -167,10 +179,14 @@ let
"--run" "--run"
preScript preScript
] ]
)); ))
++ [ "--inherit-argv0" ];
} }
(lib.mkIf (config.pathAdd != [ ]) { env.PATH.value = lib.concatStringsSep ":" config.pathAdd; }) (lib.mkIf (config.pathAdd != [ ]) {
env.PATH.value = lib.lists.map builtins.toString config.pathAdd;
env.PATH.action = "prefix";
})
]; ];
}; };
in in

View File

@ -59,11 +59,13 @@ in
} }
(lib.mkIf (config.xdg.configDirs != [ ]) { (lib.mkIf (config.xdg.configDirs != [ ]) {
env.XDG_CONFIG_DIRS.value = lib.concatStringsSep ":" config.xdg.configDirs; env.XDG_CONFIG_DIRS.value = lib.lists.map builtins.toString config.xdg.configDirs;
env.XDG_CONFIG_DIRS.action = "prefix";
}) })
(lib.mkIf (config.xdg.dataDirs != [ ]) { (lib.mkIf (config.xdg.dataDirs != [ ]) {
env.XDG_DATA_DIRS.value = lib.concatStringsSep ":" config.xdg.dataDirs; env.XDG_DATA_DIRS.value = lib.lists.map builtins.toString config.xdg.dataDirs;
env.XDG_DATA_DIRS.action = "prefix";
}) })
]; ];
}; };