2014-04-14 16:26:48 +02:00
|
|
|
|
{ config, lib, pkgs, utils, ... }:
|
2009-10-12 19:27:57 +02:00
|
|
|
|
|
2012-10-12 23:01:49 +02:00
|
|
|
|
with utils;
|
2014-05-05 20:58:51 +02:00
|
|
|
|
with lib;
|
2009-03-06 13:27:35 +01:00
|
|
|
|
|
2009-07-16 16:51:49 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
###### interface
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2009-05-28 01:14:38 +02:00
|
|
|
|
options = {
|
|
|
|
|
|
2009-07-16 16:51:49 +02:00
|
|
|
|
swapDevices = mkOption {
|
2009-05-28 01:14:38 +02:00
|
|
|
|
default = [];
|
|
|
|
|
example = [
|
|
|
|
|
{ device = "/dev/hda7"; }
|
|
|
|
|
{ device = "/var/swapfile"; }
|
|
|
|
|
{ label = "bigswap"; }
|
|
|
|
|
];
|
2009-07-16 16:51:49 +02:00
|
|
|
|
description = ''
|
2009-05-28 01:14:38 +02:00
|
|
|
|
The swap devices and swap files. These must have been
|
|
|
|
|
initialised using <command>mkswap</command>. Each element
|
|
|
|
|
should be an attribute set specifying either the path of the
|
|
|
|
|
swap device or file (<literal>device</literal>) or the label
|
|
|
|
|
of the swap device (<literal>label</literal>, see
|
|
|
|
|
<command>mkswap -L</command>). Using a label is
|
|
|
|
|
recommended.
|
2009-07-16 16:51:49 +02:00
|
|
|
|
'';
|
|
|
|
|
|
2013-03-14 17:09:21 +01:00
|
|
|
|
type = types.listOf types.optionSet;
|
2009-07-16 16:51:49 +02:00
|
|
|
|
|
2009-12-26 17:20:00 +01:00
|
|
|
|
options = {config, options, ...}: {
|
|
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2009-12-26 17:20:00 +01:00
|
|
|
|
device = mkOption {
|
|
|
|
|
example = "/dev/sda3";
|
2013-10-30 11:02:04 +01:00
|
|
|
|
type = types.str;
|
2010-06-04 16:22:11 +02:00
|
|
|
|
description = "Path of the device.";
|
2009-12-26 17:20:00 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
label = mkOption {
|
|
|
|
|
example = "swap";
|
2013-10-30 11:02:04 +01:00
|
|
|
|
type = types.str;
|
2012-06-12 15:41:51 +02:00
|
|
|
|
description = ''
|
|
|
|
|
Label of the device. Can be used instead of <varname>device</varname>.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
size = mkOption {
|
|
|
|
|
default = null;
|
2012-06-13 05:28:03 +02:00
|
|
|
|
example = 2048;
|
2012-06-12 15:41:51 +02:00
|
|
|
|
type = types.nullOr types.int;
|
2010-06-04 16:22:11 +02:00
|
|
|
|
description = ''
|
2012-06-13 05:28:03 +02:00
|
|
|
|
If this option is set, ‘device’ is interpreted as the
|
|
|
|
|
path of a swapfile that will be created automatically
|
|
|
|
|
with the indicated size (in megabytes) if it doesn't
|
|
|
|
|
exist.
|
2010-06-04 16:22:11 +02:00
|
|
|
|
'';
|
2009-12-26 17:20:00 +01:00
|
|
|
|
};
|
|
|
|
|
|
2013-05-13 23:42:55 +02:00
|
|
|
|
priority = mkOption {
|
|
|
|
|
default = null;
|
|
|
|
|
example = 2048;
|
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
|
description = ''
|
|
|
|
|
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.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2009-07-16 16:51:49 +02:00
|
|
|
|
};
|
|
|
|
|
|
2009-12-26 17:20:00 +01:00
|
|
|
|
config = {
|
2013-10-28 00:08:07 +01:00
|
|
|
|
device = mkIf options.label.isDefined
|
|
|
|
|
"/dev/disk/by-label/${config.label}";
|
2009-07-16 16:51:49 +02:00
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2009-07-16 16:51:49 +02:00
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2009-05-28 01:14:38 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
2009-07-16 16:51:49 +02:00
|
|
|
|
|
2012-08-07 23:34:10 +02:00
|
|
|
|
config = mkIf ((length config.swapDevices) != 0) {
|
2012-10-12 22:47:11 +02:00
|
|
|
|
|
2012-08-07 23:34:10 +02:00
|
|
|
|
system.requiredKernelConfig = with config.lib.kernelConfig; [
|
|
|
|
|
(isYes "SWAP")
|
|
|
|
|
];
|
2012-10-12 22:47:11 +02:00
|
|
|
|
|
|
|
|
|
# Create missing swapfiles.
|
|
|
|
|
# FIXME: support changing the size of existing swapfiles.
|
2013-01-16 12:33:18 +01:00
|
|
|
|
systemd.services =
|
2012-10-12 22:47:11 +02:00
|
|
|
|
let
|
|
|
|
|
|
2012-10-12 23:01:49 +02:00
|
|
|
|
createSwapDevice = sw:
|
|
|
|
|
assert sw.device != "";
|
|
|
|
|
let device' = escapeSystemdPath sw.device; in
|
|
|
|
|
nameValuePair "mkswap-${escapeSystemdPath sw.device}"
|
2012-10-12 22:47:11 +02:00
|
|
|
|
{ description = "Initialisation of Swapfile ${sw.device}";
|
2012-10-12 23:01:49 +02:00
|
|
|
|
wantedBy = [ "${device'}.swap" ];
|
|
|
|
|
before = [ "${device'}.swap" ];
|
2012-10-12 22:47:11 +02:00
|
|
|
|
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}
|
2014-04-23 17:45:46 +02:00
|
|
|
|
chmod 0600 ${sw.device}
|
2012-10-12 22:47:11 +02:00
|
|
|
|
mkswap ${sw.device}
|
|
|
|
|
fi
|
|
|
|
|
'';
|
2012-10-12 23:32:36 +02:00
|
|
|
|
unitConfig.RequiresMountsFor = [ "${dirOf sw.device}" ];
|
2012-10-12 22:47:11 +02:00
|
|
|
|
unitConfig.DefaultDependencies = false; # needed to prevent a cycle
|
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
in listToAttrs (map createSwapDevice (filter (sw: sw.size != null) config.swapDevices));
|
|
|
|
|
|
2012-08-07 23:34:10 +02:00
|
|
|
|
};
|
|
|
|
|
|
2006-12-21 02:07:23 +01:00
|
|
|
|
}
|