Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 146 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ For security reasons, each host is expected to have its own dedicated key, locat

In order to generate a new key, run:

```sh
```bash
age-keygen -pq
```

Expand All @@ -60,7 +60,7 @@ Store the output in a temporary file.
Create a dedicated SOPS file for encrypting the host key. The general convention is to locate the
host key in `./secrets/hosts/${HOSTNAME}.yaml`.

```
```bash
# Create the file via
sops edit ./secrets/hosts/tux-ibp-amdgen9-nixos-btw.yaml
```
Expand Down Expand Up @@ -108,7 +108,7 @@ creation_rules:

Finally, re-encrypt all secrets with the new key by running:

```sh
```bash
find secrets -name '*.yaml' -type f -print0 | xargs -0 -l sops updatekeys --yes
```

Expand Down Expand Up @@ -227,6 +227,149 @@ During the installation, `hosts/tux-ibp-amdgen9-nixos-btw/hardware-configuration
rewritten, based on the detected hardware. Don't forget to commit the updated file!


### Enable Secure Boot

The flake has support for Secure Boot via [Lanzaboote](https://github.com/nix-community/lanzaboote).

The following steps are derived from [Lanzaboote's manual](https://nix-community.github.io/lanzaboote/introduction.html)
and subsections.


#### Prerequisites

Secure Boot requires NixOS to be installed in UEFI mode (not legacy BIOS) and `systemd-boot` to be
used as the boot loader.

These prerequisites can be checked via `bootctl status`:

```console
$ bootctl status
System:
Firmware: UEFI 2.40 (American Megatrends 5.11)
Firmware Arch: x64
Secure Boot: disabled (disabled)
TPM2 Support: yes
Measured UKI: no
Boot into FW: supported

Current Boot Loader:
Product: systemd-boot 260.5
...
```

In the `bootctl` output, the firmware needs to be `UEFI` and the current boot loader needs to be
`systemd-boot`.

#### System preparation

First, create the keys, which will be enrolled:

```bash
# Unless already present, install sbctl. Note that `sudo` won't see packages
# installed into your own shell's `PATH`, so generate the keys through the
# same `nix-shell` invocation instead of a separate `sudo sbctl ...` call:
sudo nix-shell -p sbctl --run "sbctl create-keys"
```

Then, enable Secure Boot for the host in question.

```diff
diff --git a/hosts/dell-xps9360-nixos-btw/default.nix b/hosts/dell-xps9360-nixos-btw/default.nix
index a90a902..1b72913 100644
--- a/hosts/dell-xps9360-nixos-btw/default.nix
+++ b/hosts/dell-xps9360-nixos-btw/default.nix
@@ -30,6 +29,8 @@

ai.claude-code.enable = true;

+ boot.secureboot.enable = true;
+
virtualisation = {
docker.enable = true;
podman.enable = true;
```

Then, rebuild the system with `nixos-rebuild switch`:

```bash
nixos-rebuild switch --flake . --sudo
```

Finally, verify that your machine is ready to have Secure Boot activated:

```console
$ sudo sbctl verify
Verifying file database and EFI images in /boot...
✓ /boot/EFI/BOOT/BOOTX64.EFI is signed
✓ /boot/EFI/Linux/nixos-generation-34-2jmnpsirjequvi2dbljik3xtbnqztk7m2gyaac43z3ypsx2vwyaa.efi is signed
✓ /boot/EFI/Linux/nixos-generation-35-xyg7hzxdcrrife23cnk4fyiwemy2lfzbsg6grdct47zgfxwz666a.efi is signed
✓ /boot/EFI/Linux/nixos-generation-36-nitlhcuad5svld25r62myqwpaapjptysjm35ahcn5d2talpgwwsq.efi is signed
✓ /boot/EFI/Linux/nixos-generation-37-tch4qkd3ohb5ibbwn6mismuk42l6hcea7h2vtm3ngna34iy5z5ua.efi is signed
✓ /boot/EFI/Linux/nixos-generation-38-2wt3sto427436gacwsye5t3dscu6vmuk7wkqg4wvfg5wm4by732a.efi is signed
✓ /boot/EFI/Linux/nixos-generation-39-f2ul6j7fdirt34nfufdeof2hf53srwyygnmpydjkpgebxkppdcha.efi is signed
✗ /boot/EFI/nixos/kernel-7.3-vuo5a7ruui4lqe3pwwbusyfxitkpukuv2bpxdrk2bf4u4lcgf4pa.efi is not signed
✗ /boot/EFI/nixos/kernel-7.3.3-hrbpymjqaigqn4prjafojemdr6xnhcrh5su7mapqjjzlz33vidbq.efi is not signed
✗ /boot/EFI/nixos/kernel-7.3.5-pwtmghyal4oh3esr3cgbsztittf732hswwzvapi7am2x2itbzumq.efi is not signed
✗ /boot/EFI/nixos/kernel-7.3.6-rb7lvtbwetvcsqhgnusau7u3n4vewcy52n7uoa7dzxexq2vcbnha.efi is not signed
✓ /boot/EFI/systemd/systemd-bootx64.efi is signed
```

It is expected that the files starting with `kernel-` are not signed.
Only proceed with the Secure Boot activation if the check above passes!


#### Activate Secure Boot

Enable Secure Boot by booting into the machine's BIOS menu. Typically, this involves pressing F2,
F8, F10 or F12 during the early boot process, such as the initial splash screen.

Essentially, it comes down to the following 3 items:

- enable the Secure Boot option
- enter Setup Mode
- if necessary, delete PK, KEK and DB keys.
DO NOT(!) delete the DBX keys. This represents the "Forbidden Signature Database".

The exact steps to enter Setup Mode vary significantly by vendor. On some ThinkPads, you select
"Reset to Setup Mode" explicitly; on Framework laptops, you delete each PK/KEK/DB entry
individually (never "Erase all Secure Boot Settings"); on Microsoft Surface devices, simply
disabling Secure Boot already puts the firmware into Setup Mode.

See [Lanzaboote's vendor-specific
instructions](https://nix-community.github.io/lanzaboote/getting-started/enable-secure-boot.html)
for steps matching your hardware.

Once enabled and booted back into the system, enroll the keys:

```console
$ sudo sbctl enroll-keys --microsoft
Enrolling keys to EFI variables...
With vendor keys from microsoft...✓
Enrolled keys to the EFI variables!
```

Since `--microsoft` was used, double-check afterwards that the Secure Boot Forbidden Signature
Database (dbx) isn't empty, e.g. by checking the file size of `/sys/firmware/efi/efivars/dbx-*`. An
empty dbx weakens protection against revoked, Microsoft-signed bootkits.

Reboot once more and verify that Secure Boot is active:

```console
$ bootctl status
System:
Firmware: UEFI 2.40 (American Megatrends 5.11)
Firmware Arch: x64
Secure Boot: enabled (user)
TPM2 Support: yes
Measured UKI: yes
Boot into FW: supported

Current Boot Loader:
Product: systemd-boot 260.5
...
```


## FAQ

The section below addresses the most frequently asked questions.
Expand Down
99 changes: 99 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
url = "github:Mic92/sops-nix";
inputs.nixpkgs.follows = "nixpkgs";
};

lanzaboote = {
url = "github:nix-community/lanzaboote/v1.2.0";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs =
Expand Down
2 changes: 2 additions & 0 deletions hosts/dell-xps9360-nixos-btw/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

ai.claude-code.enable = true;

boot.secureboot.enable = true;

virtualisation = {
docker.enable = true;
podman.enable = true;
Expand Down
1 change: 1 addition & 0 deletions modules/users/rtucek/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
./git.nix
./home.nix
./hyprland.nix
./secureboot.nix
./tmuxinator.nix
./user.nix
./virtualisation.nix
Expand Down
34 changes: 34 additions & 0 deletions modules/users/rtucek/secureboot.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
config,
lib,
pkgs,
inputs,
...
}:
let
cfg = config.rtucek.boot.secureboot;
in
{
imports = [
inputs.lanzaboote.nixosModules.lanzaboote
];

options.rtucek.boot.secureboot = {
enable = lib.options.mkEnableOption "Secure Boot via Lanzaboote";
};

config = lib.mkIf cfg.enable {
environment.systemPackages = [
pkgs.sbctl
];

# Lanzaboote will replace systemd-boot, hence systemd-boot has to be forcefully disabled.
# As such, it will override what we configure as a sensible default in `system/core/boot.nix`.
boot.loader.systemd-boot.enable = lib.mkForce false;

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