-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathnushell.nix
More file actions
207 lines (183 loc) · 6.37 KB
/
nushell.nix
File metadata and controls
207 lines (183 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
{
config,
pkgs,
lib,
...
}: let
inherit (lib.options) mkEnableOption mkPackageOption mkOption literalExpression;
inherit (lib.modules) mkIf;
inherit (lib.types) attrsOf anything lines listOf package;
inherit (lib.strings) concatStringsSep optionalString match;
inherit (lib.attrsets) mapAttrsToList mapAttrs attrValues;
inherit (lib.lists) flatten;
inherit (lib.trivial) boolToString;
inherit (lib.meta) getExe;
inherit (builtins) isBool isAttrs isList isInt isFloat;
nu.generate = {
config = settings: let
nuFormat = attrs:
mapAttrsToList (
n: v: let
v' =
if isBool v
then boolToString v
else if isAttrs v
then nuFormat attrs.${n}
else if isInt v || isFloat v
then toString v
else if match "^[0-9_]+$" (toString v) != null
then toString v
else "\"${toString v}\"";
in
if isList v'
then
attrValues (mapAttrs (
name: list: (map (value: "${name}.${value}") list)
) {${n} = v';})
else "${n} = ${v'}"
)
attrs;
in
concatStringsSep "\n " (map (v: "$env.config.${v}") (flatten (nuFormat settings)));
aliases = aliases:
concatStringsSep "\n " (mapAttrsToList (n: v: "alias ${n} = ${v}") aliases);
variables = variables: ''
load-env {${concatStringsSep ", " (mapAttrsToList (n: v: "${n}: \"${v}\"") variables)}}
'';
};
cfg = config.rum.programs.nushell;
in {
options.rum.programs.nushell = {
enable = mkEnableOption "nushell";
package = mkPackageOption pkgs "nushell" {};
settings = mkOption {
type = attrsOf anything;
default = {};
example = {
show_banner = false;
history = {
file_format = "sqlite";
max_size = "1_000_000";
sync_on_enter = true;
isolation = true;
};
};
description = ''
A set of options for nushell, flattened, prepended with `$env.config` to avoid overwriting
(tables of config are overwritten if they occupy the same namespace). It is then written
to {file}`$XDG_CONFIG_HOME/nushell/config.nu`.
Please see [The Nushell Book] for configuration options.
[The Nushell Book]: https://www.nushell.sh/book/configuration.html
'';
};
aliases = mkOption {
type = attrsOf anything;
default = {};
example = {
ll = "ls -l";
spp = "spotify_player";
};
description = ''
A set of aliases for nushell, converted into nu and written to
{file}`$XDG_CONFIG_HOME/nushell/config.nu`. Please note that we cannot
handle complex aliases that use `def` at this time.
Please see [The Nushell Book] for a simple tutorial.
[The Nushell Book]: https://www.nushell.sh/book/aliases.html
'';
};
plugins = mkOption {
type = listOf package;
default = [];
example = literalExpression ''
with pkgs.nushellPlugins; [
units
formats
query
]
'';
description = ''
A list of plugin packages to be installed and added to the
nushell plugin registry.
'';
};
extraConfig = mkOption {
type = lines;
default = "";
example = ''
def webdev [--run (-r)] {
cd ~/my-website
if $run {
nix develop --command pnpm run dev
} else {
nix develop --command nu
}
}
'';
description = ''
Extra configuration to be written to {file}`$XDG_CONFIG_HOME/nushell/config.nu`.
Please see [The Nushell Book] for more info.
[The Nushell Book]: https://www.nushell.sh/book/configuration.html
'';
};
envFile = mkOption {
type = lines;
default = "";
description = ''
[The Nushell Book]: https://www.nushell.sh/book/configuration.html
Extra configuration to be written to {file}`$XDG_CONFIG_HOME/nushell/env.nu`.
Please keep in mind that the upstream documentation generally advises against
writing to this file, and instead suggests using {file}`$XDG_CONFIG_HOME/nushell/config.nu`.
See [The Nushell Book] for more info.
'';
};
loginFile = mkOption {
type = lines;
default = "";
description = ''
Nushell may be used as a login shell, so this option writes to
{file}`$XDG_CONFIG_HOME/nushell/login.nu` to allow you to configure
nushell as your login shell.
Please recognize that this is mainly for advanced users and is not
for the faint of heart. Direct your questions and concerns to
[The Nushell Book] for more information.
[The Nushell Book]: https://www.nushell.sh/book/configuration.html#configuring-nu-as-a-login-shell
'';
};
};
config = mkIf cfg.enable {
packages = [cfg.package];
xdg.config.files = let
checks = {
settings = cfg.settings != {};
aliases = cfg.aliases != {};
extraConfig = cfg.extraConfig != "";
variables = config.environment.sessionVariables != {};
};
in {
"nushell/config.nu" = mkIf (checks.settings || checks.aliases || checks.extraConfig || checks.variables) {
text = concatStringsSep "\n" [
(optionalString checks.settings (nu.generate.config cfg.settings))
(optionalString checks.aliases (nu.generate.aliases cfg.aliases))
(optionalString checks.variables (nu.generate.variables config.environment.sessionVariables))
cfg.extraConfig
];
};
"nushell/env.nu" = mkIf (cfg.envFile != "") {text = cfg.envFile;};
# from https://github.com/nushell/nushell/discussions/12997#discussioncomment-9638977
# also used in home manager
"nushell/plugin.msgpackz" = mkIf (cfg.plugins != []) {
source = let
msgPackz = pkgs.runCommand "nuPlugin-msgPackz" {} ''
mkdir -p "$out"
${getExe cfg.package} \
--plugin-config "$out/plugin.msgpackz" \
--commands '${
concatStringsSep "\n" (map (plugin: "plugin add ${getExe plugin}") cfg.plugins)
}'
'';
in "${msgPackz}/plugin.msgpackz";
};
"nushell/login.nu" = mkIf (cfg.loginFile != "") {text = cfg.loginFile;};
};
};
}