summaryrefslogtreecommitdiff
path: root/os/core/bootloader.nix
blob: aae167eb1ec5a5d542e83d0c2add97d7aaeeb688 (plain)
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
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.os.core.bootloader;
  gpgHome = "/root/.gnupg";
in
{
  options.os.core.bootloader = {
    type = lib.mkOption {
      type = lib.types.enum [
        "systemd-boot"
        "grub"
        "none"
      ];
      default = "systemd-boot";
      description = "Which bootloader to use";
    };

    efi = lib.mkOption {
      type = lib.types.bool;
      default = if cfg.grub.device == "nodev" then true else false;
      description = "Whether the system uses UEFI or Legacy BIOS";
    };

    timeout = lib.mkOption {
      type = lib.types.int;
      default = 3;
      description = "Boot menu timeout in seconds";
    };

    grub = {
      device = lib.mkOption {
        type = lib.types.str;
        default = "nodev";
        description = "Device to install GRUB to (e.g. /dev/nvme0n1). Use 'nodev' for UEFI.";
      };
      useOSProber = lib.mkOption {
        type = lib.types.bool;
        default = false;
        description = "Scan for other operating systems";
      };
      defaultEntry = lib.mkOption {
        type = lib.types.int;
        default = 0;
        description = "Index of the default boot entry";
      };
      signing = {
        enable = lib.mkEnableOption "GPG signing for Libreboot/GRUB";
        keyId = lib.mkOption {
          type = lib.types.str;
          description = "The GPG Key ID used to sign the boot files";
        };
      };
    };

    luks.enable = lib.mkEnableOption "LUKS encryption support";
  };

  config = lib.mkMerge [
    # 1. Common Kernel & Initrd Settings
    {
      boot = {
        loader = {
          timeout = cfg.timeout;
          efi.canTouchEfiVariables = lib.mkDefault cfg.efi;
        };
        supportedFilesystems = [
          "ntfs"
          "btrfs"
        ];
        kernelParams = [
          "quiet"
          "splash"
        ];
        consoleLogLevel = 0;
        initrd.availableKernelModules = [
          "aesni_intel"
          "cryptd"
        ];
      };
      systemd.settings.Manager.DefaultTimeoutStopSec = "5s";
    }

    # 2. Systemd-boot Implementation
    (lib.mkIf (cfg.type == "systemd-boot") {
      boot.loader.systemd-boot = {
        enable = true;
        editor = false;
        consoleMode = "max";
      };
    })

    # 3. GRUB Implementation
    (lib.mkIf (cfg.type == "grub") {
      boot.loader.grub = {
        enable = true;
        device = cfg.grub.device;
        efiSupport = cfg.efi;
        useOSProber = cfg.grub.useOSProber;
        default = cfg.grub.defaultEntry;
        enableCryptodisk = cfg.luks.enable;
        copyKernels = true;

        extraConfig = lib.mkIf cfg.grub.signing.enable ''
          set check_signatures=enforce
          terminal_input console
          terminal_output console
        '';

        extraInstallCommands = lib.mkIf cfg.grub.signing.enable ''
          echo "Signing with keys from ${gpgHome}"

          SIGN_CMD="${pkgs.gnupg}/bin/gpg --homedir ${gpgHome} --detach-sign --batch --yes --default-key ${cfg.grub.signing.keyId}"

          $SIGN_CMD /boot/grub/grub.cfg

          for f in /boot/nixos/*; do
            if [[ "$f" != *.sig ]]; then
              $SIGN_CMD "$f"
            fi
          done
        '';
      };
      environment.systemPackages = lib.optional cfg.grub.signing.enable pkgs.gnupg;
    })
    {
      assertions = [
        {
          assertion = cfg.grub.signing.enable -> cfg.grub.signing.keyId != "";
          message = "Bootloader signing is enabled but os.core.bootloader.grub.signing.keyId is not set.";
        }
      ];
    }
  ];
}