fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Ytnushell: deprecation of let-env (#4292)8c731978

master
5.1 KiB193 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.nushell;
8
9  configDir = if pkgs.stdenv.isDarwin then
10    "Library/Application Support/nushell"
11  else
12    "${config.xdg.configHome}/nushell";
13
14  linesOrSource = name:
15    types.submodule ({ config, ... }: {
16      options = {
17        text = mkOption {
18          type = types.lines;
19          default = if config.source != null then
20            builtins.readFile config.source
21          else
22            "";
23          defaultText = literalExpression
24            "if source is defined, the content of source, otherwise empty";
25          description = ''
26            Text of the nushell {file}`${name}` file.
27            If unset then the source option will be preferred.
28          '';
29        };
30
31        source = mkOption {
32          type = types.nullOr types.path;
33          default = null;
34          description = ''
35            Path of the nushell {file}`${name}` file to use.
36            If the text option is set, it will be preferred.
37          '';
38        };
39      };
40    });
41in {
42  meta.maintainers = [ maintainers.Philipp-M ];
43
44  imports = [
45    (mkRemovedOptionModule [ "programs" "nushell" "settings" ] ''
46      Please use
47
48        'programs.nushell.configFile' and 'programs.nushell.envFile'
49
50      instead.
51    '')
52  ];
53
54  options.programs.nushell = {
55    enable = mkEnableOption "nushell";
56
57    package = mkOption {
58      type = types.package;
59      default = pkgs.nushell;
60      defaultText = literalExpression "pkgs.nushell";
61      description = "The package to use for nushell.";
62    };
63
64    configFile = mkOption {
65      type = types.nullOr (linesOrSource "config.nu");
66      default = null;
67      example = literalExpression ''
68        { text = '''
69            let $config = {
70              filesize_metric: false
71              table_mode: rounded
72              use_ls_colors: true
73            }
74          ''';
75        }
76      '';
77      description = ''
78        The configuration file to be used for nushell.
79
80        See <https://www.nushell.sh/book/configuration.html#configuration> for more information.
81      '';
82    };
83
84    envFile = mkOption {
85      type = types.nullOr (linesOrSource "env.nu");
86      default = null;
87      example = ''
88        $env.FOO = 'BAR'
89      '';
90      description = ''
91        The environment variables file to be used for nushell.
92
93        See <https://www.nushell.sh/book/configuration.html#configuration> for more information.
94      '';
95    };
96
97    loginFile = mkOption {
98      type = types.nullOr (linesOrSource "login.nu");
99      default = null;
100      example = ''
101        # Prints "Hello, World" upon logging into tty1
102        if (tty) == "/dev/tty1" {
103          echo "Hello, World"
104        }
105      '';
106      description = ''
107        The login file to be used for nushell upon logging in.
108
109        See <https://www.nushell.sh/book/configuration.html#configuring-nu-as-a-login-shell> for more information.
110      '';
111    };
112
113    extraConfig = mkOption {
114      type = types.lines;
115      default = "";
116      description = ''
117        Additional configuration to add to the nushell configuration file.
118      '';
119    };
120
121    extraEnv = mkOption {
122      type = types.lines;
123      default = "";
124      description = ''
125        Additional configuration to add to the nushell environment variables file.
126      '';
127    };
128
129    extraLogin = mkOption {
130      type = types.lines;
131      default = "";
132      description = ''
133        Additional configuration to add to the nushell login file.
134      '';
135    };
136
137    shellAliases = mkOption {
138      type = types.attrsOf types.str;
139      default = { };
140      example = { ll = "ls -l"; };
141      description = ''
142        An attribute set that maps aliases (the top level attribute names in
143        this option) to command strings or directly to build outputs.
144      '';
145    };
146
147    environmentVariables = mkOption {
148      type = types.attrsOf types.str;
149      default = { };
150      example = { FOO = "BAR"; };
151      description = ''
152        An attribute set that maps an environment variable to a shell interpreted string.
153      '';
154    };
155  };
156
157  config = mkIf cfg.enable {
158    home.packages = [ cfg.package ];
159
160    home.file = mkMerge [
161      (let
162        writeConfig = cfg.configFile != null || cfg.extraConfig != ""
163          || aliasesStr != "";
164
165        aliasesStr = concatStringsSep "\n"
166          (mapAttrsToList (k: v: "alias ${k} = ${v}") cfg.shellAliases);
167      in mkIf writeConfig {
168        "${configDir}/config.nu".text = mkMerge [
169          (mkIf (cfg.configFile != null) cfg.configFile.text)
170          cfg.extraConfig
171          aliasesStr
172        ];
173      })
174
175      (let
176        envVarsStr = concatStringsSep "\n"
177          (mapAttrsToList (k: v: "$env.${k} = ${v}") cfg.environmentVariables);
178      in mkIf (cfg.envFile != null || cfg.extraEnv != "" || envVarsStr != "") {
179        "${configDir}/env.nu".text = mkMerge [
180          (mkIf (cfg.envFile != null) cfg.envFile.text)
181          cfg.extraEnv
182          envVarsStr
183        ];
184      })
185      (mkIf (cfg.loginFile != null || cfg.extraLogin != "") {
186        "${configDir}/login.nu".text = mkMerge [
187          (mkIf (cfg.loginFile != null) cfg.loginFile.text)
188          cfg.extraLogin
189        ];
190      })
191    ];
192  };
193}