aboutsummaryrefslogtreecommitdiff
path: root/nixos/modules/autounlock.nix
blob: d7d6a7c72cb93dafd7c61e09f5f3f3b92a85037a (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
{
  config,
  lib,
  pkgs,
  ...
}: let
  inherit (lib) mapAttrs mkOption mkIf types;
  cnf = config.cynerd.autounlock;
in {
  options = {
    cynerd.autounlock = mkOption {
      type = with types; attrsOf str;
      default = {};
      description = "Devices to be auto-unlocked.";
    };
  };

  config = mkIf (cnf != {}) {
    environment.systemPackages = [pkgs.luks-hw-password];
    boot.initrd = {
      extraFiles."/luks-hw-password".source = pkgs.luks-hw-password;
      luks.devices =
        mapAttrs (name: value: {
          device = value;
          keyFile = "/keys/${name}.key";
          fallbackToPassword = true;
          preOpenCommands = ''
            mkdir -p /keys
            /luks-hw-password/bin/luks-hw-password > /keys/${name}.key
          '';
          postOpenCommands = ''
            rm -rf /keys
          '';
        })
        cnf;
    };
  };
}