aboutsummaryrefslogtreecommitdiff
path: root/infra/modules/base/zfs.nix
blob: 4a818e72a0cccc4df083fa758cb2e8982430d0c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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 = config.infra.persist.dir;
              options = {
                canmount = "on";
                mountpoint = "legacy";
              };
              mountOptions = [
                "nosuid"
                "nodev"
              ];
            };
          };
        };
      };
    };
}