forked from numtide/devshell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.nix
More file actions
233 lines (206 loc) · 5.49 KB
/
options.nix
File metadata and controls
233 lines (206 loc) · 5.49 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
{ lib, pkgs, config, ... }:
with lib;
let
resolveKey = key:
let
attrs = builtins.filter builtins.isString (builtins.split "\\." key);
in
builtins.foldl' (sum: attr: sum.${attr}) pkgs attrs
;
pad = str: num:
if num > 0 then
pad "${str} " (num - 1)
else
str
;
ansiBoldOrange = "$(tput setaf 202)";
ansiReset = "$(tput sgr0)";
commandsToMenu = commands:
let
commandLengths =
map ({ name, ... }: builtins.stringLength name) commands;
maxCommandLength =
builtins.foldl'
(max: v: if v > max then v else max)
0
commandLengths
;
commandCategories = lib.unique (
(zipAttrsWithNames [ "category" ] (name: vs: vs) commands).category
);
commandByCategoriesSorted =
builtins.attrValues (lib.genAttrs
commandCategories
(category: lib.nameValuePair category (builtins.sort
(a: b: a.name < b.name)
(builtins.filter
(x: x.category == category)
commands
)
))
);
opCat = { name, value }:
let
opCmd = { name, help, ... }:
let
len = maxCommandLength - (builtins.stringLength name);
in
if help == null || help == "" then
name
else
"${pad name len} - ${help}";
in
"\n[${name}]\n" + builtins.concatStringsSep "\n" (map opCmd value);
in
builtins.concatStringsSep "\n" (map opCat commandByCategoriesSorted)
;
# Because we want to be able to push pure JSON-like data into the
# environment.
strOrPackage =
types.coercedTo types.str resolveKey types.package;
# These are all the options available for the commands.
commandOptions = {
name = mkOption {
type = types.str;
# default = null;
description = ''
Name of this command. Defaults to attribute name in commands.
'';
};
category = mkOption {
type = types.str;
default = "general commands";
description = ''
Set a free text category under which this command is grouped
and shown in the help menu.
'';
};
help = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Describes what the command does in one line of text.
'';
};
command = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
If defined, it will define a script for the command.
'';
};
package = mkOption {
type = types.nullOr strOrPackage;
default = null;
description = ''
Used to bring in a specific package. This package will be added to the
environment.
'';
};
};
in
{
options = {
name = mkOption {
type = types.str;
default = "devshell";
description = ''
Name of the shell environment. It usually maps to the project name.
'';
};
# TODO: rename motd to something better.
motd = mkOption {
type = types.str;
default = ''
${ansiBoldOrange}🔨 Welcome to ${config.name}${ansiReset}
$(menu)
'';
description = ''
Message Of The Day.
This is the welcome message that is being printed when the user opens
the shell.
'';
};
commands = mkOption {
type = types.listOf (types.submodule { options = commandOptions; });
default = [ ];
description = ''
Add commands to the environment.
'';
example = literalExample ''
[
{
help = "print hello";
name = "hello";
alias = "echo hello";
}
{
help = "used to format nix code";
package = pkgs.nixpkgs-fmt;
}
]
'';
};
bash = mkOption {
type = types.submodule {
options = {
extra = mkOption {
type = types.lines;
default = "";
description = ''
Extra commands to run in bash on environment startup.
'';
};
interactive = mkOption {
type = types.lines;
default = "";
description = ''
Same as shellHook, but is only executed on interactive shells.
This is useful to setup things such as custom prompt commands.
'';
};
};
};
default = { };
};
env = mkOption {
type = types.attrs;
default = { };
description = ''
Environment variables to add to the environment.
If the value is null, it will unset the environment variable.
Otherwise, the value will be converted to string before being set.
'';
example = {
GO111MODULE = "on";
HTTP_PORT = 8080;
};
};
packages = mkOption {
type = types.listOf strOrPackage;
default = [ ];
description = ''
A list of packages to add to the environment.
If the packages are passed as string, they will be retried from
nixpkgs with the same attribute name.
'';
};
};
config = {
commands = [
{
help = "prints this menu";
name = "menu";
command = ''
cat <<'DEVSHELL_MENU'
${commandsToMenu config.commands}
DEVSHELL_MENU
'';
}
];
packages =
builtins.filter
(x: x != null)
(map (x: x.package) config.commands);
};
}