infra/nix/os/modules/opinionatedDisk.nix

57 lines
1.5 KiB
Nix
Raw Normal View History

2022-10-31 11:04:38 +01:00
{ lib, config, ... }:
with lib;
let
2020-12-31 02:12:29 +01:00
cfg = config.hardware.opinionatedDisk;
2018-11-10 19:24:24 +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";
2022-10-31 11:04:38 +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" = {
device = (ownLib.disk.bootFsDevice cfg.diskId);
fsType = "vfat";
};
fileSystems."/" = {
2018-11-10 19:24:24 +01:00
device = (ownLib.disk.rootFsDevice cfg.diskId);
fsType = "btrfs";
options = [ "subvol=nixos" ];
};
fileSystems."/home" = {
2018-11-10 19:24:24 +01:00
device = (ownLib.disk.rootFsDevice cfg.diskId);
fsType = "btrfs";
options = [ "subvol=home" ];
};
2022-10-31 11:04:38 +01:00
swapDevices = [{ device = (ownLib.disk.swapFsDevice cfg.diskId); }];
boot.loader.grub = {
2018-11-10 19:24:24 +01:00
device = (ownLib.disk.bootGrubDevice cfg.diskId);
2020-12-31 02:12:29 +01:00
enableCryptodisk = cfg.encrypted;
};
2022-10-31 11:04:38 +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;
};
2022-10-31 11:04:38 +01:00
}]);
};
}