2009-03-06 12:27:35 +00:00
|
|
|
{pkgs, config, ...}:
|
|
|
|
|
2007-01-10 13:07:57 +00:00
|
|
|
let
|
|
|
|
|
2009-05-28 00:14:38 +01:00
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
swapDevices = pkgs.lib.mkOption {
|
|
|
|
default = [];
|
|
|
|
example = [
|
|
|
|
{ device = "/dev/hda7"; }
|
|
|
|
{ device = "/var/swapfile"; }
|
|
|
|
{ label = "bigswap"; }
|
|
|
|
];
|
|
|
|
description = "
|
|
|
|
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.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2009-03-06 12:27:35 +00:00
|
|
|
inherit (pkgs) utillinux lib;
|
|
|
|
|
|
|
|
swapDevices = config.swapDevices;
|
|
|
|
|
2007-01-10 13:07:57 +00:00
|
|
|
devicesByPath =
|
2008-02-11 11:51:51 +00:00
|
|
|
map (x: x.device) (lib.filter (x: x ? device) swapDevices);
|
2007-01-10 13:07:57 +00:00
|
|
|
|
|
|
|
devicesByLabel =
|
2008-02-11 11:51:51 +00:00
|
|
|
map (x: x.label) (lib.filter (x: x ? label) swapDevices);
|
2007-01-10 13:07:57 +00:00
|
|
|
|
|
|
|
in
|
2006-12-21 01:07:23 +00:00
|
|
|
|
|
|
|
|
2009-03-06 12:27:35 +00:00
|
|
|
{
|
2009-05-28 00:14:38 +01:00
|
|
|
require = [options];
|
|
|
|
|
|
|
|
services.extraJobs = [{
|
|
|
|
name = "swap";
|
|
|
|
|
|
|
|
job = ''
|
|
|
|
start on startup
|
|
|
|
start on new-devices
|
|
|
|
|
|
|
|
script
|
|
|
|
for device in ${toString devicesByPath}; do
|
|
|
|
${utillinux}/sbin/swapon "$device" || true
|
|
|
|
done
|
|
|
|
|
|
|
|
for label in ${toString devicesByLabel}; do
|
|
|
|
${utillinux}/sbin/swapon -L "$label" || true
|
|
|
|
done
|
|
|
|
|
|
|
|
# Remove swap devices not listed in swapDevices.
|
|
|
|
# !!! disabled because it doesn't work with labels
|
|
|
|
#for used in $(cat /proc/swaps | grep '^/' | sed 's/ .*//'); do
|
|
|
|
# found=
|
|
|
|
# for device in $ {toString swapDevices}; do
|
|
|
|
# if test "$used" = "$device"; then found=1; fi
|
|
|
|
# done
|
|
|
|
# if test -z "$found"; then
|
|
|
|
# ${utillinux}/sbin/swapoff "$used" || true
|
|
|
|
# fi
|
|
|
|
#done
|
|
|
|
|
|
|
|
end script
|
|
|
|
'';
|
|
|
|
}];
|
2006-12-21 01:07:23 +00:00
|
|
|
}
|