aboutsummaryrefslogtreecommitdiffstats
path: root/modules/by-name/bo/boot/module.nix
blob: 2667b3855a9edceea89301c82094ac39b4911d8f (plain) (blame)
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
# nixos-config - My current NixOS configuration
#
# Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This file is part of my nixos-config.
#
# You should have received a copy of the License along with this program.
# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
{
  config,
  lib,
  pkgs,
  modules,
  modulesPath,
  system,
  specialArgs,
  ...
}: let
  cfg = config.soispha.boot;

  tails = let
    tailsPrefix = "/EFI/tails";
  in {
    root = "${tailsPrefix}/tails.iso";
    initrd = "${tailsPrefix}/initrd.img";
    vmlinuz = "${tailsPrefix}/vmlinuz-linux";
  };

  iso = pkgs.tails-iso;

  # From:
  # - The extracted ISO's boot dir
  # - Reverse engineered from:
  #     - `<tails iso squashfs>/usr/share/initramfs-tools/init`
  #     - `<tails iso squashfs>/usr/lib/live/boot/`
  iso_options =
    ## General options?
    [
      "initrd=${tails.initrd}"
      "noprompt"
      "timezone=Etc/UTC"
      "config"
      "noautologin"
      "slab_nomerge"
      "slub_debug=FZ"
      "mce=0"
      "vsyscall=none"
      "init_on_free=1"
      "mds=full,nosmt"
      "page_alloc.shuffle=1"
      "randomize_kstack_offset=on"
      "efi_pstore.pstore_disable=1"
      "erst_disable"
      "spec_store_bypass_disable=on"
      "systemd.condition_needs_update=no"
    ]
    ## Options for the first `init` script
    ++ [
      # Use the `*-live` scripts
      "boot=live"

      # "splash"
      "plymouth.enable=0"

      # "quiet"
      "debug"
    ]
    ## Options for the `*-live` `init` scripts
    ++ [
      "module=Tails"

      # TODO: RO-makes the fromiso not work <2026-06-08>
      # "live-boot.read-only=/dev/nvme0*"

      # Don't store things persistently
      "nopersistence"

      "fromiso=/dev/nvme0n1p1/${tails.root}"
    ];
in {
  options.soispha.boot = {
    enable = lib.mkEnableOption "Bootloader configuration";
    enableIsoEntry = lib.mkEnableOption "an tails iso boot entry";
  };

  imports = [
    modules.lanzaboote.nixosModules.lanzaboote
  ];

  config = lib.mkIf cfg.enable {
    # This should only be necessary for `lanzaboote`, but that is the current default in
    # this module.
    soispha.impermanence.directories = [
      "/var/lib/sbctl"
    ];

    boot = {
      initrd = {
        kernelModules = ["nvme" "btrfs"];
      };

      kernelPackages = pkgs.linuxPackages_latest;

      lanzaboote = {
        enable = true;
        pkiBundle = "/var/lib/sbctl";

        settings = {
          # Disable editing the kernel command line (which could allow someone to become root)
          editor = false;
          default = "@saved";
        };
      };

      loader = {
        external = lib.mkIf cfg.enableIsoEntry {
          installHook = lib.mkForce (let
            lanzabooteCfg = config.boot.lanzaboote;

            lanzabooteInstallHook = import "${modulesPath}/../lib/eval-config.nix" {
              inherit system specialArgs;
              modules = [
                modules.lanzaboote.nixosModules.lanzaboote

                {
                  # Copy the relevant config into the eval-module context.
                  boot = {
                    inherit (config.boot) kernelPackages;

                    lanzaboote = {
                      inherit (lanzabooteCfg) enable pkiBundle;
                      settings = {
                        inherit (lanzabooteCfg.settings) editor default;
                      };
                    };

                    loader = {
                      inherit (config.boot.loader) timeout efi systemd-boot;
                    };
                  };
                  systemd.package = config.systemd.package;
                }
              ];
            };

            install = pkgs.writeShellScript "wrapped-install-tails-iso-marker" ''
              echo "[Wrapped bootloader install] Copying tails iso..."
              ${copyExtraFiles}

              echo "[Wrapped bootloader install] Running original lanzaboote install..."
              ${lanzabooteInstallHook.config.boot.loader.external.installHook}
            '';

            copyExtraFiles = let
              systemdCfg = config.boot.loader.systemd-boot;
              nixosDir = "EFI/nixos";

              bootMountPoint = config.boot.loader.efi.efiSysMountPoint;
              install = lib.getExe' pkgs.coreutils "install";

              inherit (lib) mapAttrsToList;
              inherit (lib.strings) escapeShellArg concatStrings;
            in
              pkgs.writeShellScript "copy-extra-files" ''
                ${concatStrings (
                  mapAttrsToList (n: v: ''
                    ${install} -Dp "${v}" "${bootMountPoint}/"${escapeShellArg n}
                    ${install} -D /dev/null "${bootMountPoint}/${nixosDir}/.extra-files/"${escapeShellArg n}
                  '')
                  systemdCfg.extraFiles
                )}

                ${lib.getExe pkgs.sbctl} sign "${bootMountPoint}/${tails.vmlinuz}"

                ${concatStrings (
                  mapAttrsToList (n: v: ''
                    ${install} -Dp "${pkgs.writeText n v}" "${bootMountPoint}/loader/entries/"${escapeShellArg n}
                    ${install} -D /dev/null "${bootMountPoint}/${nixosDir}/.extra-files/loader/entries/"${escapeShellArg n}
                  '')
                  systemdCfg.extraEntries
                )}
              '';
          in
            install);
        };

        systemd-boot = lib.mkIf cfg.enableIsoEntry {
          # Lanzaboote currently replaces the systemd-boot module.
          enable = false;

          extraEntries = {
            "live.conf" = ''
              title Tails ${iso.passthru.version} Live ISO
              linux ${tails.vmlinuz}
              initrd ${tails.initrd}
              options ${builtins.concatStringsSep " " iso_options}
            '';
          };

          extraFiles = {
            "${tails.root}" = "${iso}/tails.iso";
            "${tails.vmlinuz}" = "${iso}/live/vmlinuz-linux";
            "${tails.initrd}" = "${iso}/live/initrd.img";
          };
        };

        efi = {
          canTouchEfiVariables = true;
          efiSysMountPoint = "/boot";
        };
      };
    };
  };
}