Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Bootloader & Secure Boot

Platforms: NixOS only.

Supports Limine (default), systemd-boot, and GRUB. Secure Boot via sbctl, natively on Limine and through lanzaboote on systemd-boot.

Configuration

  config = lib.mkIf cfg.enable (lib.mkMerge [
    {
      assertions = [
        {
          assertion = !(cfg.secureBoot && cfg.type == "systemd-boot") || hasLanzaboote;
          message = "othrys.system.bootloader: secureBoot with type = \"systemd-boot\" requires lanzaboote. Add lanzaboote as a flake input and import lanzaboote.nixosModules.lanzaboote in the host's modules.";
        }
        {
          assertion = !(cfg.secureBoot && cfg.type == "grub");
          message = "othrys.system.bootloader: GRUB has no Secure Boot support in NixOS. Set type to \"limine\" or \"systemd-boot\", or set secureBoot = false.";
        }
        {
          assertion = !(cfg.secureBoot && cfg.type == "none");
          message = "othrys.system.bootloader: secureBoot does nothing with type = \"none\", since this module manages no bootloader there. Set type to \"limine\" or \"systemd-boot\", or set secureBoot = false.";
        }
      ];

      # Only a loader this module installs needs to write boot entries, and a
      # host can still say otherwise.
      boot.loader.efi.canTouchEfiVariables = lib.mkIf (cfg.type != "none") (lib.mkDefault true);

      boot.loader.limine = lib.mkIf (cfg.type == "limine") (
        {
          enable = true;
          secureBoot.enable = cfg.secureBoot;
          inherit (cfg) extraEntries;
        }
        // limit "maxGenerations"
      );

      # Lanzaboote installs and signs systemd-boot itself, so the stock module
      # goes off whenever lanzaboote takes over. Without the lanzaboote import
      # it stays on. Turning it off there leaves NixOS on its default GRUB,
      # whose own assertion then fails beside the one above.
      #
      # Lanzaboote reads its own limit from this option, so the one write
      # covers both.
      #
      # The menu editor lets anyone at the keyboard append `init=/bin/sh` to
      # the kernel command line, which is a root shell on a disk that is not
      # encrypted and a way around the login on one that is. nixpkgs leaves
      # it on; lanzaboote copies this flag into its own loader.conf. Limine
      # ships with its editor off.
      boot.loader.systemd-boot = lib.mkIf (cfg.type == "systemd-boot") (
        {
          enable = !(cfg.secureBoot && hasLanzaboote);
          editor = lib.mkDefault false;
        }
        // limit "configurationLimit"
      );

      # GRUB has no switch for its editor. Only a menu password keeps a
      # present attacker off the command line, and that is the host's own
      # `boot.loader.grub.users` to set.
      boot.loader.grub = lib.mkIf (cfg.type == "grub") (
        {
          enable = true;
          efiSupport = true;
          device = "nodev";
        }
        // limit "configurationLimit"
      );

      environment.systemPackages = lib.mkIf cfg.secureBoot [
        pkgs.sbctl
      ];

      othrys.internal.persist."system.bootloader" = lib.mkIf cfg.secureBoot {
        directories = [
          {
            directory = "/var/lib/sbctl";
            user = "root";
            group = "root";
            mode = "0700";
          }
        ];
      };
    }

    # The guard sits on the attrset because mkIf does not defer option
    # existence. A conditional write into boot.lanzaboote still fails on a
    # host that never imported the module.
    (lib.optionalAttrs hasLanzaboote {
      boot.lanzaboote = lib.mkIf (cfg.type == "systemd-boot" && cfg.secureBoot) {
        enable = true;
        pkiBundle = "/var/lib/sbctl";
      };
    })
  ]);

Bootloader Selection

Set in host config via othrys.system.bootloader.type:

ValueBootloaderSecure BootNotes
"limine"LimineYes, nativeDefault. Supports extra entries (dual-boot).
"systemd-boot"systemd-bootYes, needs lanzabooteSimple, well-supported.
"grub"GRUBRejectedEFI support, device = "nodev".
"none"NoneRejectedThe module manages no bootloader and the host configures one itself.

systemd-boot lets anyone at the keyboard edit the kernel command line from the menu, and init=/bin/sh there is a root shell. nixpkgs leaves that editor on. This module sets boot.loader.systemd-boot.editor to false as a default, and lanzaboote copies the flag into its own loader.conf, so both paths get the same answer. A host that wants the editor back assigns the option itself. Limine ships with its editor off. GRUB has no such switch; only a menu password, boot.loader.grub.users, keeps a present attacker off the command line, and the module leaves that to the host.

