infra/nix/os/modules/opinionatedDisk.nix

73 lines
1.7 KiB
Nix

{
lib,
config,
pkgs,
...
}:
with lib;
let
cfg = config.hardware.opinionatedDisk;
ownLib = pkgs.callPackage ../lib/default.nix { };
earlyDiskId = cfg: if cfg.earlyDiskIdOverride != "" then cfg.earlyDiskIdOverride else cfg.diskId;
in
{
options.hardware.opinionatedDisk = {
enable = mkEnableOption "Enable opinionated filesystem layout";
diskId = mkOption { type = types.str; };
encrypted = mkOption {
default = true;
type = types.bool;
};
earlyDiskIdOverride = mkOption {
default = "";
type = types.str;
};
};
config = lib.mkIf cfg.enable {
fileSystems."/boot" = {
device = ownLib.disk.bootFsDevice cfg.diskId;
fsType = "vfat";
};
fileSystems."/" = {
device = ownLib.disk.rootFsDevice cfg.diskId;
fsType = "btrfs";
options = [ "subvol=nixos" ];
};
fileSystems."/home" = {
device = ownLib.disk.rootFsDevice cfg.diskId;
fsType = "btrfs";
options = [ "subvol=home" ];
};
swapDevices = [ { device = ownLib.disk.swapFsDevice cfg.diskId; } ];
boot.loader.grub = {
device = ownLib.disk.bootGrubDevice (earlyDiskId cfg);
enableCryptodisk = cfg.encrypted;
};
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;
};
}
]
);
};
}