nushell: Allow arbitrary env vars

This commit is contained in:
Joaquín Triñanes 2024-09-02 16:13:43 +02:00
parent 471e3eb0a1
commit 1a08dc8f6e
No known key found for this signature in database
GPG key ID: 6E1446DD451C6BAF
3 changed files with 22 additions and 8 deletions

View file

@ -145,11 +145,17 @@ in {
};
environmentVariables = mkOption {
type = types.attrsOf types.str;
type = types.attrsOf (pkgs.formats.json { }).type;
default = { };
example = { FOO = "BAR"; };
example = {
FOO = "BAR";
BOOLEAN_VAR = true;
NUMERIC_VAR = 4;
LIST_VAR = [ "elem1" 2 ];
OBJECT_VAR = { some = "field"; };
};
description = ''
An attribute set that maps an environment variable to a shell interpreted string.
Environment variables that will be set.
'';
};
};
@ -173,9 +179,12 @@ in {
})
(let
envVarsStr = concatStringsSep "\n"
(mapAttrsToList (k: v: "$env.${k} = ${v}") cfg.environmentVariables);
in mkIf (cfg.envFile != null || cfg.extraEnv != "" || envVarsStr != "") {
hasEnvVars = cfg.environmentVariables != { };
envVarsStr = lib.mkIf hasEnvVars ''
${builtins.toJSON cfg.environmentVariables} | load-env
'';
in mkIf (cfg.envFile != null || cfg.extraEnv != "" || hasEnvVars) {
"${configDir}/env.nu".text = mkMerge [
(mkIf (cfg.envFile != null) cfg.envFile.text)
cfg.extraEnv

View file

@ -1,4 +1,4 @@
$env.FOO = 'BAR'
$env.BAR = $'(echo BAZ)'
{"BAR":"$'(echo BAZ)'","BOOLEAN_VAR":true,"LIST_VAR":["elem1",2],"NUMERIC_VAR":4} | load-env

View file

@ -28,7 +28,12 @@
"ll" = "ls -a";
};
environmentVariables = { BAR = "$'(echo BAZ)'"; };
environmentVariables = {
BAR = "$'(echo BAZ)'";
BOOLEAN_VAR = true;
NUMERIC_VAR = 4;
LIST_VAR = [ "elem1" 2 ];
};
};
test.stubs.nushell = { };