boot.loader.efi.canTouchEfiVariables is a default of true for the three loaders the module installs and is not set for "none".

Generation Limit

maxGenerations is how many of the newest generations the bootloader keeps on the boot partition and lists in its menu. It defaults to 5. Older generations stay in the system profile and only leave the boot menu. The option maps to boot.loader.limine.maxGenerations, or to configurationLimit for systemd-boot and GRUB. Lanzaboote takes its limit from the systemd-boot option. null writes nothing and leaves the bootloader’s own default, which is no limit for Limine and systemd-boot.

The limit exists because every listed generation has its kernel and its initrd copied to the boot partition. With no limit that is every generation in the profile, so the partition fills. Limine removes unused files only at the end of an install that succeeds. Once the partition is full, a switch fails with No space left on device in the bootloader install, and no later switch can free the space.

An install copies the new generation before it removes an old one, so the partition has to hold the limit plus one. One generation costs its kernel plus its initrd, and that varies a great deal between hosts.

Cost of one generation1G partition holdsHighest safe maxGenerations
40M2524
110M98
165M65
220M43

On a running Limine host, the size sudo du -sh /boot/limine/kernels reports, divided by the number of kernel and initrd pairs in that directory, gives the cost. A host with a large initrd, which a GPU driver loaded in the initrd typically causes, sets a lower number than the default.

Recovering a full boot partition

The limit cannot help until one install succeeds, so a partition that is already full needs space freed by hand. The installer names each file after the store directory it came from, such as <hash>-linux-6.18.51-bzImage, which makes the files of kept generations easy to tell apart.

  1. Delete old generations from the profile, keeping the newest three: sudo nix-env -p /nix/var/nix/profiles/system --delete-generations +3

  2. List the boot files that no kept generation, nor the booted or the current system, points at:

    sudo bash -c '
    keep=$(for g in /nix/var/nix/profiles/system-*-link /run/booted-system /run/current-system; do
      for f in kernel initrd; do basename "$(dirname "$(readlink -f "$g/$f")")"; done
    done | sort -u)
    find /boot/limine -type f \( -name "*-bzImage" -o -name "*-initrd" -o -name "*.tmp" \) | while read -r p; do
      b=$(basename "$p"); hit=0
      for k in $keep; do case "$b" in "$k"-*) hit=1 ;; esac; done
      [ "$hit" = 0 ] && du -h "$p"
    done'
    
  3. Read the list. Then run the same command with du -h "$p" replaced by rm -v "$p".

  4. Switch again. A successful install rewrites the boot menu from the kept generations.

Until that switch succeeds, the old boot menu still lists the deleted generations, and those entries do not boot. The newest entries keep every file they need.

Secure Boot

When othrys.system.bootloader.secureBoot = true:

  • sbctl is installed for managing Secure Boot keys
  • Keys in /var/lib/sbctl are persisted across reboots (via impermanence)
  • With "limine", boot.loader.limine.secureBoot.enable is set and Limine signs itself with the sbctl keys
  • With "systemd-boot", boot.lanzaboote is enabled with pkiBundle = "/var/lib/sbctl", and boot.loader.systemd-boot.enable goes to false because lanzaboote installs and signs systemd-boot itself

The Secure Boot guide walks through key creation and enrollment.

systemd-boot needs lanzaboote

nixosModules.default does not import lanzaboote, so a host that never enables Secure Boot on systemd-boot needs no extra input. A host that does adds the input and the module in the consuming flake:

inputs.lanzaboote.url = "github:nix-community/lanzaboote/<release-tag>";
modules = [inputs.lanzaboote.nixosModules.lanzaboote];

Without that import, secureBoot = true on "systemd-boot" fails evaluation with an assertion that names both lines.

What is rejected

  • GRUB has no Secure Boot support in NixOS, so secureBoot = true with type = "grub" fails evaluation instead of silently doing nothing.
  • type = "none" fails the same way, since the module manages no bootloader there.

Limits

  • No option exists for TPM measured boot. Lanzaboote’s boot.lanzaboote.measuredBoot options are left for the host to set directly.
  • The eval-bootloader check evaluates every type with secureBoot off and on, and reads back the loader options each combination sets. No check boots a VM under firmware Secure Boot, so the checks prove evaluation only.

Dual Boot

Add extra entries via othrys.system.bootloader.extraEntries (Limine format):

othrys.system.bootloader.extraEntries = ''
  /Windows 11
      protocol: efi
      path: uuid(...):/.../bootmgfw.efi
'';