From f5dca27dcebdb4229549e157b91645e20c7ba743 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Sat, 3 Aug 2024 16:45:48 +0800 Subject: [PATCH] wrapper-manager-fds/modules: add suffix and prefix for setting envvars --- modules/wrapper-manager/base.nix | 46 +++++++++++++++++++--------- modules/wrapper-manager/xdg-dirs.nix | 6 ++-- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/modules/wrapper-manager/base.nix b/modules/wrapper-manager/base.nix index 5c9a255..7b63a87 100644 --- a/modules/wrapper-manager/base.nix +++ b/modules/wrapper-manager/base.nix @@ -8,7 +8,7 @@ let envConfig = config; - toStringType = with lib.types; coercedTo anything (x: builtins.toString x) str; + toStringType = with lib.types; coercedTo str (x: "${x}") str; envSubmodule = { config, @@ -23,6 +23,8 @@ let "unset" "set" "set-default" + "prefix" + "suffix" ]; description = '' Sets the appropriate action for the environment variable. @@ -37,15 +39,29 @@ let }; value = lib.mkOption { - type = toStringType; + type = with lib.types; either toStringType (listOf toStringType); description = '' The value of the variable that is holding. + + ::: {.note} + It accepts a list of values only for `prefix` and `suffix` action. + ::: ''; example = "HELLO THERE"; }; - isEscaped = lib.mkEnableOption "escaping of the value" // { - default = true; + separator = lib.mkOption { + 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; makeWrapperArgs = - [ - "--argv0" - config.arg0 - ] - ++ (lib.mapAttrsToList ( + lib.mapAttrsToList ( n: v: if v.action == "unset" then "--${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 - "--${v.action} ${lib.escapeShellArg n} ${ - if v.isEscaped then lib.escapeShellArg v.value else v.value - }" - ) config.env) + "--${v.action} ${lib.escapeShellArg n} ${lib.escapeShellArg v.value}" + ) config.env ++ (builtins.map (v: "--add-flags ${lib.escapeShellArg v}") config.prependArgs) ++ (builtins.map (v: "--append-flags ${lib.escapeShellArg v}") config.appendArgs) ++ (lib.optionals (!envConfig.build.isBinary && config.preScript != "") ( @@ -167,10 +179,14 @@ let "--run" 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 diff --git a/modules/wrapper-manager/xdg-dirs.nix b/modules/wrapper-manager/xdg-dirs.nix index 3557533..80913d9 100644 --- a/modules/wrapper-manager/xdg-dirs.nix +++ b/modules/wrapper-manager/xdg-dirs.nix @@ -59,11 +59,13 @@ in } (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 != [ ]) { - 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"; }) ]; };