1{ config, lib, pkgs, ... }:
2
3let
4
5 perlWrapped = pkgs.perl.withPackages (p: with p; [ ConfigIniFiles FileSlurp ]);
6
7in
8
9{
10
11 options = {
12 system.switch.enable = lib.mkOption {
13 type = lib.types.bool;
14 default = true;
15 description = lib.mdDoc ''
16 Whether to include the capability to switch configurations.
17
18 Disabling this makes the system unable to be reconfigured via `nixos-rebuild`.
19
20 This is good for image based appliances where updates are handled
21 outside the image. Reducing features makes the image lighter and
22 slightly more secure.
23 '';
24 };
25 };
26
27 config = lib.mkIf config.system.switch.enable {
28 system.activatableSystemBuilderCommands = ''
29 mkdir $out/bin
30 substitute ${./switch-to-configuration.pl} $out/bin/switch-to-configuration \
31 --subst-var out \
32 --subst-var-by toplevel ''${!toplevelVar} \
33 --subst-var-by coreutils "${pkgs.coreutils}" \
34 --subst-var-by distroId ${lib.escapeShellArg config.system.nixos.distroId} \
35 --subst-var-by installBootLoader ${lib.escapeShellArg config.system.build.installBootLoader} \
36 --subst-var-by localeArchive "${config.i18n.glibcLocales}/lib/locale/locale-archive" \
37 --subst-var-by perl "${perlWrapped}" \
38 --subst-var-by shell "${pkgs.bash}/bin/sh" \
39 --subst-var-by su "${pkgs.shadow.su}/bin/su" \
40 --subst-var-by systemd "${config.systemd.package}" \
41 --subst-var-by utillinux "${pkgs.util-linux}" \
42 ;
43
44 chmod +x $out/bin/switch-to-configuration
45 ${lib.optionalString (pkgs.stdenv.hostPlatform == pkgs.stdenv.buildPlatform) ''
46 if ! output=$(${perlWrapped}/bin/perl -c $out/bin/switch-to-configuration 2>&1); then
47 echo "switch-to-configuration syntax is not valid:"
48 echo "$output"
49 exit 1
50 fi
51 ''}
52 '';
53 };
54
55}