2023-07-24 09:23:20 +03:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
2024-07-29 15:39:54 +03:00
|
|
|
}:
|
|
|
|
let
|
|
|
|
mkUnlock =
|
|
|
|
{
|
|
|
|
sshEndpoint,
|
|
|
|
pingEndpoint,
|
|
|
|
remotePubkey,
|
|
|
|
pwFile,
|
|
|
|
pingTimeoutSec,
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
timeoutStr = builtins.toString pingTimeoutSec;
|
|
|
|
in
|
|
|
|
''
|
|
|
|
set -x
|
|
|
|
# if host is reachable via "pingEndpoint", which, we presume is
|
|
|
|
# VPN (which implies the rootfs has been unlocked for VPN to work),
|
|
|
|
# exit successfully.
|
|
|
|
${pkgs.iputils}/bin/ping -q -W ${timeoutStr} -c 1 ${pingEndpoint} && exit 0
|
2023-07-24 09:23:20 +03:00
|
|
|
|
2024-07-29 15:39:54 +03:00
|
|
|
exec ${pkgs.openssh}/bin/ssh \
|
|
|
|
-i /etc/ssh/ssh_host_ed25519_key \
|
|
|
|
-o UserKnownHostsFile=none \
|
|
|
|
-o GlobalKnownHostsFile=/dev/null \
|
|
|
|
-o KnownHostsCommand="${pkgs.coreutils}/bin/echo ${sshEndpoint} ${remotePubkey}" \
|
|
|
|
root@${sshEndpoint} < "${pwFile}"
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
{
|
2023-07-24 09:23:20 +03:00
|
|
|
options.mj.services.zfsunlock = with lib.types; {
|
|
|
|
enable = lib.mkEnableOption "remotely unlock zfs-encrypted root volumes";
|
|
|
|
|
|
|
|
targets = lib.mkOption {
|
2024-07-29 15:39:54 +03:00
|
|
|
default = { };
|
2023-10-01 23:14:05 +03:00
|
|
|
type = attrsOf (submodule {
|
|
|
|
options = {
|
2024-07-29 15:39:54 +03:00
|
|
|
sshEndpoint = lib.mkOption { type = str; };
|
|
|
|
pingEndpoint = lib.mkOption { type = str; };
|
2023-10-01 23:14:05 +03:00
|
|
|
pingTimeoutSec = lib.mkOption {
|
|
|
|
type = int;
|
|
|
|
default = 20;
|
2023-07-24 09:23:20 +03:00
|
|
|
};
|
2024-07-29 15:39:54 +03:00
|
|
|
remotePubkey = lib.mkOption { type = str; };
|
|
|
|
pwFile = lib.mkOption { type = path; };
|
|
|
|
startAt = lib.mkOption { type = either str (listOf str); };
|
2023-10-01 23:14:05 +03:00
|
|
|
};
|
|
|
|
});
|
2023-07-24 09:23:20 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf config.mj.services.zfsunlock.enable {
|
2024-07-29 15:39:54 +03:00
|
|
|
systemd.services = lib.mapAttrs' (
|
|
|
|
name: cfg:
|
|
|
|
lib.nameValuePair "zfsunlock-${name}" {
|
|
|
|
description = "zfsunlock service for ${name}";
|
|
|
|
script = mkUnlock (builtins.removeAttrs cfg [ "startAt" ]);
|
|
|
|
serviceConfig = {
|
|
|
|
User = "root";
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
) config.mj.services.zfsunlock.targets;
|
2023-07-24 09:23:20 +03:00
|
|
|
|
2024-07-29 15:39:54 +03:00
|
|
|
systemd.timers = lib.mapAttrs' (
|
|
|
|
name: cfg:
|
|
|
|
lib.nameValuePair "zfsunlock-${name}" {
|
|
|
|
description = "zfsunlock timer for ${name}";
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = cfg.startAt;
|
|
|
|
};
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
}
|
|
|
|
) config.mj.services.zfsunlock.targets;
|
2023-08-22 14:09:41 +03:00
|
|
|
|
2024-07-29 15:39:54 +03:00
|
|
|
mj.base.unitstatus.units = map (name: "zfsunlock-${name}") (
|
|
|
|
builtins.attrNames config.mj.services.zfsunlock.targets
|
|
|
|
);
|
2023-07-24 09:23:20 +03:00
|
|
|
};
|
|
|
|
}
|