summaryrefslogtreecommitdiff
path: root/modules/xnet/disk/default.nix
blob: aca6ae2a93d982e43287da5c39ca89b7b91b2b5e (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
{ config, lib, modulesPath, ... }:
let
  cfg = config.xnet.disk;
  commonOpts = {
    acltype = "posixacl";
    atime = "off";
    compression = "on";
    normalization = "formD";
    relatime = "off";
    xattr = "sa";
    "com.sun:auto-snapshot" = "false";
  };

  inherit (lib) mkOption mkDefault mkIf types;
in
{
  imports = [
    ./boot.nix
    (modulesPath + "/installer/scan/not-detected.nix")
  ];

  options.xnet.disk = {
    enable = mkOption {
      type = types.bool;
      default = false;
      description = "Apply xnet-standard ZFS disk layout.";
    };

    device = mkOption {
      type = types.str;
      description = "Underlying device that build the root ZFS pool.";
    };

    extraDatasets = mkOption { };
  };

  config = mkIf cfg.enable {
    networking.hostId = builtins.substring 0 8
      (builtins.hashString "md5" config.networking.hostName);

    services.zfs = {
      autoScrub.enable = true;
      trim.enable = true;
    };

    zramSwap.enable = mkDefault true;

    boot = {
      kernelParams = [ "nohibernate" "elevator=none" ];
      supportedFilesystems = [ "vfat" "zfs" ];
      zfs.devNodes = "/dev/disk/by-partuuid";
    };

    disko.devices = (import ./layouts/single.nix cfg.device) // {
      nodev."/" = {
        fsType = "tmpfs";
        mountOptions = [ "defaults" "size=2G" "mode=755" ];
      };

      zpool.zroot = {
        type = "zpool";
        options = {
          ashift = "12";
          autotrim = "on";
        };

        datasets = {
          "local" = {
            type = "zfs_fs";
            options = commonOpts;
          };

          "local/reserved" = {
            type = "zfs_fs";
            options = {
              refreservation = "10G";
              mountpoint = "none";
            };
          };

          "local/nix" = {
            type = "zfs_fs";
            mountpoint = "/nix";
            options.mountpoint = "legacy";
          };

          "local/certs" = {
            type = "zfs_fs";
            mountpoint = "/certs";
            options.mountpoint = "legacy";
          };

          "persist" = {
            type = "zfs_fs";
            mountpoint = "/persist";
            options = { mountpoint = "legacy"; } // commonOpts;
          };

          "persist/data" = {
            type = "zfs_fs";
            mountpoint = "/persist/data";
            options.mountpoint = "legacy";
          };
        } // cfg.extraDatasets;
      };
    };

    users.groups.backup = {
      members = [ config.services.syncoid.group ];
      gid = 2001;
    };
  };
}