about summary refs log tree commit diff stats
path: root/system/fileSystemLayouts/default.nix
blob: 3edf0a797dcb71d5da8bf1893f86425217a66e6e (plain) (blame)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# vim: ts=2
{
  config,
  lib,
  ...
}:
with lib; let
  cfg = config.system.fileSystemLayouts;
  defaultMountOptions = [
    "compress-force=zstd:15" # This saves disk space, at a performance cost
    "noatime" # should have some performance upsides, and I don't use it anyways
    "lazytime" # make time changes in memory
  ];
in {
  options.system.fileSystemLayouts = {
    enable = mkEnableOption (mdDoc "fileSystemLayout");
    mainDisk = mkOption {
      type = lib.types.path;
      example = literalExpression "/dev/disk/by-uuid/0442cb6d-f13a-4635-b487-fa76189774c5";
      description = lib.mdDoc "Path to the main disk";
    };
    efiDisk = mkOption {
      type = lib.types.path;
      example = literalExpression "/dev/disk/by-uuid/5143-6136";
      description = lib.mdDoc "Path to the main disk";
    };
    ssd = mkOption {
      type = lib.types.bool;
      example = literalExpression "true";
      default = false;
      description = lib.mdDoc "Enable ssd specific improvements?";
    };
    swap = {
      uuid = mkOption {
        type = lib.types.str;
        example = literalExpression "d1d20ae7-3d8a-44da-86da-677dbbb10c89";
        description = lib.mdDoc "The uuid of the swapfile";
      };
      resumeOffset = mkOption {
        type = lib.types.str;
        example = literalExpression "134324224";
        description = lib.mdDoc "The resume offset of the swapfile";
      };
    };
  };

  config = mkIf cfg.enable {
    systemd = mkIf cfg.ssd {
      timers.fstrim = {
        wantedBy = ["timers.target"];
        wants = ["fstrim.service"];
        unitConfig = {
          Description = "Discard unused blocks once a week";
          Documentation = "man:fstrim";
          ConditionVirtualization = "!container";
          ConditionPathExists = "!/etc/initrd-release";
        };
        timerConfig = {
          OnCalendar = "weekly";
          AccuracySec = "1h";
          Persistent = "true";
          RandomizedDelaySec = "6000";
        };
      };
      services.fstrim = {
        unitConfig = {
          Description = "Discard unused blocks on filesystems from /etc/fstab";
          Documentation = "man:fstrim(8)";
          ConditionVirtualization = "!container";
        };
        serviceConfig = {
          Type = "oneshot";
          ExecStart = "/usr/bin/fstrim --listed-in /etc/fstab:/proc/self/mountinfo --verbose --quiet-unsupported";
          PrivateDevices = "no";
          PrivateNetwork = "yes";
          PrivateUsers = "no";
          ProtectKernelTunables = "yes";
          ProtectKernelModules = "yes";
          ProtectControlGroups = "yes";
          MemoryDenyWriteExecute = "yes";
          SystemCallFilter = "@default @file-system @basic-io @system-service";
        };
      };
    };

    fileSystems = {
      "/" = {
        device = "none";
        fsType = "tmpfs";
        options = ["defaults" "size=4G" "mode=755"];
      };
      "/nix" = {
        device = cfg.mainDisk;
        fsType = "btrfs";
        options = ["subvol=nix-store"] ++ defaultMountOptions;
      };
      "/srv" = {
        device = cfg.mainDisk;
        fsType = "btrfs";
        neededForBoot = true;
        options = ["subvol=persistent-storage"] ++ defaultMountOptions;
      };
      "/swap" = {
        device = cfg.mainDisk;
        fsType = "btrfs";
        neededForBoot = true;
        options = ["subvol=swap"] ++ defaultMountOptions;
      };
      "/boot" = {
        device = cfg.efiDisk;
        fsType = "vfat";
      };
    };
    swapDevices = [
      {
        device = "/swap/swapfile";
        discardPolicy = "both"; # TODO this is the default in swapon, so it should be fine?
        priority = 1; # should not be used, aside of hibernate
      }
    ];
    zramSwap = {
      enable = true;
      priority = 10; # needs to be higher than harware-swap
    };
    boot.kernelParams = [
      "resume=UUID=${cfg.swap.uuid}"
      "resume_offset=${cfg.swap.resumeOffset}"
    ];
  };
}