1{
2 lib,
3 config,
4 pkgs,
5 ...
6}:
7let
8 preSwitchCheckScript = lib.concatLines (
9 lib.mapAttrsToList (name: text: ''
10 # pre-switch check ${name}
11 if ! (
12 ${text}
13 ) >&2 ; then
14 echo "Pre-switch check '${name}' failed" >&2
15 exit 1
16 fi
17 '') config.system.preSwitchChecks
18 );
19in
20{
21 options.system.preSwitchChecksScript = lib.mkOption {
22 type = lib.types.pathInStore;
23 internal = true;
24 readOnly = true;
25 default = lib.getExe (
26 pkgs.writeShellApplication {
27 name = "pre-switch-checks";
28 text = preSwitchCheckScript;
29 }
30 );
31 };
32
33 options.system.preSwitchChecks = lib.mkOption {
34 default = { };
35 example = lib.literalExpression ''
36 { failsEveryTime =
37 '''
38 false
39 ''';
40 }
41 '';
42
43 description = ''
44 A set of shell script fragments that are executed before the switch to a
45 new NixOS system configuration. A failure in any of these fragments will
46 cause the switch to fail and exit early.
47 The scripts receive the new configuration path and the action verb passed
48 to switch-to-configuration, as the first and second positional arguments
49 (meaning that you can access them using `$1` and `$2`, respectively).
50 '';
51
52 type = lib.types.attrsOf lib.types.str;
53 };
54}