1{ options, config, lib, pkgs, ... }:
2
3let
4 inherit (lib)
5 mkOption
6 types
7 ;
8
9 systemBuilderArgs = {
10 activationScript = config.system.activationScripts.script;
11 dryActivationScript = config.system.dryActivationScript;
12 };
13
14in
15{
16 options = {
17 system.activatable = mkOption {
18 type = types.bool;
19 default = true;
20 description = ''
21 Whether to add the activation script to the system profile.
22
23 The default, to have the script available all the time, is what we normally
24 do, but for image based systems, this may not be needed or not be desirable.
25 '';
26 };
27 system.activatableSystemBuilderCommands = options.system.systemBuilderCommands // {
28 description = ''
29 Like `system.systemBuilderCommands`, but only for the commands that are
30 needed *both* when the system is activatable and when it isn't.
31
32 Disclaimer: This option might go away in the future. It might be
33 superseded by separating switch-to-configuration into a separate script
34 which will make this option superfluous. See
35 https://github.com/NixOS/nixpkgs/pull/263462#discussion_r1373104845 for
36 a discussion.
37 '';
38 };
39 system.build.separateActivationScript = mkOption {
40 type = types.package;
41 description = ''
42 A separate activation script package that's not part of the system profile.
43
44 This is useful for configurations where `system.activatable` is `false`.
45 Otherwise, you can just use `system.build.toplevel`.
46 '';
47 };
48 };
49 config = {
50 system.activatableSystemBuilderCommands = ''
51 echo "$activationScript" > $out/activate
52 echo "$dryActivationScript" > $out/dry-activate
53 substituteInPlace $out/activate --subst-var-by out ''${!toplevelVar}
54 substituteInPlace $out/dry-activate --subst-var-by out ''${!toplevelVar}
55 chmod u+x $out/activate $out/dry-activate
56 unset activationScript dryActivationScript
57 '';
58
59 system.systemBuilderCommands = lib.mkIf
60 config.system.activatable
61 config.system.activatableSystemBuilderCommands;
62 system.systemBuilderArgs = lib.mkIf config.system.activatable
63 (systemBuilderArgs // {
64 toplevelVar = "out";
65 });
66
67 system.build.separateActivationScript =
68 pkgs.runCommand
69 "separate-activation-script"
70 (systemBuilderArgs // {
71 toplevelVar = "toplevel";
72 toplevel = config.system.build.toplevel;
73 })
74 ''
75 mkdir $out
76 ${config.system.activatableSystemBuilderCommands}
77 '';
78 };
79}