|
| 1 | +{ withSystem, inputs, ... }: |
| 2 | +{ |
| 3 | + flake.modules.nixos.mcl-disko = |
| 4 | + { |
| 5 | + pkgs, |
| 6 | + config, |
| 7 | + lib, |
| 8 | + ... |
| 9 | + }: |
| 10 | + with lib; |
| 11 | + let |
| 12 | + cfg = config.mcl.disko; |
| 13 | + in |
| 14 | + { |
| 15 | + imports = [ |
| 16 | + inputs.disko.nixosModules.disko |
| 17 | + ]; |
| 18 | + options.mcl.disko = { |
| 19 | + enable = mkEnableOption "Enable Module"; |
| 20 | + |
| 21 | + legacyBoot = mkOption { |
| 22 | + type = types.bool; |
| 23 | + default = false; |
| 24 | + example = true; |
| 25 | + description = "Declare if the configuration is for a Hetzner server or not"; |
| 26 | + }; |
| 27 | + |
| 28 | + swapSize = mkOption { |
| 29 | + type = types.str; |
| 30 | + default = "32G"; |
| 31 | + example = "32768M"; |
| 32 | + description = "The size of the hard disk space used when RAM is full"; |
| 33 | + }; |
| 34 | + |
| 35 | + espSize = mkOption { |
| 36 | + type = types.str; |
| 37 | + default = "4G"; |
| 38 | + example = "4096M"; |
| 39 | + description = "The size of the hard disk space used for the ESP filesystem"; |
| 40 | + }; |
| 41 | + |
| 42 | + disks = mkOption { |
| 43 | + type = types.listOf types.str; |
| 44 | + default = [ ]; |
| 45 | + example = [ |
| 46 | + "/dev/disk/sda" |
| 47 | + "/dev/disk/sdb" |
| 48 | + "/dev/disk/sdc" |
| 49 | + ]; |
| 50 | + description = "The disk partitions to be used when ZFS is being created"; |
| 51 | + }; |
| 52 | + |
| 53 | + zpool = { |
| 54 | + name = mkOption { |
| 55 | + type = types.str; |
| 56 | + default = "zfs_root"; |
| 57 | + description = "The name of the ZFS Pool"; |
| 58 | + }; |
| 59 | + |
| 60 | + mode = mkOption { |
| 61 | + type = types.enum [ |
| 62 | + "stripe" |
| 63 | + "mirror" |
| 64 | + "raidz1" |
| 65 | + "raidz2" |
| 66 | + "raidz3" |
| 67 | + ]; |
| 68 | + default = "stripe"; |
| 69 | + description = "Set ZFS Pool redundancy - e.g. 'mirror', 'raidz1', etc."; |
| 70 | + }; |
| 71 | + |
| 72 | + extraDatasets = mkOption { |
| 73 | + type = types.attrsOf ( |
| 74 | + types.submodule ( |
| 75 | + { name, ... }: |
| 76 | + { |
| 77 | + options = { |
| 78 | + mountpoint = mkOption { |
| 79 | + type = types.nullOr types.str; |
| 80 | + default = name; |
| 81 | + example = "/var/lib"; |
| 82 | + description = "The ZFS dataset mountpoint"; |
| 83 | + }; |
| 84 | + |
| 85 | + type = mkOption { |
| 86 | + type = types.enum [ |
| 87 | + "zfs_fs" |
| 88 | + "zfs_volume" |
| 89 | + ]; |
| 90 | + default = "zfs_fs"; |
| 91 | + description = "Type of ZFS dataset"; |
| 92 | + }; |
| 93 | + snapshot = mkEnableOption "Whether to enable ZFS snapshots"; |
| 94 | + }; |
| 95 | + |
| 96 | + } |
| 97 | + ) |
| 98 | + ); |
| 99 | + default = { }; |
| 100 | + example = { |
| 101 | + "/opt".snapshot = false; |
| 102 | + "/opt/downloads".snapshot = false; |
| 103 | + "/opt/downloads/vm-images" = { |
| 104 | + snapshot = false; |
| 105 | + options = { |
| 106 | + quota = "120G"; |
| 107 | + }; |
| 108 | + }; |
| 109 | + }; |
| 110 | + description = "Extra ZFS Pool datasets"; |
| 111 | + }; |
| 112 | + }; |
| 113 | + }; |
| 114 | + |
| 115 | + config.disko = |
| 116 | + let |
| 117 | + makePrimaryZfsDisk = import ./primaryZfsPartition.nix; |
| 118 | + makeSecondaryZfsDisk = import ./secondaryZfsPartition.nix; |
| 119 | + |
| 120 | + first = builtins.head cfg.disks; |
| 121 | + rest = builtins.tail cfg.disks; |
| 122 | + secondaryDisks = builtins.listToAttrs ( |
| 123 | + builtins.map (disk: { |
| 124 | + name = disk; |
| 125 | + value = |
| 126 | + if cfg.zpool.mode != "stripe" then |
| 127 | + makePrimaryZfsDisk { |
| 128 | + disk = first; |
| 129 | + espSize = cfg.espSize; |
| 130 | + swapSize = cfg.swapSize; |
| 131 | + legacyBoot = cfg.legacyBoot; |
| 132 | + poolName = cfg.zpool.name; |
| 133 | + } |
| 134 | + else |
| 135 | + makeSecondaryZfsDisk { |
| 136 | + poolName = cfg.zpool.name; |
| 137 | + inherit disk; |
| 138 | + }; |
| 139 | + }) rest |
| 140 | + ); |
| 141 | + in |
| 142 | + lib.mkIf cfg.enable { |
| 143 | + devices = { |
| 144 | + disk = secondaryDisks // { |
| 145 | + "${first}" = makePrimaryZfsDisk { |
| 146 | + disk = first; |
| 147 | + espSize = cfg.espSize; |
| 148 | + swapSize = cfg.swapSize; |
| 149 | + legacyBoot = cfg.legacyBoot; |
| 150 | + poolName = cfg.zpool.name; |
| 151 | + }; |
| 152 | + }; |
| 153 | + zpool = import ./zpool.nix { |
| 154 | + poolName = cfg.zpool.name; |
| 155 | + poolMode = cfg.zpool.mode; |
| 156 | + poolExtraDatasets = cfg.zpool.extraDatasets; |
| 157 | + inherit lib; |
| 158 | + }; |
| 159 | + }; |
| 160 | + }; |
| 161 | + }; |
| 162 | +} |
0 commit comments