about summary refs log tree commit diff stats
path: root/system/disks
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--system/disks/default.nix120
-rw-r--r--system/disks/fstrim.nix44
-rw-r--r--system/disks/hibernate.nix42
3 files changed, 206 insertions, 0 deletions
diff --git a/system/disks/default.nix b/system/disks/default.nix
new file mode 100644
index 00000000..33e1fa33
--- /dev/null
+++ b/system/disks/default.nix
@@ -0,0 +1,120 @@
+{
+  config,
+  lib,
+  pkgs,
+  ...
+}: let
+  cfg = config.soispha.disks;
+  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.soispha.disks = {
+    disk = lib.mkOption {
+      type = lib.types.path;
+      example = lib.literalExpression "/dev/disk/by-uuid/0442cb6d-f13a-4635-b487-fa76189774c5";
+      description = lib.mdDoc "The disk used for installing the OS";
+    };
+    ssd = lib.mkOption {
+      type = lib.types.bool;
+      example = lib.literalExpression "true";
+      default = false;
+      description = lib.mdDoc "Enable ssd specific improvements, like trim";
+    };
+    swap = {
+      uuid = lib.mkOption {
+        type = lib.types.str;
+        example = lib.literalExpression "d1d20ae7-3d8a-44da-86da-677dbbb10c89";
+        description = lib.mdDoc "The uuid of the swapfile";
+      };
+      resumeOffset = lib.mkOption {
+        type = lib.types.str;
+        example = lib.literalExpression "134324224";
+        description = lib.mdDoc "The resume offset of the swapfile";
+      };
+    };
+  };
+
+  config = {
+    systemd = lib.recursiveUpdate (import ./hibernate.nix {inherit pkgs;}) (import ./fstrim.nix {inherit pkgs lib cfg;});
+
+    disko.devices = {
+      disk = {
+        main = {
+          device = cfg.mainDisk;
+          content = {
+            type = "gpt";
+            partitions = {
+              root = {
+                size = "100%";
+                name = "root";
+                content = {
+                  type = "btrfs";
+                  extraArgs = ["-f"]; # Override existing partitions
+                  subvolumes = {
+                    "nix" = {
+                      mountpoint = "/nix";
+                      mountOptions = defaultMountOptions;
+                    };
+                    "persistent-storage" = {
+                      mountpoint = "/srv";
+                      mountOptions = defaultMountOptions;
+                    };
+                    "swap" = {
+                      mountpoint = "/swap";
+                      mountOptions = defaultMountOptions;
+                    };
+                  };
+                };
+              };
+              boot = {
+                type = "EF00";
+                size = "512M";
+                name = "boot";
+                content = {
+                  type = "filesystem";
+                  format = "vfat";
+                  mountpoint = "/boot";
+                };
+              };
+            };
+          };
+        };
+      };
+      nodev = {
+        "/" = {
+          fsType = "tmpfs";
+          mountOptions = ["defaults" "size=4G" "mode=755"];
+        };
+      };
+    };
+    fileSystems = {
+      "/srv" = {
+        neededForBoot = true;
+      };
+      "/swap" = {
+        neededForBoot = true;
+      };
+    };
+    swapDevices = [
+      {
+        device = "/swap/swapfile";
+        priority = 1; # lower than zramSwap, just in case
+        # size = 2048; # TODO can nixos create a btrfs swapfile correctly?
+      }
+    ];
+    zramSwap = {
+      enable = true;
+      priority = 10; # needs to be higher than hardware-swap
+    };
+    boot.kernelParams = [
+      "resume=UUID=${cfg.swap.uuid}"
+      "resume_offset=${cfg.swap.resumeOffset}"
+      "zswap.enabled=0" # zswap and zram are not really compatible
+    ];
+  };
+}
+# vim: ts=2
+
diff --git a/system/disks/fstrim.nix b/system/disks/fstrim.nix
new file mode 100644
index 00000000..6a8da5be
--- /dev/null
+++ b/system/disks/fstrim.nix
@@ -0,0 +1,44 @@
+{
+  pkgs,
+  lib,
+  cfg,
+}: {
+  timers.fstrim = lib.mkIf cfg.ssd {
+    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 = lib.mkIf cfg.ssd {
+    wantedBy = lib.mkForce [];
+    unitConfig = {
+      Description = "Discard unused blocks on filesystems from /etc/fstab";
+      Documentation = "man:fstrim(8)";
+      ConditionVirtualization = "!container";
+    };
+    serviceConfig = {
+      Type = "oneshot";
+      ExecStart = "${pkgs.util-linux}/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";
+    };
+  };
+}
+# vim: ts=2
+
diff --git a/system/disks/hibernate.nix b/system/disks/hibernate.nix
new file mode 100644
index 00000000..5bb0f8d5
--- /dev/null
+++ b/system/disks/hibernate.nix
@@ -0,0 +1,42 @@
+{pkgs}: {
+  services = {
+    hibernate-preparation = {
+      # TODO check if they work
+      wantedBy = ["systemd-hibernate.service"];
+      unitConfig = {
+        Description = "Enable swap file and disable zram before hibernate";
+        Before = "systemd-hibernate.service";
+      };
+      serviceConfig = {
+        Type = "oneshot";
+        User = "root";
+        ExecStart = "${pkgs.dash}/bin/dash -c \"${pkgs.util-linux}/bin/swapon /swap/swapfile && ${pkgs.util-linux}/bin/swapoff /dev/zram0\"";
+      };
+    };
+    hibernate-resume = {
+      wantedBy = ["hibernate.target"];
+      unitConfig = {
+        Description = "Disable swap after resuming from hibernation";
+        After = "hibernate.target";
+      };
+      serviceConfig = {
+        Type = "oneshot";
+        User = "root";
+        ExecStart = "${pkgs.util-linux}/bin/swapoff /swap/swapfile";
+      };
+    };
+    swapoff-start = {
+      wantedBy = ["multi-user.target"];
+      unitConfig = {
+        Description = "Disable hardware swap after booting";
+      };
+      serviceConfig = {
+        Type = "oneshot";
+        User = "root";
+        ExecStart = "${pkgs.util-linux}/bin/swapoff /swap/swapfile";
+      };
+    };
+    #systemd-hibernate.serviceConfig.Environment = "SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1";
+    #systemd-logind.serviceConfig.Environment = "SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1";
+  };
+}