2009-03-06 12:27:35 +00:00
|
|
|
{pkgs, config, ...}:
|
|
|
|
|
2007-01-10 13:07:57 +00:00
|
|
|
let
|
|
|
|
|
2009-07-16 15:51:49 +01:00
|
|
|
inherit (pkgs) utillinux;
|
|
|
|
inherit (pkgs.lib) mkOption filter types;
|
2009-05-28 00:14:38 +01:00
|
|
|
|
2009-07-16 15:51:49 +01:00
|
|
|
toPath = x: if x.device != null then x.device else "/dev/disk/by-label/${x.label}";
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
2009-05-28 00:14:38 +01:00
|
|
|
options = {
|
|
|
|
|
2009-07-16 15:51:49 +01:00
|
|
|
swapDevices = mkOption {
|
2009-05-28 00:14:38 +01:00
|
|
|
default = [];
|
|
|
|
example = [
|
|
|
|
{ device = "/dev/hda7"; }
|
|
|
|
{ device = "/var/swapfile"; }
|
|
|
|
{ label = "bigswap"; }
|
|
|
|
];
|
2009-07-16 15:51:49 +01:00
|
|
|
description = ''
|
2009-05-28 00:14:38 +01: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 15:51:49 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
type = types.list types.optionSet;
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
device = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "/dev/sda3";
|
|
|
|
type = types.nullOr types.string;
|
|
|
|
description = ''
|
|
|
|
Path of the device.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
label = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "swap";
|
|
|
|
type = types.nullOr types.string;
|
|
|
|
description = "
|
|
|
|
Label of the device. Can be used instead of <varname>device</varname>.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2009-05-28 00:14:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
2009-07-16 15:51:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
|
|
|
jobs = pkgs.lib.singleton
|
|
|
|
{ name = "swap";
|
|
|
|
|
|
|
|
task = true;
|
|
|
|
|
|
|
|
startOn = ["startup" "new-devices"];
|
|
|
|
|
|
|
|
script =
|
|
|
|
''
|
|
|
|
swapDevices=${toString (map toPath config.swapDevices)}
|
|
|
|
|
|
|
|
for device in $swapDevices; do
|
|
|
|
${utillinux}/sbin/swapon "$device" || true
|
|
|
|
done
|
|
|
|
|
|
|
|
# Remove swap devices not listed in swapDevices.
|
|
|
|
for used in $(cat /proc/swaps | grep '^/' | sed 's/ .*//'); do
|
|
|
|
found=
|
|
|
|
for device in $swapDevices; do
|
|
|
|
device=$(readlink -f $device)
|
|
|
|
if test "$used" = "$device"; then found=1; fi
|
|
|
|
done
|
|
|
|
if test -z "$found"; then
|
|
|
|
${utillinux}/sbin/swapoff "$used" || true
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
};
|
2009-05-28 00:14:38 +01:00
|
|
|
|
2009-07-16 15:51:49 +01:00
|
|
|
};
|
2009-05-28 00:14:38 +01:00
|
|
|
|
2006-12-21 01:07:23 +00:00
|
|
|
}
|