summaryrefslogtreecommitdiff
path: root/modules/usbauth/default.nix
blob: 1784221b9cf8cd621746c12d120ab8c77165706f (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
{
  config,
  lib,
  modulesPath,
  pkgs,
  ...
}:
{

  imports = [ (modulesPath + "/profiles/all-hardware.nix") ];

  options.usbauth = lib.mkOption {
    description = "USB Auth";
    type = lib.types.submodule {
      options = {
        enable = lib.mkOption { type = lib.types.bool; };
        device = lib.mkOption { type = lib.types.str; };
        keyname = lib.mkOption { type = lib.types.str; };
        uuid = lib.mkOption { type = lib.types.str; };
      };
    };
  };

  config = lib.mkIf config.usbauth.enable {
    boot.initrd.postDeviceCommands = pkgs.lib.mkBefore ''
      mkdir -m 0755 -p /key
      while ! findfs UUID=${config.usbauth.uuid} 2> /dev/null; do sleep 0.1; done > /dev/null
      cryptsetup open $(findfs UUID=${config.usbauth.uuid}) _key
      mount -o ro "/dev/mapper/_key" /key
    '';

    boot.initrd.luks.devices."${config.usbauth.device}" = {
      keyFile = "/key/.keys/${config.usbauth.keyname}";
      preLVM = false;
    };
  };
}