-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathdevshell.nix
More file actions
486 lines (422 loc) · 13.4 KB
/
devshell.nix
File metadata and controls
486 lines (422 loc) · 13.4 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
{
config,
lib,
pkgs,
options,
...
}:
let
cfg = config.devshell;
sanitizedName = lib.strings.sanitizeDerivationName cfg.name;
ansi = import ../nix/ansi.nix;
bashBin = "${cfg.bashPackage}/bin";
bashPath = "${cfg.bashPackage}/bin/bash";
# Because we want to be able to push pure JSON-like data into the
# environment.
strOrPackage = import ../nix/strOrPackage.nix { inherit lib pkgs; };
# Use this to define a flake app for the environment.
mkFlakeApp = bin: {
type = "app";
program = "${bin}";
};
mkSetupHook =
rc:
pkgs.stdenvNoCC.mkDerivation {
name = "devshell-setup-hook";
setupHook = pkgs.writeText "devshell-setup-hook.sh" ''
source ${rc}
'';
dontUnpack = true;
dontBuild = true;
dontInstall = true;
};
mkNakedShell = pkgs.callPackage ../nix/mkNakedShell.nix { };
addAttributeName =
prefix:
lib.mapAttrs (
k: v:
v
// {
text = ''
#### ${prefix}.${k}
${v.text}
'';
}
);
inherit (lib)
mkOption
mkEnableOption
attrNames
attrValues
textClosureMap
replaceStrings
types
id
;
entryOptions = {
text = mkOption {
type = types.str;
description = ''
Script to run.
'';
};
deps = mkOption {
type = types.listOf types.str;
default = [ ];
description = ''
A list of other steps that this one depends on.
'';
};
};
# Write a bash profile to load
envBash = pkgs.writeText "devshell-env.bash" ''
if [[ -n ''${IN_NIX_SHELL:-} || ''${DIRENV_IN_ENVRC:-} = 1 ]]; then
# We know that PWD is always the current directory in these contexts
PRJ_ROOT=$PWD
elif [[ -z ''${PRJ_ROOT:-} ]]; then
${lib.optionalString (cfg.prj_root_fallback != null) cfg.prj_root_fallback}
if [[ -z "''${PRJ_ROOT:-}" ]]; then
echo "ERROR: please set the PRJ_ROOT env var to point to the project root" >&2
return 1
fi
fi
export PRJ_ROOT
# Expose the folder that contains the assembled environment.
export DEVSHELL_DIR=@DEVSHELL_DIR@
# Prepend the PATH with the devshell dir and bash
PATH=''${PATH%:/path-not-set}
PATH=''${PATH#${bashBin}:}
export PATH=$DEVSHELL_DIR/bin:${bashBin}:$PATH
${cfg.startup_env}
${textClosureMap id (addAttributeName "startup" cfg.startup) (attrNames cfg.startup)}
# Interactive sessions
if [[ $- == *i* ]]; then
${textClosureMap id (addAttributeName "interactive" cfg.interactive) (attrNames cfg.interactive)}
fi # Interactive session
'';
# This is our entrypoint script.
entrypoint = pkgs.writeScript "${cfg.name}-entrypoint" ''
#!${bashPath}
# Script that sets-up the environment. Can be both sourced or invoked.
export DEVSHELL_DIR=@DEVSHELL_DIR@
# If the file is sourced, skip all of the rest and just source the env
# script.
if (return 0) &>/dev/null; then
source "$DEVSHELL_DIR/env.bash"
return
fi
# Be strict!
set -euo pipefail
while (( "$#" > 0 )); do
case "$1" in
-h|--help)
help=1
;;
--pure)
pure=1
;;
--prj-root)
if (( "$#" < 2 )); then
echo 1>&2 '${cfg.name}: missing required argument to --prj-root'
exit 1
fi
PRJ_ROOT="$2"
shift
;;
--env-bin)
if (( "$#" < 2 )); then
echo 1>&2 '${cfg.name}: missing required argument to --env-bin'
exit 1
fi
env_bin="$2"
shift
;;
--)
shift
break
;;
*)
break
;;
esac
shift
done
if [[ -n "''${help:-}" ]]; then
${pkgs.coreutils}/bin/cat <<USAGE
Usage: ${cfg.name}
$0 -h | --help # show this help
$0 [--pure] # start a bash sub-shell
$0 [--pure] <cmd> [...] # run a command in the environment
Options:
* --pure : execute the script in a clean environment
* --prj-root <path> : set the project root (\$PRJ_ROOT)
* --env-bin <path> : path to the env executable (default: /usr/bin/env)
USAGE
exit
fi
if (( "$#" == 0 )); then
# Start an interactive shell
set -- ${lib.escapeShellArg bashPath} --rcfile "$DEVSHELL_DIR/env.bash" --noprofile
fi
if [[ -n "''${pure:-}" ]]; then
# re-execute the script in a clean environment.
# note that the `--` in between `"$0"` and `"$@"` will immediately
# short-circuit options processing on the second pass through this
# script, in case we get something like:
# <entrypoint> --pure -- --pure <cmd>
set -- "''${env_bin:-/usr/bin/env}" -i -- ''${HOME:+"HOME=''${HOME:-}"} ''${PRJ_ROOT:+"PRJ_ROOT=''${PRJ_ROOT:-}"} "$0" -- "$@"
else
# Start a script
source "$DEVSHELL_DIR/env.bash"
fi
exec -- "$@"
'';
# Builds the DEVSHELL_DIR with all the dependencies
devshell_dir = pkgs.buildEnv rec {
name = "${sanitizedName}-dir";
paths = cfg.packages;
postBuild = ''
substitute ${envBash} $out/env.bash --subst-var-by DEVSHELL_DIR $out
substitute ${entrypoint} $out/entrypoint --subst-var-by DEVSHELL_DIR $out
chmod +x $out/entrypoint
mainProgram="${meta.mainProgram}"
# ensure mainProgram doesn't collide
if [ -e "$out/bin/$mainProgram" ]; then
echo "Warning: Cannot create entry point for this devshell at '\$out/bin/$mainProgram' because an executable with that name already exists." >&2
echo "Set meta.mainProgram to something else than '$mainProgram'." >&2
else
# if $out/bin is a single symlink, transform it into a directory tree
# (buildEnv does that when there is only one package in the environment)
if [ -L "$out/bin" ]; then
mv "$out/bin" bin-tmp
mkdir "$out/bin"
ln -s bin-tmp/* "$out/bin/"
fi
ln -s $out/entrypoint "$out/bin/$mainProgram"
fi
'';
meta.mainProgram = config.meta.mainProgram or sanitizedName;
};
# Returns a list of all the input derivation ... for a derivation.
inputsOf =
drv:
lib.filter lib.isDerivation (
(drv.buildInputs or [ ])
++ (drv.nativeBuildInputs or [ ])
++ (drv.propagatedBuildInputs or [ ])
++ (drv.propagatedNativeBuildInputs or [ ])
);
in
{
options.devshell = {
bashPackage = mkOption {
internal = true;
type = strOrPackage;
default = pkgs.bashInteractive;
defaultText = "pkgs.bashInteractive";
description = "Version of bash to use in the project";
};
package = mkOption {
internal = true;
type = types.package;
description = ''
This package contains the DEVSHELL_DIR
'';
};
startup = mkOption {
type = types.attrsOf (types.submodule { options = entryOptions; });
default = { };
internal = true;
description = ''
A list of scripts to execute on startup.
'';
};
startup_env = mkOption {
type = types.str;
default = "";
internal = true;
description = ''
Please ignore. Used by the env module.
'';
};
interactive = mkOption {
type = types.attrsOf (types.submodule { options = entryOptions; });
default = { };
internal = true;
description = ''
A list of scripts to execute on interactive startups.
'';
};
# TODO: rename motd to something better.
motd = mkOption {
type = types.str;
default = ''
{202}🔨 Welcome to ${cfg.name}{reset}
$(type -p menu &>/dev/null && menu)
'';
apply = replaceStrings (map (key: "{${key}}") (attrNames ansi)) (attrValues ansi);
description = ''
Message Of The Day.
This is the welcome message that is being printed when the user opens
the shell.
You may use any valid ansi color from the 8-bit ansi color table. For example, to use a green color you would use something like {106}. You may also use {bold}, {italic}, {underline}. Use {reset} to turn off all attributes.
'';
};
load_profiles = mkEnableOption "load etc/profiles.d/*.sh in the shell";
name = mkOption {
type = types.str;
default = "devshell";
description = ''
Name of the shell environment. It usually maps to the project name.
'';
};
meta = mkOption {
type = types.attrsOf types.anything;
default = { };
description = ''
Metadata, such as 'meta.description'. Can be useful as metadata for downstream tooling.
'';
};
packages = mkOption {
type = types.listOf strOrPackage;
default = [ ];
description = ''
The set of packages to appear in the project environment.
Those packages come from <https://nixos.org/NixOS/nixpkgs> and can be
searched by going to <https://search.nixos.org/packages>
'';
};
packagesFrom = mkOption {
type = types.listOf strOrPackage;
default = [ ];
description = ''
Add all the build dependencies from the listed packages to the
environment.
'';
};
shell = mkOption {
internal = true;
type = types.package;
description = "TODO";
};
prj_root_fallback = mkOption {
type =
let
envType = options.env.type.nestedTypes.elemType;
coerceFunc = value: { inherit value; };
in
types.nullOr (types.coercedTo types.nonEmptyStr coerceFunc envType);
apply = x: if x == null then x else x // { name = "PRJ_ROOT"; };
default = {
eval = "$PWD";
};
example = lib.literalExpression ''
{
# Use the top-level directory of the working tree
eval = "$(git rev-parse --show-toplevel)";
};
'';
description = ''
If IN_NIX_SHELL is nonempty, or DIRENV_IN_ENVRC is set to '1', then
PRJ_ROOT is set to the value of PWD.
This option specifies the path to use as the value of PRJ_ROOT in case
IN_NIX_SHELL is empty or unset and DIRENV_IN_ENVRC is any value other
than '1'.
Set this to null to force PRJ_ROOT to be defined at runtime (except if
IN_NIX_SHELL or DIRENV_IN_ENVRC are defined as described above).
Otherwise, you can set this to a string representing the desired
default path, or to a submodule of the same type valid in the 'env'
options list (except that the 'name' field is ignored).
'';
};
};
config.devshell = {
package = devshell_dir;
packages = lib.foldl' (sum: drv: sum ++ (inputsOf drv)) [ ] cfg.packagesFrom;
startup = {
motd = lib.noDepEntry ''
__devshell-motd() {
${pkgs.coreutils}/bin/cat <<DEVSHELL_PROMPT
${cfg.motd}
DEVSHELL_PROMPT
}
if [[ ''${DEVSHELL_NO_MOTD:-} = 1 ]]; then
# Skip if that env var is set
:
elif [[ ''${DIRENV_IN_ENVRC:-} = 1 ]]; then
# Print the motd in direnv
__devshell-motd
else
# Print information if the prompt is displayed. We have to make
# that distinction because `nix-shell -c "cmd"` is running in
# interactive mode.
__devshell-prompt() {
__devshell-motd
# Make it a noop
__devshell-prompt() { :; }
}
PROMPT_COMMAND=__devshell-prompt''${PROMPT_COMMAND+;$PROMPT_COMMAND}
fi
'';
}
// (lib.optionalAttrs cfg.load_profiles {
load_profiles = lib.noDepEntry ''
# Load installed profiles
for file in "$DEVSHELL_DIR/etc/profile.d/"*.sh; do
# If that folder doesn't exist, bash loves to return the whole glob
[[ -f "$file" ]] && source "$file"
done
'';
});
interactive = {
PS1_util = lib.noDepEntry ''
if [[ -n "''${PRJ_ROOT:-}" ]]; then
# Print the path relative to $PRJ_ROOT
rel_root() {
local path
path=$(${pkgs.coreutils}/bin/realpath --relative-to "$PRJ_ROOT" "$PWD")
if [[ $path != . ]]; then
echo " $path "
fi
}
else
# If PRJ_ROOT is unset, print only the current directory name
rel_root() {
echo " \W "
}
fi
'';
# Set a cool PS1
PS1 = lib.stringAfter [ "PS1_util" ] (
lib.mkDefault ''
__set_prompt() {
PS1='\[\033[38;5;202m\][${cfg.name}]$(rel_root)\$\[\033[0m\] '
}
# Only set the prompt when entering a nix shell, since nix shells
# always reset to plain bash, otherwise respect the user's preferences
[[ -n "''${IN_NIX_SHELL:-}" ]] && __set_prompt
unset -f __set_prompt
''
);
};
# Use a naked derivation to limit the amount of noise passed to nix-shell.
shell = mkNakedShell {
name = sanitizedName;
meta =
# set default for meta.mainProgram here to gain compatibility with:
# `lib.getExe`, `nix run`, `nix bundle`, etc.
{
mainProgram = cfg.package.meta.mainProgram;
}
// cfg.meta;
profile = cfg.package;
passthru = {
inherit config;
# keep flakeApp attribute for backward compatibility
flakeApp = mkFlakeApp "${devshell_dir}/entrypoint";
hook = mkSetupHook "${devshell_dir}/env.bash";
inherit (config._module.args) pkgs;
};
};
};
}