infra/nix/os/modules/opinionatedDisk.nix

74 lines
1.7 KiB
Nix
Raw Permalink Normal View History

2023-02-07 18:24:28 +01:00
{
lib,
config,
2023-07-06 22:42:24 +02:00
pkgs,
2023-02-07 18:24:28 +01:00
...
}:
2024-11-15 10:17:56 +01:00
with lib;
let
2020-12-31 02:12:29 +01:00
cfg = config.hardware.opinionatedDisk;
2024-11-15 10:17:56 +01:00
ownLib = pkgs.callPackage ../lib/default.nix { };
2024-01-18 14:59:17 +00:00
2024-11-15 10:17:56 +01:00
earlyDiskId = cfg: if cfg.earlyDiskIdOverride != "" then cfg.earlyDiskIdOverride else cfg.diskId;
in
{
2020-12-31 02:12:29 +01:00
options.hardware.opinionatedDisk = {
enable = mkEnableOption "Enable opinionated filesystem layout";
2024-11-15 10:17:56 +01:00
diskId = mkOption { type = types.str; };
2020-12-31 02:12:29 +01:00
encrypted = mkOption {
default = true;
type = types.bool;
};
2024-01-18 14:59:17 +00:00
earlyDiskIdOverride = mkOption {
default = "";
type = types.str;
2024-01-18 14:59:17 +00:00
};
};
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";
2024-11-15 10:17:56 +01:00
options = [ "subvol=nixos" ];
};
fileSystems."/home" = {
2023-02-07 18:24:28 +01:00
device = ownLib.disk.rootFsDevice cfg.diskId;
fsType = "btrfs";
2024-11-15 10:17:56 +01:00
options = [ "subvol=home" ];
};
2024-11-15 10:17:56 +01:00
swapDevices = [ { device = ownLib.disk.swapFsDevice cfg.diskId; } ];
boot.loader.grub = {
2024-01-18 14:59:17 +00:00
device = ownLib.disk.bootGrubDevice (earlyDiskId cfg);
2020-12-31 02:12:29 +01:00
enableCryptodisk = cfg.encrypted;
};
2024-11-15 10:17:56 +01:00
boot.initrd.luks.devices = lib.optionalAttrs cfg.encrypted (
builtins.listToAttrs [
2023-02-07 18:24:28 +01:00
{
2024-11-15 10:17:56 +01:00
name =
let
splitstring = builtins.split "/" (ownLib.disk.bootLuksDevice cfg.diskId);
lastelem = (builtins.length splitstring) - 1;
in
2023-02-07 18:24:28 +01:00
builtins.elemAt splitstring lastelem;
value = {
device = ownLib.disk.bootLuksDevice cfg.diskId;
2024-01-18 14:59:17 +00:00
2023-02-07 18:24:28 +01:00
preLVM = true;
allowDiscards = true;
};
}
2024-11-15 10:17:56 +01:00
]
);
};
}