2018-07-20 20:56:59 +00:00
|
|
|
{ config, lib, ... }:
|
2009-11-15 12:48:42 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2009-11-15 12:48:42 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.powerManagement;
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-11-15 12:48:42 +00:00
|
|
|
powerManagement = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.bool;
|
2012-10-05 02:10:35 +00:00
|
|
|
default = true;
|
2009-11-15 12:48:42 +00:00
|
|
|
description =
|
2022-07-28 21:19:15 +00:00
|
|
|
lib.mdDoc ''
|
2009-11-15 12:48:42 +00:00
|
|
|
Whether to enable power management. This includes support
|
|
|
|
for suspend-to-RAM and powersave features on laptops.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
resumeCommands = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.lines;
|
2009-11-15 12:48:42 +00:00
|
|
|
default = "";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Commands executed after the system resumes from suspend-to-RAM.";
|
2009-11-15 12:48:42 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-11-15 12:56:40 +00:00
|
|
|
powerUpCommands = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.lines;
|
2009-11-15 12:56:40 +00:00
|
|
|
default = "";
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2014-08-27 21:41:15 +00:00
|
|
|
"''${pkgs.hdparm}/sbin/hdparm -B 255 /dev/sda"
|
|
|
|
'';
|
2009-11-15 12:56:40 +00:00
|
|
|
description =
|
2022-07-28 21:19:15 +00:00
|
|
|
lib.mdDoc ''
|
2009-11-15 12:56:40 +00:00
|
|
|
Commands executed when the machine powers up. That is,
|
|
|
|
they're executed both when the system first boots and when
|
|
|
|
it resumes from suspend or hibernation.
|
|
|
|
'';
|
|
|
|
};
|
2010-12-02 20:23:45 +00:00
|
|
|
|
|
|
|
powerDownCommands = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.lines;
|
2010-12-02 20:23:45 +00:00
|
|
|
default = "";
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2014-08-27 21:41:15 +00:00
|
|
|
"''${pkgs.hdparm}/sbin/hdparm -B 255 /dev/sda"
|
|
|
|
'';
|
2010-12-02 20:23:45 +00:00
|
|
|
description =
|
2022-07-28 21:19:15 +00:00
|
|
|
lib.mdDoc ''
|
2010-12-02 20:23:45 +00:00
|
|
|
Commands executed when the machine powers down. That is,
|
|
|
|
they're executed both when the system shuts down and when
|
|
|
|
it goes to suspend or hibernation.
|
|
|
|
'';
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-11-15 12:48:42 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-11-15 12:48:42 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-11-15 12:48:42 +00:00
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2013-08-24 15:47:06 +00:00
|
|
|
systemd.targets.post-resume = {
|
2013-08-27 06:03:49 +00:00
|
|
|
description = "Post-Resume Actions";
|
2013-08-24 15:47:06 +00:00
|
|
|
requires = [ "post-resume.service" ];
|
|
|
|
after = [ "post-resume.service" ];
|
|
|
|
wantedBy = [ "sleep.target" ];
|
|
|
|
unitConfig.StopWhenUnneeded = true;
|
|
|
|
};
|
|
|
|
|
2012-10-04 21:57:10 +00:00
|
|
|
# Service executed before suspending/hibernating.
|
2019-08-13 21:52:01 +00:00
|
|
|
systemd.services.pre-sleep =
|
2012-10-04 21:57:10 +00:00
|
|
|
{ description = "Pre-Sleep Actions";
|
|
|
|
wantedBy = [ "sleep.target" ];
|
|
|
|
before = [ "sleep.target" ];
|
|
|
|
script =
|
|
|
|
''
|
|
|
|
${cfg.powerDownCommands}
|
|
|
|
'';
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
};
|
|
|
|
|
2019-08-13 21:52:01 +00:00
|
|
|
systemd.services.post-resume =
|
2013-08-24 15:47:06 +00:00
|
|
|
{ description = "Post-Resume Actions";
|
2022-07-22 16:33:37 +00:00
|
|
|
after = [ "suspend.target" "hibernate.target" "hybrid-sleep.target" "suspend-then-hibernate.target" ];
|
2012-10-04 21:57:10 +00:00
|
|
|
script =
|
|
|
|
''
|
2022-11-03 12:00:00 +00:00
|
|
|
/run/current-system/systemd/bin/systemctl try-restart --no-block post-resume.target
|
2012-10-04 21:57:10 +00:00
|
|
|
${cfg.resumeCommands}
|
|
|
|
${cfg.powerUpCommands}
|
|
|
|
'';
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
};
|
|
|
|
|
2009-11-15 12:48:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|