summaryrefslogtreecommitdiff
path: root/modules/secureboot.nix
blob: 29bff219ae2ed46ac669e647085111c0da235944 (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
{
  inputs,
  lib,
  pkgs,
  config,
  ...
}:
let
  sb-do-enroll-keys = pkgs.writeShellApplication {
    name = "sb-do-enroll-keys";
    text = ''
      set -ex;
      ${pkgs.sbctl}/bin/sbctl enroll-keys
    '';
  };
  sb-do-enroll-tpm = pkgs.writeShellApplication {
    name = "sb-do-enroll-tpm";
    runtimeInputs = [ pkgs.tpm2-tss ];
    text = ''
      set -ex;
      LUKS_DEVICE="${config.disko.devices.disk.nvme0n1.content.partitions.luks.device}"
      ${pkgs.systemd}/bin/systemd-cryptenroll --wipe-slot=tpm2 --tpm2-device=auto --tpm2-pcrs=0+7 "''${LUKS_DEVICE}"
    '';
  };
in
{
  imports = [
    inputs.lanzaboote.nixosModules.lanzaboote
  ];

  environment.systemPackages = [
    pkgs.sbctl
    sb-do-enroll-keys
    sb-do-enroll-tpm
  ];

  # Lanzaboote currently replaces the systemd-boot module.
  # This setting is usually set to true in configuration.nix
  # generated at installation time. So we force it to false
  # for now.
  boot.loader.systemd-boot.enable = lib.mkForce false;
  boot.initrd.systemd.enable = true;
  boot.initrd.systemd.tpm2.enable = true;

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

  # Create and enroll Secure Boot keys on first boot
  systemd.services.sbctl-setup = {
    description = "Create and enroll Secure Boot keys";
    wantedBy = [ "multi-user.target" ];
    unitConfig.ConditionPathExists = "!/var/lib/sbctl/GUID";

    serviceConfig = {
      Type = "oneshot";
      RemainAfterExit = true;
    };

    script = ''
      echo "Creating Secure Boot keys..."
      ${pkgs.sbctl}/bin/sbctl create-keys
    '';
  };
}