From 8a02c0507c0b3e05e07133d812fc39700df4fb4f Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Mon, 6 May 2024 00:10:38 +0200 Subject: [PATCH 1/2] swap: support swapDevices.*.discardPolicy option --- example/swap.nix | 1 + lib/types/swap.nix | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/example/swap.nix b/example/swap.nix index 5d59004..6cf965a 100644 --- a/example/swap.nix +++ b/example/swap.nix @@ -35,6 +35,7 @@ size = "100%"; content = { type = "swap"; + discardPolicy = "both"; resumeDevice = true; # resume from hiberation from this device }; }; diff --git a/lib/types/swap.nix b/lib/types/swap.nix index 26202ca..372641d 100644 --- a/lib/types/swap.nix +++ b/lib/types/swap.nix @@ -11,6 +11,18 @@ default = device; description = "Device"; }; + discardPolicy = lib.mkOption { + default = null; + example = "once"; + type = lib.types.nullOr (lib.types.enum [ "once" "pages" "both" ]); + description = lib.mdDoc '' + Specify the discard policy for the swap device. If "once", then the + whole swap space is discarded at swapon invocation. If "pages", + asynchronous discard on freed pages is performed, before returning to + the available pages pool. With "both", both policies are activated. + See swapon(8) for more information. + ''; + }; extraArgs = lib.mkOption { type = lib.types.listOf lib.types.str; default = [ ]; @@ -53,7 +65,11 @@ default = lib.optionalAttrs (!config.randomEncryption) { fs.${config.device} = '' if ! swapon --show | grep -q "^$(readlink -f ${config.device}) "; then - swapon ${config.device} + swapon ${ + lib.optionalString (config.discardPolicy != null) + "--discard${lib.optionalString (config.discardPolicy != "both") + "=${config.discardPolicy}" + }"} ${config.device} fi ''; }; @@ -64,7 +80,7 @@ default = [{ swapDevices = [{ device = config.device; - randomEncryption = config.randomEncryption; + inherit (config) discardPolicy randomEncryption; }]; boot.resumeDevice = lib.mkIf config.resumeDevice config.device; }]; From c5a25d5cedf6830fc225065a72a91f4890d2ef48 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Mon, 6 May 2024 11:43:53 +0200 Subject: [PATCH 2/2] swap: support swapDevices.*.priority option This just forwards to the underlying NixOS option and expands the mount script to exhibit the same behavior. --- example/swap.nix | 1 + lib/types/swap.nix | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/example/swap.nix b/example/swap.nix index 6cf965a..a6df51c 100644 --- a/example/swap.nix +++ b/example/swap.nix @@ -29,6 +29,7 @@ content = { type = "swap"; randomEncryption = true; + priority = 100; # prefer to encrypt as long as we have space for it }; }; plainSwap = { diff --git a/lib/types/swap.nix b/lib/types/swap.nix index 372641d..d56871a 100644 --- a/lib/types/swap.nix +++ b/lib/types/swap.nix @@ -28,6 +28,15 @@ default = [ ]; description = "Extra arguments"; }; + priority = lib.mkOption { + type = lib.types.nullOr lib.types.int; + default = null; + description = lib.mdDoc '' + Specify the priority of the swap device. Priority is a value between 0 and 32767. + Higher numbers indicate higher priority. + null lets the kernel choose a priority, which will show up as a negative value. + ''; + }; randomEncryption = lib.mkOption { type = lib.types.bool; default = false; @@ -69,7 +78,10 @@ lib.optionalString (config.discardPolicy != null) "--discard${lib.optionalString (config.discardPolicy != "both") "=${config.discardPolicy}" - }"} ${config.device} + }"} ${ + lib.optionalString (config.priority != null) + "--priority=${toString config.priority}" + } ${config.device} fi ''; }; @@ -80,7 +92,7 @@ default = [{ swapDevices = [{ device = config.device; - inherit (config) discardPolicy randomEncryption; + inherit (config) discardPolicy priority randomEncryption; }]; boot.resumeDevice = lib.mkIf config.resumeDevice config.device; }];