From 1944356d054ee92e7aa90dd9d25ee6a45bc5f4aa Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 7 Dec 2024 21:14:58 +0100 Subject: [PATCH] Add support for partitioning btrfs volumes --- lib/modules/fileSystems.nix | 1 + lib/modules/fileSystems/btrfs.nix | 108 ++++++++++++++++++++++++++++ profiles/machines/manuel/server.nix | 47 +++++++----- 3 files changed, 138 insertions(+), 18 deletions(-) create mode 100644 lib/modules/fileSystems/btrfs.nix diff --git a/lib/modules/fileSystems.nix b/lib/modules/fileSystems.nix index f3632f86..7f6d2368 100644 --- a/lib/modules/fileSystems.nix +++ b/lib/modules/fileSystems.nix @@ -105,6 +105,7 @@ in '' ])} ${cfg.diskSetup.scripts.partition} + ${cfg.btrfs.script} ${mountScript} ${cfg.diskSetup.scripts.swap} ''; diff --git a/lib/modules/fileSystems/btrfs.nix b/lib/modules/fileSystems/btrfs.nix new file mode 100644 index 00000000..82dca6d1 --- /dev/null +++ b/lib/modules/fileSystems/btrfs.nix @@ -0,0 +1,108 @@ +{ lib, config, ... }: +let + inherit (lib) types mkOption; + cfg = config.valhalla.fileSystems.btrfs; + + profileType = types.enum [ + "raid0" + "raid1" + "raid1c3" + "raid1c4" + "raid5" + "raid6" + "raid10" + "dup" + "single" + ]; + + volumeType = types.submodule ( + { config, name, ... }: { + options = { + mountPoint = mkOption { + type = types.nullOr types.str; + description = "The path to mount the volume to."; + default = null; + }; + + devices = mkOption { + type = types.listOf types.str; + description = "The devices of the btrfs volume."; + }; + + label = mkOption { + type = types.nullOr types.str; + description = "The label of the volume."; + default = name; + }; + + dataProfile = mkOption { + type = types.nullOr profileType; + description = "The data profile."; + default = null; + }; + + metadataProfile = mkOption { + type = types.nullOr profileType; + description = "The metadata profile."; + default = null; + }; + }; + } + ); +in +{ + options = { + valhalla = { + fileSystems.btrfs = { + volumes = mkOption { + type = types.attrsOf volumeType; + description = "The btrfs volumes of the system."; + default = { }; + }; + + script = mkOption { + type = types.str; + description = "The script for creating the btrfs volumes."; + default = lib.strings.concatLines ( + builtins.map + ( + _: builtins.concatStringsSep " " ( + [ "mkfs.btrfs" ] ++ + (lib.optionals (_.metadataProfile != null) [ "--metadata" "${_.metadataProfile}" ]) ++ + (lib.optionals (_.dataProfile != null) [ "--data" "${_.dataProfile}" ]) ++ + (lib.optionals (_.label != null) [ "--label" "${_.label}" ]) ++ + _.devices + ) + ) + (builtins.attrValues cfg.volumes) + ); + }; + }; + }; + }; + + config = { + valhalla = { + linux.programs.btrfs = lib.optionalAttrs + (builtins.any + (_: (builtins.length _.devices) > 1) + (builtins.attrValues cfg.volumes)) + { + enable = true; + pools = true; + }; + + fileSystems.mounts = lib.attrsets.concatMapAttrs + ( + name: volume: + if (volume.mountPoint != null) then { + ${volume.mountPoint} = { + device = builtins.elemAt volume.devices 0; + fsType = "btrfs"; + }; + } else { } + ) + cfg.volumes; + }; + }; +} diff --git a/profiles/machines/manuel/server.nix b/profiles/machines/manuel/server.nix index a7a04c93..c88b9041 100644 --- a/profiles/machines/manuel/server.nix +++ b/profiles/machines/manuel/server.nix @@ -5,28 +5,39 @@ in { config = { valhalla = { - fileSystems.diskSetup.devices = { - OS = { - partitions = { - Boot = { - index = 1; - type = "uefi"; - size = "+1G"; - format = fs.fat32; - mountPoint = config.valhalla.boot.efiMountPoint; - }; + fileSystems = { + diskSetup.devices = { + OS = { + path = "/dev/sda"; - Swap = { - index = 2; - type = "swap"; - }; + partitions = { + Boot = { + index = 1; + type = "uefi"; + size = "+1G"; + format = fs.fat32; + mountPoint = config.valhalla.boot.efiMountPoint; + }; + Swap = { + index = 2; + type = "swap"; + }; + + OS = { + index = 3; + label = lib.mkDefault config.valhalla.boot.label; + type = "linux"; + }; + }; + }; + }; + + btrfs = { + volumes = { OS = { - index = 3; - label = lib.mkDefault config.valhalla.boot.label; - type = "linux"; - format = fs.ext4; mountPoint = "/"; + devices = [ "/dev/sda3" "/dev/sdb" ]; }; }; };