infra/nix/os/modules/opinionatedDisk.nix

63 lines
1.5 KiB
Nix
Raw Normal View History

2023-02-07 18:24:28 +01:00
{
lib,
config,
...
}:
with lib; let
2020-12-31 02:12:29 +01:00
cfg = config.hardware.opinionatedDisk;
2023-02-07 18:24:28 +01:00
ownLib = import ../lib/default.nix {};
in {
2020-12-31 02:12:29 +01:00
options.hardware.opinionatedDisk = {
enable = mkEnableOption "Enable opinionated filesystem layout";
2023-02-07 18:24:28 +01:00
diskId = mkOption {type = types.str;};
2020-12-31 02:12:29 +01:00
encrypted = mkOption {
default = true;
type = types.bool;
};
};
config = lib.mkIf cfg.enable {
2018-11-10 19:24:24 +01:00
fileSystems."/boot" = {
2023-02-07 18:24:28 +01:00
device = ownLib.disk.bootFsDevice cfg.diskId;
fsType = "vfat";
};
fileSystems."/" = {
2023-02-07 18:24:28 +01:00
device = ownLib.disk.rootFsDevice cfg.diskId;
fsType = "btrfs";
2023-02-07 18:24:28 +01:00
options = ["subvol=nixos"];
};
fileSystems."/home" = {
2023-02-07 18:24:28 +01:00
device = ownLib.disk.rootFsDevice cfg.diskId;
fsType = "btrfs";
2023-02-07 18:24:28 +01:00
options = ["subvol=home"];
};
2023-02-07 18:24:28 +01:00
swapDevices = [{device = ownLib.disk.swapFsDevice cfg.diskId;}];
boot.loader.grub = {
2023-02-07 18:24:28 +01:00
device = ownLib.disk.bootGrubDevice cfg.diskId;
2020-12-31 02:12:29 +01:00
enableCryptodisk = cfg.encrypted;
};
2023-02-07 18:24:28 +01:00
boot.initrd.luks.devices =
lib.optionalAttrs cfg.encrypted
(builtins.listToAttrs [
{
name = let
splitstring =
builtins.split "/" (ownLib.disk.bootLuksDevice cfg.diskId);
lastelem = (builtins.length splitstring) - 1;
in
builtins.elemAt splitstring lastelem;
value = {
device = ownLib.disk.bootLuksDevice cfg.diskId;
preLVM = true;
allowDiscards = true;
};
}
]);
};
}