nixos-config/lib/fudo/host-filesystems.nix

120 lines
4.4 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
with lib;
let
hostname = config.instance.hostname;
host-filesystems = config.fudo.hosts.${hostname}.encrypted-filesystems;
2021-10-18 07:03:01 -07:00
optionalOrDefault = str: default: if (str != null) then str else default;
2021-10-18 07:03:01 -07:00
filesystemsToMountpointLists = mapAttrsToList
(fs: fsOpts: fsOpts.mountpoints);
concatMapAttrs = f: as: concatMap (i: i) (mapAttrsToList f as);
in {
config = {
users.groups = let
mountpointToGroups = mp: mpOpts:
optional (mpOpts.group != null)
(nameValuePair mpOpts.group {
members = mpOpts.users;
});
mountpointListToGroups =
concatMapAttrs mountpointToGroups;
mountpointListsToGroups =
concatMap mountpointListToGroups;
in listToAttrs
(mountpointListsToGroups
(filesystemsToMountpointLists host-filesystems));
2021-10-18 07:03:01 -07:00
systemd = {
2021-10-18 07:03:01 -07:00
# Ensure the mountpoints exist
tmpfiles.rules = let
2021-10-18 07:03:01 -07:00
mountpointToPath = mp: mpOpts:
2021-10-18 21:55:24 -07:00
"d '${mp}' 750 root ${optionalOrDefault mpOpts.group "-"} - -";
2021-10-18 07:03:01 -07:00
filesystemsToMountpointLists = mapAttrsToList
(fs: fsOpts: fsOpts.mountpoints);
mountpointListsToPaths = concatMap
2021-10-18 07:03:01 -07:00
(mps: mapAttrsToList mountpointToPath mps);
in mountpointListsToPaths (filesystemsToMountpointLists host-filesystems);
# Actual mounts of decrypted filesystems
mounts = let
2021-10-17 22:36:01 -07:00
filesystems = mapAttrsToList
(fs: opts: { filesystem = fs; opts = opts; })
host-filesystems;
2021-10-17 23:33:07 -07:00
mounts = concatMap
(fs: mapAttrsToList
(mp: mp-opts:
2021-10-17 23:33:07 -07:00
{
what = "/dev/mapper/${fs.filesystem}";
type = fs.opts.filesystem-type;
where = mp;
options = concatStringsSep "," (fs.opts.options ++ mp-opts.options);
2021-10-17 23:17:43 -07:00
description = "${fs.opts.filesystem-type} filesystem on ${fs.filesystem} mounted to ${mp}";
requires = [ "${fs.filesystem}-decrypt.service" ];
2021-10-18 21:55:24 -07:00
partOf = [ "${fs.filesystem}.target" ];
wantedBy = [ "${fs.filesystem}.target" ];
})
fs.opts.mountpoints)
2021-10-17 23:33:07 -07:00
filesystems;
2021-10-18 07:03:01 -07:00
in mounts;
2021-10-18 07:03:01 -07:00
# Jobs to decrypt the encrypted devices
services = mapAttrs' (filesystem-name: opts:
nameValuePair "${filesystem-name}-decrypt"
{
description = "Decrypt the ${filesystem-name} filesystem when the key is available at ${opts.key-path}";
path = with pkgs; [ cryptsetup ];
serviceConfig = {
2021-10-18 07:03:01 -07:00
Type = "oneshot";
RemainAfterExit = true;
ExecStart = pkgs.writeShellScript "decrypt-${filesystem-name}.sh" ''
2021-10-18 21:55:24 -07:00
[ -e /dev/mapper/${filesystem-name} ] || cryptsetup open --type luks --key-file ${opts.key-path} ${opts.encrypted-device} ${filesystem-name}
'';
ExecStartPost = pkgs.writeShellScript "remove-${filesystem-name}-key.sh" ''
rm ${opts.key-path}
'';
ExecStop = pkgs.writeShellScript "close-${filesystem-name}.sh" ''
cryptsetup close /dev/mapper/${filesystem-name}
'';
};
2021-10-18 21:55:24 -07:00
restartIfChanged = true;
})
host-filesystems;
2021-10-18 07:03:01 -07:00
# Watch the path of the key, trigger decrypt when it's available
2021-10-18 21:55:24 -07:00
paths = let
decryption-jobs = mapAttrs' (filesystem-name: opts:
nameValuePair "${filesystem-name}-decrypt"
{
wantedBy = [ "default.target" ];
description = "Watch for decryption key, then decrypt the target filesystem.";
pathConfig = {
PathExists = opts.key-path;
Unit = "${filesystem-name}-decrypt.service";
};
}) host-filesystems;
post-decryption-jobs = mapAttrs' (filesystem-name: opts:
nameValuePair "${filesystem-name}-mount"
{
wantedBy = [ "default.target" ];
description = "Mount ${filesystem-name} filesystems once the decrypted device is available.";
pathConfig = {
PathExists = "/dev/mapper/${filesystem-name}";
Unit = "${filesystem-name}.target";
};
}) host-filesystems;
in decryption-jobs // post-decryption-jobs;
targets = mapAttrs (filesystem-name: opts:
{
description = "${filesystem-name} enabled and available.";
}) host-filesystems;
};
};
}