nix-config/modules/disko.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

191 lines
6.0 KiB
Nix
Raw Normal View History

2023-05-08 23:33:12 +02:00
{ config, lib, ... }:
2023-04-29 23:03:39 +02:00
2023-05-08 23:33:12 +02:00
let
cfg = config.disko;
in
2023-04-29 23:03:39 +02:00
{
2023-05-19 01:57:15 +02:00
options.disko.disks = lib.mkOption {
description = lib.mdDoc "Disk names to format.";
type = with lib.types; listOf (submodule (_: {
2023-05-19 01:57:15 +02:00
options = {
device = lib.mkOption {
type = lib.types.str;
default = null;
example = "/dev/sda";
description = "Path of the disk.";
};
2023-04-29 23:03:39 +02:00
2023-05-19 01:57:15 +02:00
name = lib.mkOption {
type = lib.types.str;
default = null;
example = "ssd0";
description = "Name of the disk.";
};
2023-04-29 23:03:39 +02:00
2023-05-19 01:57:15 +02:00
withBoot = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Wether to include a boot partition.";
};
2023-04-29 23:03:39 +02:00
2023-05-19 01:57:15 +02:00
withCeph = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Wether to include a ceph partition.";
};
2023-04-29 23:03:39 +02:00
2023-05-19 01:57:15 +02:00
withLuks = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Wether to encrypt the paritions.";
};
withZfs = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Wether to include a zfs parition.";
};
};
}));
default = [ ];
2023-04-29 23:03:39 +02:00
};
2023-05-08 23:33:12 +02:00
config = {
2023-05-19 03:49:13 +02:00
assertions = lib.mkIf (cfg.disks != [ ]) (lib.head (map
2023-05-19 01:57:15 +02:00
(disk: [
{
assertion = disk.withCeph || disk.withZfs;
message = "Must enable ceph or zfs!";
}
{
assertion = disk.withCeph -> disk.withLuks;
message = "Ceph requires Luks!";
}
])
2023-05-19 03:49:13 +02:00
cfg.disks));
2023-05-08 23:33:12 +02:00
disko.devices = lib.mkIf (cfg.disks != [ ]) (lib.head (map
(disk:
let
diskName = if disk.name != "" then "-${disk.name}" else "";
luksName = "crypt-${config.networking.hostName}${diskName}";
rootSize = 200; # size of the zfs partition if inside of lvm
vgName = "lvm-${config.networking.hostName}${diskName}";
zfs = {
size = if (!disk.withCeph) then "100%FREE" else "${toString rootSize}GiB";
content = {
pool = zfsName;
type = "zfs";
2023-05-10 20:34:18 +02:00
};
};
zfsName = "${config.networking.hostName}${diskName}";
in
{
disk.${disk.device} = {
inherit (disk) device;
type = "disk";
content = {
type = "table";
format = "gpt";
partitions = lib.optional disk.withZfs
{
name = "ESP";
start = "1MiB";
end = "512MiB";
bootable = true;
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
} ++ [
{
name = "root";
start = if disk.withZfs then "512MiB" else "1MiB";
end = "100%";
part-type = "primary";
content = lib.optionalAttrs disk.withLuks
{
type = "luks";
name = luksName;
2023-05-20 19:12:03 +02:00
# trim potential new lines to not have them in the password
keyFile = "tr -d '\n' </$PWD/keyFile";
content = {
type = "lvm_pv";
vg = vgName;
};
} // lib.optionalAttrs (!disk.withLuks) zfs.content;
}
];
2023-05-10 20:34:18 +02:00
};
};
} // lib.optionalAttrs disk.withLuks {
lvm_vg.${vgName} = {
type = "lvm_vg";
lvs = lib.optionalAttrs disk.withCeph
{
2023-05-20 04:18:29 +02:00
ceph.size = "100%FREE";
} // lib.optionalAttrs disk.withZfs { inherit zfs; };
};
} // {
zpool.${zfsName} = {
type = "zpool";
rootFsOptions.acltype = "posixacl";
options = {
ashift = "12";
autotrim = "on";
2023-05-10 20:34:18 +02:00
};
datasets =
let
dataset = mountpoint: {
2023-05-21 21:12:56 +02:00
inherit mountpoint;
options = {
canmount = "on";
compression = "zstd";
dnodesize = "auto";
normalization = "formD";
xattr = "sa";
inherit mountpoint;
2023-05-10 20:34:18 +02:00
};
type = "zfs_fs";
};
in
{
"data" = dataset "/";
"data/etc" = dataset "/etc";
"data/home" = dataset "/home";
"data/var" = dataset "/var";
# used by services.postgresqlBackup and later by restic
"data/var/backup" = dataset "/var/backup";
"data/var/lib" = dataset "/var/lib";
"data/var/log" = dataset "/var/log";
2023-05-21 21:12:56 +02:00
"nixos" = lib.recursiveUpdate (dataset "nixos") {
mountpoint = null;
options = {
canmount = "off";
mountpoint = "none";
2023-05-10 20:34:18 +02:00
};
type = "zfs_fs";
};
"nixos/nix" = dataset "/nix";
2023-05-21 21:24:10 +02:00
"nixos/nix/store" = lib.recursiveUpdate (dataset "/nix/store") {
2023-05-21 21:12:56 +02:00
options.atime = "off";
};
"nixos/nix/var" = dataset "/nix/var";
2023-05-21 21:12:56 +02:00
# zfs uses copy on write and requires some free space to delete files when the disk is completely filled
"reserved" = lib.recursiveUpdate (dataset "reserved") {
mountpoint = null;
options = {
canmount = "off";
mountpoint = "none";
reservation = "5GiB";
2023-05-10 20:34:18 +02:00
};
type = "zfs_fs";
2023-05-08 23:33:12 +02:00
};
};
};
})
cfg.disks));
2023-05-08 23:33:12 +02:00
};
2023-04-29 23:03:39 +02:00
}