diff --git a/modules/config/swap.nix b/modules/config/swap.nix
index 550cd4755af7..f0a6ceeda81c 100644
--- a/modules/config/swap.nix
+++ b/modules/config/swap.nix
@@ -33,13 +33,22 @@ with pkgs.lib;
device = mkOption {
example = "/dev/sda3";
- type = types.string;
+ type = types.uniq types.string;
description = "Path of the device.";
};
label = mkOption {
example = "swap";
- type = types.string;
+ type = types.uniq types.string;
+ description = ''
+ Label of the device. Can be used instead of device.
+ '';
+ };
+
+ size = mkOption {
+ default = null;
+ example = "swap";
+ type = types.nullOr types.int;
description = ''
Label of the device. Can be used instead of device.
'';
diff --git a/modules/tasks/filesystems.nix b/modules/tasks/filesystems.nix
index 5848e21688c5..9495cac67855 100644
--- a/modules/tasks/filesystems.nix
+++ b/modules/tasks/filesystems.nix
@@ -22,7 +22,7 @@ let
# Swap devices.
${flip concatMapStrings config.swapDevices (sw:
- "${sw.device} none swap\n"
+ "${sw.device} none swap\n"
)}
'';
@@ -213,6 +213,17 @@ in
${flip concatMapStrings config.fileSystems (fs: optionalString fs.autocreate ''
mkdir -p -m 0755 '${fs.mountPoint}'
'')}
+
+ # 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";