server-configuration/disko.nix
2025-04-15 11:30:05 +02:00

130 lines
3.5 KiB
Nix
Executable file

{
lib,
root-disk ? throw "Expected a mf disk brother",
raid-disks ? [],
swap-size ? -1,
...
}: {
disko.devices = {
disk =
{
# Main SSD where /nix/store /etc /boot etc. is located.
main = {
type = "disk";
device = root-disk;
content = {
type = "gpt";
partitions = {
# Define a 512 megabyte boot partition
boot = {
size = "512M";
type = "EF00";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
mountOptions = ["umask=0077"];
};
};
# If the swap argument is defined then create a swap partition of the given size
swap = lib.mkIf (swap-size != -1) {
size = swap-size;
content = {
type = "swap";
discardPolicy = "both";
resumeDevice = true;
};
};
# Finally allocate the rest of the disk as a zfs device
root = {
size = "100%";
content = {
type = "zfs";
pool = "zroot";
};
};
};
};
};
}
# Import all disks into raid named "raid5"
// lib.attrsets.genAttrs raid-disks (name: {
type = "disk";
device = "/dev/" + name;
content = {
type = "gpt";
partitions = {
zfs = {
size = "100%";
content = {
type = "zfs";
pool = "raid5";
};
};
};
};
});
zpool = {
# Set up zroot pool
zroot = {
type = "zpool";
rootFsOptions = {
mountpoint = "none";
acltype = "posixacl"; # POSIX compliant file permissions
xattr = "sa"; # Store attributes in inodes to reduce disk reads
};
datasets = {
root = {
type = "zfs_fs";
mountpoint = "/";
};
"nix/store" = {
type = "zfs_fs";
mountpoint = "/nix/store";
};
};
};
# If any disks where defined to be in the raid we set up the raid5 pool
raid5 = lib.mkIf (builtins.length raid-disks > 0) {
type = "zpool";
mode = "raidz";
rootFsOptions = {
compression = "zstd"; # Compress filesystem for more storage
mountpoint = "none"; # Dont mount the zfs pool anywhere
acltype = "posixacl"; # POSIX compliant file permissions
xattr = "sa"; # Store attributes in inodes to reduce disk reads
"com.sun:auto-snapshot" = "true"; # Never a bad idea to have backups
};
datasets = {
var = {
type = "zfs_fs";
mountpoint = "/var";
};
home = {
type = "zfs_fs";
mountpoint = "/home";
};
srv = {
type = "zfs_fs";
mountpoint = "/srv";
};
opt = {
type = "zfs_fs";
mountpoint = "/opt";
};
# media = {
# type = "zfs_fs";
# mountpoint = "/media";
# };
};
};
};
};
}