From 69114148f8f279d4f5456bd88fedb9d537bed7e5 Mon Sep 17 00:00:00 2001 From: vkcku Date: Mon, 1 Jun 2026 12:01:49 +0530 Subject: infra: add ZFS configuration to base module monorepo-revid: ba5f961c68f194c29b75984a70b68dcb96124c3b --- infra/modules/base/zfs.nix | 116 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 infra/modules/base/zfs.nix (limited to 'infra/modules/base/zfs.nix') diff --git a/infra/modules/base/zfs.nix b/infra/modules/base/zfs.nix new file mode 100644 index 0000000..e2cb007 --- /dev/null +++ b/infra/modules/base/zfs.nix @@ -0,0 +1,116 @@ +{ + flake.modules.nixos.base = + { + config, + lib, + pkgs, + ... + }: + let + cfg = config.infra.zfs; + in + { + options.infra.zfs = { + pool = lib.mkOption { + type = lib.types.str; + description = "The name of the ZFS pool."; + }; + + reservation = lib.mkOption { + type = lib.types.strMatching "[0-9]+[KMGTP]?"; + description = "The amount of space to reserve for the reservation dataset."; + default = "10G"; + }; + }; + + config = { + boot = { + kernelPackages = pkgs.linuxPackages_7_0; + zfs.forceImportRoot = false; + }; + + assertions = + let + expectedZfsVersion = "2.4.2"; + in + [ + { + assertion = pkgs.zfs.version == expectedZfsVersion; + message = '' + ZFS version is ${pkgs.zfs.version}, expected ${expectedZfsVersion}. Check the latest supported kernel + version at https://github.com/openzfs/zfs/releases and update `boot.kernelPackages` + in modules/zfs.nix if needed as well as this assertion. + ''; + } + ]; + + disko.devices.zpool."${cfg.pool}" = { + type = "zpool"; + + rootFsOptions = { + acltype = "posix"; + atime = "off"; + canmount = "off"; + compression = "zstd-3"; + dnodesize = "auto"; + setuid = "off"; + xattr = "sa"; + normalization = "formD"; + utf8only = "on"; + }; + + datasets = { + reservation = { + type = "zfs_fs"; + options = { + reservation = cfg.reservation; + mountpoint = "legacy"; + }; + }; + + "local/nix" = { + type = "zfs_fs"; + mountpoint = "/nix"; + options = { + canmount = "on"; + mountpoint = "legacy"; + }; + mountOptions = [ + "nosuid" + "nodev" + ]; + }; + + "local/root" = { + type = "zfs_fs"; + mountpoint = "/"; + options = { + canmount = "on"; + mountpoint = "legacy"; + }; + postCreateHook = '' + zfs snapshot "${cfg.pool}/local/root@blank" + ''; + mountOptions = [ + "nosuid" + "nodev" + ]; + }; + + "persist" = { + type = "zfs_fs"; + mountpoint = "/persist"; + options = { + canmount = "on"; + mountpoint = "legacy"; + }; + mountOptions = [ + "nosuid" + "nodev" + ]; + }; + }; + }; + }; + }; +} -- cgit v1.3.1