1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-23 03:29:45 +01:00

nushell: allow arbitrary environment variables

This commit is contained in:
Joaquín Triñanes 2024-09-14 18:09:15 +02:00 committed by Robert Helgesson
parent edf15f1549
commit 628b15d275
No known key found for this signature in database
GPG key ID: 96E745BD17AA17ED
3 changed files with 47 additions and 9 deletions

View file

@ -145,11 +145,24 @@ in {
}; };
environmentVariables = mkOption { environmentVariables = mkOption {
type = types.attrsOf types.str; type = types.attrsOf hm.types.nushellValue;
default = { }; 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 = '' 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 (let
envVarsStr = concatStringsSep "\n" hasEnvVars = cfg.environmentVariables != { };
(mapAttrsToList (k: v: "$env.${k} = ${v}") cfg.environmentVariables); envVarsStr = ''
in mkIf (cfg.envFile != null || cfg.extraEnv != "" || envVarsStr != "") { load-env ${hm.nushell.toNushell { } cfg.environmentVariables}
'';
in mkIf (cfg.envFile != null || cfg.extraEnv != "" || hasEnvVars) {
"${configDir}/env.nu".text = mkMerge [ "${configDir}/env.nu".text = mkMerge [
(mkIf (cfg.envFile != null) cfg.envFile.text) (mkIf (cfg.envFile != null) cfg.envFile.text)
cfg.extraEnv cfg.extraEnv

View file

@ -1,4 +1,17 @@
$env.FOO = 'BAR' $env.FOO = 'BAR'
$env.BAR = $'(echo BAZ)' 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": ({|| "> "})
}

View file

@ -1,4 +1,4 @@
{ pkgs, config, ... }: { pkgs, config, lib, ... }:
{ {
programs.nushell = { programs.nushell = {
@ -28,7 +28,17 @@
"ll" = "ls -a"; "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 = { }; test.stubs.nushell = { };