From a7aaedcf259fc8b41670396f32a08b6900f16da9 Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Tue, 13 Aug 2024 09:42:17 +0100 Subject: [PATCH] feat: add swap module Configures `swapDevices` based on the active swap partitions when the report was captured. Intended to match the behaviour of `nixos-generate-config` https://github.com/NixOS/nixpkgs/blob/dac9cdf8c930c0af98a63cbfe8005546ba0125fb/nixos/modules/installer/tools/nixos-generate-config.pl#L335-L357 Signed-off-by: Brian McGee --- modules/nixos/facter.nix | 1 + modules/nixos/swap.nix | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 modules/nixos/swap.nix diff --git a/modules/nixos/facter.nix b/modules/nixos/facter.nix index beb0b50..ce56d81 100644 --- a/modules/nixos/facter.nix +++ b/modules/nixos/facter.nix @@ -14,6 +14,7 @@ in ./networking ./virtualisation.nix ./firmware.nix + ./swap.nix ./system.nix ]; diff --git a/modules/nixos/swap.nix b/modules/nixos/swap.nix new file mode 100644 index 0000000..d8a8693 --- /dev/null +++ b/modules/nixos/swap.nix @@ -0,0 +1,30 @@ +{ lib, config, ... }: +let + inherit (config.facter) report; + +in +{ + options.facter.swap.enable = lib.mkEnableOption "Enable the Facter Swap module" // { + default = lib.length (report.swap or [ ]) > 0; + defaultText = "enabled if there are swap entries in the report"; + }; + + # generate the swapDevices option from the swap devices that were active when the report was captured + config = lib.mkIf config.facter.swap.enable { + swapDevices = + let + # we only take swap partitions that are not zram devices + # https://github.com/NixOS/nixpkgs/blob/dac9cdf8c930c0af98a63cbfe8005546ba0125fb/nixos/modules/installer/tools/nixos-generate-config.pl#L335-L357 + swapPartitions = lib.filter ( + { filename, type, ... }: type == "partition" && !(lib.hasPrefix "/dev/zram" filename) + ) report.swap; + in + map ( + { filename, ... }: + { + device = filename; + } + ) swapPartitions; + }; + +}