Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
./random-alerts
./host-info.nix
./secrets.nix
./mcl-disko
];
}
196 changes: 196 additions & 0 deletions modules/mcl-disko/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
{ withSystem, inputs, ... }:
{
flake.modules.nixos.mcl-disko =
{
pkgs,
config,
lib,
...
}:
with lib;
let
cfg = config.mcl.disko;
in
{
imports = [
inputs.disko.nixosModules.disko
];
options.mcl.disko = {
enable = mkEnableOption "Enable Module";

legacyBoot = mkOption {
type = types.bool;
default = false;
example = true;
description = "Declare if the configuration is for a Hetzner server or not";
};

swapSize = mkOption {
type = types.str;
default = "32G";
example = "32768M";
description = "The size of the hard disk space used when RAM is full";
};

espSize = mkOption {
type = types.str;
default = "4G";
example = "4096M";
description = "The size of the hard disk space used for the ESP filesystem";
};

disks = mkOption {
type = types.listOf types.str;
default = [ ];
example = [
"/dev/disk/sda"
"/dev/disk/sdb"
"/dev/disk/sdc"
];
description = "The disk partitions to be used when ZFS is being created";
};

disksNames = mkOption {
type = types.listOf types.str;
default = cfg.disks;
example = [
"sda"
"sdb"
"sdc"
];
description = "The disk partitions names to be used when ZFS is being created";
};

zpool = {
name = mkOption {
type = types.str;
default = "zfs_root";
description = "The name of the ZFS Pool";
};

mode = mkOption {
type = types.enum [
"stripe"
"mirror"
"raidz1"
"raidz2"
"raidz3"
];
default = "stripe";
description = "Set ZFS Pool redundancy - e.g. 'mirror', 'raidz1', etc.";
};

extraDatasets = mkOption {
type = types.attrsOf (
types.submodule (
{ name, ... }:
{
options = {
mountpoint = mkOption {
type = types.nullOr types.str;
default = name;
example = "/var/lib";
description = "The ZFS dataset mountpoint";
};

type = mkOption {
type = types.enum [
"zfs_fs"
"zfs_volume"
];
default = "zfs_fs";
description = "Type of ZFS dataset";
};

options = mkOption {
type = types.attrs;
default = { };
example = {
refreservation = "50G";
};
description = "";
};

snapshot = mkEnableOption "Whether to enable ZFS snapshots";

refreservation = mkOption {
type = types.nullOr types.str;
default = null;
description = "Size of reservations";
};
};
}
)
);
default = { };
example = {
"/opt".snapshot = false;
"/opt/downloads".snapshot = false;
"/opt/downloads/vm-images" = {
snapshot = false;
options = {
quota = "120G";
};
};
};
description = "Extra ZFS Pool datasets";
};
};
};

config.disko =
let
makePrimaryZfsDisk = import ./primaryZfsPartition.nix;
makeSecondaryZfsDisk = import ./secondaryZfsPartition.nix;

first = builtins.head cfg.disks;
rest = builtins.tail cfg.disks;

firstDiskName = builtins.head cfg.disksNames;
restDisksNames = builtins.tail cfg.disksNames;

secondaryDisks = builtins.listToAttrs (
lib.zipListsWith (disk: diskName: {
name = diskName;
value =
if cfg.zpool.mode != "stripe" then
makePrimaryZfsDisk {
inherit disk lib;
isSecondary = true;
espSize = cfg.espSize;
swapSize = cfg.swapSize;
legacyBoot = cfg.legacyBoot;
poolName = cfg.zpool.name;
}
else
makeSecondaryZfsDisk {
poolName = cfg.zpool.name;
inherit disk lib;
};
}) rest restDisksNames
);

in
lib.mkIf cfg.enable {
devices = {
disk = secondaryDisks // {
"${firstDiskName}" = makePrimaryZfsDisk {
inherit lib;
disk = first;
isSecondary = false;
espSize = cfg.espSize;
swapSize = cfg.swapSize;
legacyBoot = cfg.legacyBoot;
poolName = cfg.zpool.name;
};
};
zpool = import ./zpool.nix {
poolName = cfg.zpool.name;
poolMode = cfg.zpool.mode;
poolExtraDatasets = cfg.zpool.extraDatasets;
inherit lib;
};
};
};
};
}
95 changes: 95 additions & 0 deletions modules/mcl-disko/primaryZfsPartition.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
lib,
disk,
isSecondary,
espSize,
swapSize,
legacyBoot,
poolName,
}:
{
type = "disk";
device = disk;
content =
{
type = if legacyBoot then "table" else "gpt";
partitions =
if !legacyBoot then
{
"ESP" = {
device = "${disk}-part1";
size = espSize;
type = "EF00";
content = {
type = "filesystem";
format = "vfat";
mountpoint = if isSecondary then null else "/boot";
mountOptions = [ "umask=0077" ];
};
};

"zfs" = {
device = "${disk}-part2";
end = "-${swapSize}";
type = "BF00";
content = {
type = "zfs";
pool = "${poolName}";
};
};

"swap" = {
device = "${disk}-part3";
size = swapSize;
content = {
type = "swap";
randomEncryption = true;
};
};
}
else
[
{
name = "boot";
start = "1MiB";
end = "2MiB";
part-type = "primary";
flags = [ "bios_grub" ];
}
{
name = "ESP";
start = "2MiB";
end = espSize;
bootable = true;
content = {
type = "filesystem";
format = "vfat";
mountpoint = if isSecondary then null else "/boot";
};
}
{
name = "zfs";
start = espSize;
end = "-${swapSize}";
part-type = "primary";
content = {
type = "zfs";
pool = "${poolName}";
};
}
{
name = "swap";
start = "-${swapSize}";
end = "100%";
part-type = "primary";
content = {
type = "swap";
randomEncryption = true;
};
}
];
}
// lib.optionalAttrs legacyBoot {
format = "gpt";
};
}
22 changes: 22 additions & 0 deletions modules/mcl-disko/secondaryZfsPartition.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
lib,
poolName,
disk,
}:
{
type = "disk";
device = disk;
content = {
type = "gpt";
partitions = {
zfs = {
device = "${disk}-part1";
size = "100%";
content = {
type = "zfs";
pool = "${poolName}";
};
};
};
};
}
Loading