diff --git a/modules/programs/nushell.nix b/modules/programs/nushell.nix index b69ec88f0..6586d1e1a 100644 --- a/modules/programs/nushell.nix +++ b/modules/programs/nushell.nix @@ -145,11 +145,24 @@ in { }; environmentVariables = mkOption { - type = types.attrsOf types.str; + type = types.attrsOf hm.types.nushellValue; default = { }; - example = { FOO = "BAR"; }; + example = literalExpression '' + { + FOO = "BAR"; + LIST_VALUE = [ "foo" "bar" ]; + NU_LIB_DIRS = lib.concatStringsSep ":" [ ./scripts ]; + PROMPT_COMMAND = lib.hm.nushell.mkNushellInline '''{|| "> "}'''; + ENV_CONVERSIONS.PATH = { + from_string = lib.hm.nushell.mkNushellInline "{|s| $s | split row (char esep) }"; + to_string = lib.hm.nushell.mkNushellInline "{|v| $v | str join (char esep) }"; + }; + } + ''; description = '' - An attribute set that maps an environment variable to a shell interpreted string. + Environment variables to be set. + + Inline values can be set with `lib.hm.nushell.mkNushellInline`. ''; }; }; @@ -173,9 +186,11 @@ 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 = '' + load-env ${hm.nushell.toNushell { } cfg.environmentVariables} + ''; + in mkIf (cfg.envFile != null || cfg.extraEnv != "" || hasEnvVars) { "${configDir}/env.nu".text = mkMerge [ (mkIf (cfg.envFile != null) cfg.envFile.text) cfg.extraEnv diff --git a/tests/modules/programs/nushell/env-expected.nu b/tests/modules/programs/nushell/env-expected.nu index 07105fc54..50f6f767b 100644 --- a/tests/modules/programs/nushell/env-expected.nu +++ b/tests/modules/programs/nushell/env-expected.nu @@ -1,4 +1,17 @@ $env.FOO = 'BAR' -$env.BAR = $'(echo BAZ)' \ No newline at end of file +load-env { + "ENV_CONVERSIONS": { + "PATH": { + "from_string": ({|s| $s | split row (char esep) }) + "to_string": ({|v| $v | str join (char esep) }) + } + } + "FOO": "BAR" + "LIST_VALUE": [ + "foo" + "bar" + ] + "PROMPT_COMMAND": ({|| "> "}) +} diff --git a/tests/modules/programs/nushell/example-settings.nix b/tests/modules/programs/nushell/example-settings.nix index d870eb9db..4fc1402a4 100644 --- a/tests/modules/programs/nushell/example-settings.nix +++ b/tests/modules/programs/nushell/example-settings.nix @@ -1,4 +1,4 @@ -{ pkgs, config, ... }: +{ pkgs, config, lib, ... }: { programs.nushell = { @@ -28,7 +28,17 @@ "ll" = "ls -a"; }; - environmentVariables = { BAR = "$'(echo BAZ)'"; }; + environmentVariables = { + FOO = "BAR"; + LIST_VALUE = [ "foo" "bar" ]; + PROMPT_COMMAND = lib.hm.nushell.mkNushellInline ''{|| "> "}''; + ENV_CONVERSIONS.PATH = { + from_string = + lib.hm.nushell.mkNushellInline "{|s| $s | split row (char esep) }"; + to_string = + lib.hm.nushell.mkNushellInline "{|v| $v | str join (char esep) }"; + }; + }; }; test.stubs.nushell = { };