From e8de4455ab0230d88b39a580a934ace69f5110ca Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 12 Oct 2012 16:47:11 -0400 Subject: [PATCH] Update automatic swapfile creation for systemd --- modules/config/swap.nix | 31 +++++++++++++++++++++++++++++++ modules/tasks/filesystems.nix | 10 ---------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/modules/config/swap.nix b/modules/config/swap.nix index 5b20f657e12..776574bc016 100644 --- a/modules/config/swap.nix +++ b/modules/config/swap.nix @@ -74,9 +74,40 @@ with pkgs.lib; }; config = mkIf ((length config.swapDevices) != 0) { + system.requiredKernelConfig = with config.lib.kernelConfig; [ (isYes "SWAP") ]; + + # Create missing swapfiles. + # FIXME: support changing the size of existing swapfiles. + boot.systemd.services = + let + + escapePath = s: # FIXME: slow + replaceChars ["/" "-"] ["-" "\\x2d"] (substring 1 (stringLength s) s); + + createSwapDevice = sw: assert sw.device != ""; nameValuePair "mkswap-${escapePath sw.device}" + { description = "Initialisation of Swapfile ${sw.device}"; + wantedBy = [ "${escapePath sw.device}.swap" ]; + before = [ "${escapePath sw.device}.swap" ]; + path = [ pkgs.utillinux ]; + script = + '' + if [ ! -e "${sw.device}" ]; then + fallocate -l ${toString sw.size}M "${sw.device}" || + dd if=/dev/zero of="${sw.device}" bs=1M count=${toString sw.size} + mkswap ${sw.device} + fi + ''; + unitConfig.RequiresMountsFor = "${dirOf sw.device}"; + unitConfig.DefaultDependencies = false; # needed to prevent a cycle + serviceConfig.Type = "oneshot"; + serviceConfig.RemainAfterExit = true; + }; + + in listToAttrs (map createSwapDevice (filter (sw: sw.size != null) config.swapDevices)); + }; } diff --git a/modules/tasks/filesystems.nix b/modules/tasks/filesystems.nix index 8bc0b7531c0..6c321b23c7e 100644 --- a/modules/tasks/filesystems.nix +++ b/modules/tasks/filesystems.nix @@ -213,16 +213,6 @@ in fi '')} - # Create missing swapfiles. - # FIXME: support changing the size of existing swapfiles. - ${flip concatMapStrings config.swapDevices (sw: optionalString (sw.size != null) '' - if [ ! -e "${sw.device}" -a -e "$(dirname "${sw.device}")" ]; then - # FIXME: use ‘fallocate’ on filesystems that support it. - dd if=/dev/zero of="${sw.device}" bs=1M count=${toString sw.size} - mkswap ${sw.device} - fi - '')} - ''; daemonType = "daemon";