summaryrefslogtreecommitdiff
path: root/sys/fs/layout.nix
blob: 7e1ac2e3227bdb74b65e93a2c869b990f6da5c58 (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
{ lib, config, ... }:
with lib; let
  cfg = config.local;
in
{
  options.local.fs.layout = with lib.types; {
    sysHddBtrfs = mkOption {
      default = null;

      type = nullOr (submodule {
        options = {
          sys = {
            device = mkOption {
              type = str;
            };

            ssd = mkOption {
              type = bool;
            };

            root = mkOption {
              type = str;
            };

            toplevel = mkOption {
              type = str;
            };
          };

          hdd = {
            device = mkOption {
              type = str;
            };

            home = mkOption {
              type = str;
            };
          };
        };
      });
    };
  };

  config = {
    local.fs.btrfs =
      let
        sysHddBtrfs = layout: {
          "/" = {
            inherit (layout.sys) device ssd;
            subvol = layout.sys.root;
          };

          "/toplevel" = {
            inherit (layout.sys) device ssd;
            subvol = layout.sys.toplevel;
          };

          "/hdd" = {
            inherit (layout.hdd) device;
            subvol = "/";
            ssd = false;
          };

          "/home" = {
            inherit (layout.hdd) device;
            subvol = layout.hdd.home;
            ssd = false;
            snapper = "home";
          };
        };

        inherit (cfg.fs) layout;

        layoutMaps = [ sysHddBtrfs ];
        layoutOpts = [ layout.sysHddBtrfs ];
        valid = filter ({ snd, ... }: snd != null) (zipLists layoutMaps layoutOpts);
      in
      optionalAttrs (valid != [ ]) ((head valid).fst (head valid).snd);

    assertions = [
      {
        assertion = length (filter (layout: layout != null) (attrValues cfg.fs.layout)) <= 1;
        message = "Cannot enable more than one filesystem layout";
      }
    ];
  };
}