about summary refs log tree commit diff stats
path: root/system/fileSystemLayouts/default.nix
diff options
context:
space:
mode:
authorene <ene@sils.li>2023-04-04 08:07:54 +0200
committerene <ene@sils.li>2023-04-04 08:14:27 +0200
commit37056f6b9c6e268ad0febf414ee6f10048534836 (patch)
tree748c8b3af641f910256f6f36001ed4ce8c67b602 /system/fileSystemLayouts/default.nix
parentFeat(hm/conf/alacritty): Hint for selecting paths (diff)
downloadnixos-config-37056f6b9c6e268ad0febf414ee6f10048534836.tar.gz
nixos-config-37056f6b9c6e268ad0febf414ee6f10048534836.zip
Fix(system/fsl): Use swapfile only for hibernate
Diffstat (limited to '')
-rw-r--r--system/fileSystemLayouts/default.nix73
1 files changed, 48 insertions, 25 deletions
diff --git a/system/fileSystemLayouts/default.nix b/system/fileSystemLayouts/default.nix
index 3edf0a79..1676d993 100644
--- a/system/fileSystemLayouts/default.nix
+++ b/system/fileSystemLayouts/default.nix
@@ -2,9 +2,9 @@
 {
   config,
   lib,
+  pkgs,
   ...
-}:
-with lib; let
+}: let
   cfg = config.system.fileSystemLayouts;
   defaultMountOptions = [
     "compress-force=zstd:15" # This saves disk space, at a performance cost
@@ -13,40 +13,68 @@ with lib; let
   ];
 in {
   options.system.fileSystemLayouts = {
-    enable = mkEnableOption (mdDoc "fileSystemLayout");
-    mainDisk = mkOption {
+    enable = lib.mkEnableOption (lib.mdDoc "fileSystemLayout");
+    mainDisk = lib.mkOption {
       type = lib.types.path;
-      example = literalExpression "/dev/disk/by-uuid/0442cb6d-f13a-4635-b487-fa76189774c5";
+      example = lib.literalExpression "/dev/disk/by-uuid/0442cb6d-f13a-4635-b487-fa76189774c5";
       description = lib.mdDoc "Path to the main disk";
     };
-    efiDisk = mkOption {
+    efiDisk = lib.mkOption {
       type = lib.types.path;
-      example = literalExpression "/dev/disk/by-uuid/5143-6136";
+      example = lib.literalExpression "/dev/disk/by-uuid/5143-6136";
       description = lib.mdDoc "Path to the main disk";
     };
-    ssd = mkOption {
+    ssd = lib.mkOption {
       type = lib.types.bool;
-      example = literalExpression "true";
+      example = lib.literalExpression "true";
       default = false;
       description = lib.mdDoc "Enable ssd specific improvements?";
     };
     swap = {
-      uuid = mkOption {
+      uuid = lib.mkOption {
         type = lib.types.str;
-        example = literalExpression "d1d20ae7-3d8a-44da-86da-677dbbb10c89";
+        example = lib.literalExpression "d1d20ae7-3d8a-44da-86da-677dbbb10c89";
         description = lib.mdDoc "The uuid of the swapfile";
       };
-      resumeOffset = mkOption {
+      resumeOffset = lib.mkOption {
         type = lib.types.str;
-        example = literalExpression "134324224";
+        example = lib.literalExpression "134324224";
         description = lib.mdDoc "The resume offset of the swapfile";
       };
     };
   };
 
-  config = mkIf cfg.enable {
-    systemd = mkIf cfg.ssd {
-      timers.fstrim = {
+  config = lib.mkIf cfg.enable {
+    systemd = {
+      services = {
+        hibernate-preparation = {
+          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";
+          };
+        };
+        systemd-hibernate.serviceConfig.Environment = "SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1";
+        systemd-logind.serviceConfig.Environment = "SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1";
+      };
+      timers.fstrim = lib.mkIf cfg.ssd {
         wantedBy = ["timers.target"];
         wants = ["fstrim.service"];
         unitConfig = {
@@ -62,7 +90,7 @@ in {
           RandomizedDelaySec = "6000";
         };
       };
-      services.fstrim = {
+      services.fstrim = lib.mkIf cfg.ssd {
         unitConfig = {
           Description = "Discard unused blocks on filesystems from /etc/fstab";
           Documentation = "man:fstrim(8)";
@@ -70,7 +98,7 @@ in {
         };
         serviceConfig = {
           Type = "oneshot";
-          ExecStart = "/usr/bin/fstrim --listed-in /etc/fstab:/proc/self/mountinfo --verbose --quiet-unsupported";
+          ExecStart = "${pkgs.util-linux}/bin/fstrim --listed-in /etc/fstab:/proc/self/mountinfo --verbose --quiet-unsupported";
           PrivateDevices = "no";
           PrivateNetwork = "yes";
           PrivateUsers = "no";
@@ -111,13 +139,7 @@ in {
         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
-      }
-    ];
+    swapDevices = [];
     zramSwap = {
       enable = true;
       priority = 10; # needs to be higher than harware-swap
@@ -125,6 +147,7 @@ in {
     boot.kernelParams = [
       "resume=UUID=${cfg.swap.uuid}"
       "resume_offset=${cfg.swap.resumeOffset}"
+      "zswap.enabled=0" # zswap and zram are not really compatible
     ];
   };
 }