1{ config, lib, pkgs, ... }:
2
3let
4
5 perlWrapped = pkgs.perl.withPackages (p: with p; [ ConfigIniFiles FileSlurp ]);
6
7 description = extra: ''
8 Whether to include the capability to switch configurations.
9
10 Disabling this makes the system unable to be reconfigured via `nixos-rebuild`.
11
12 ${extra}
13 '';
14
15in
16
17{
18
19 options.system.switch = {
20 enable = lib.mkOption {
21 type = lib.types.bool;
22 default = true;
23 description = description ''
24 This is good for image based appliances where updates are handled
25 outside the image. Reducing features makes the image lighter and
26 slightly more secure.
27 '';
28 };
29
30 enableNg = lib.mkOption {
31 type = lib.types.bool;
32 default = false;
33 description = description ''
34 Whether to use `switch-to-configuration-ng`, an experimental
35 re-implementation of `switch-to-configuration` with the goal of
36 replacing the original.
37 '';
38 };
39 };
40
41 config = lib.mkMerge [
42 {
43 assertions = [{
44 assertion = with config.system.switch; enable -> !enableNg;
45 message = "Only one of system.switch.enable and system.switch.enableNg may be enabled at a time";
46 }];
47 }
48 (lib.mkIf config.system.switch.enable {
49 system.activatableSystemBuilderCommands = ''
50 mkdir $out/bin
51 substitute ${./switch-to-configuration.pl} $out/bin/switch-to-configuration \
52 --subst-var out \
53 --subst-var-by toplevel ''${!toplevelVar} \
54 --subst-var-by coreutils "${pkgs.coreutils}" \
55 --subst-var-by distroId ${lib.escapeShellArg config.system.nixos.distroId} \
56 --subst-var-by installBootLoader ${lib.escapeShellArg config.system.build.installBootLoader} \
57 --subst-var-by localeArchive "${config.i18n.glibcLocales}/lib/locale/locale-archive" \
58 --subst-var-by perl "${perlWrapped}" \
59 --subst-var-by shell "${pkgs.bash}/bin/sh" \
60 --subst-var-by su "${pkgs.shadow.su}/bin/su" \
61 --subst-var-by systemd "${config.systemd.package}" \
62 --subst-var-by utillinux "${pkgs.util-linux}" \
63 ;
64
65 chmod +x $out/bin/switch-to-configuration
66 ${lib.optionalString (pkgs.stdenv.hostPlatform == pkgs.stdenv.buildPlatform) ''
67 if ! output=$(${perlWrapped}/bin/perl -c $out/bin/switch-to-configuration 2>&1); then
68 echo "switch-to-configuration syntax is not valid:"
69 echo "$output"
70 exit 1
71 fi
72 ''}
73 '';
74 })
75 (lib.mkIf config.system.switch.enableNg {
76 # Use a subshell so we can source makeWrapper's setup hook without
77 # affecting the rest of activatableSystemBuilderCommands.
78 system.activatableSystemBuilderCommands = ''
79 (
80 source ${pkgs.buildPackages.makeWrapper}/nix-support/setup-hook
81
82 mkdir $out/bin
83 ln -sf ${lib.getExe pkgs.switch-to-configuration-ng} $out/bin/switch-to-configuration
84 wrapProgram $out/bin/switch-to-configuration \
85 --set OUT $out \
86 --set TOPLEVEL ''${!toplevelVar} \
87 --set DISTRO_ID ${lib.escapeShellArg config.system.nixos.distroId} \
88 --set INSTALL_BOOTLOADER ${lib.escapeShellArg config.system.build.installBootLoader} \
89 --set LOCALE_ARCHIVE ${config.i18n.glibcLocales}/lib/locale/locale-archive \
90 --set SYSTEMD ${config.systemd.package}
91 )
92 '';
93 })
94 ];
95
96}