From f737259769ef4722ed956bcaaab67509b96c23cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philip=20B=C3=B8rgesen?= Date: Sun, 19 Nov 2023 00:52:02 +0100 Subject: power,sleep: Add options to control restart and sleep behavior --- modules/power/default.nix | 47 ++++++++++++++++++++++++++++ modules/power/sleep.nix | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 modules/power/default.nix create mode 100644 modules/power/sleep.nix (limited to 'modules/power') diff --git a/modules/power/default.nix b/modules/power/default.nix new file mode 100644 index 0000000..a99905f --- /dev/null +++ b/modules/power/default.nix @@ -0,0 +1,47 @@ +{ config, lib, ... }: + +let + cfg = config.power; + + types = lib.types; + + onOff = cond: if cond then "on" else "off"; +in + +{ + options = { + power.restartAfterPowerFailure = lib.mkOption { + type = types.nullOr types.bool; + default = null; + description = '' + Whether to restart the computer after a power failure. + ''; + }; + + power.restartAfterFreeze = lib.mkOption { + type = types.nullOr types.bool; + default = null; + description = '' + Whether to restart the computer after a system freeze. + ''; + }; + }; + + config = { + + system.activationScripts.power.text = '' + echo "configuring power..." >&2 + + ${lib.optionalString (cfg.restartAfterPowerFailure != null) '' + systemsetup -setRestartPowerFailure \ + '${onOff cfg.restartAfterPowerFailure}' &> /dev/null + ''} + + ${lib.optionalString (cfg.restartAfterFreeze != null) '' + systemsetup -setRestartFreeze \ + '${onOff cfg.restartAfterFreeze}' &> /dev/null + ''} + ''; + + }; +} diff --git a/modules/power/sleep.nix b/modules/power/sleep.nix new file mode 100644 index 0000000..ab5862c --- /dev/null +++ b/modules/power/sleep.nix @@ -0,0 +1,80 @@ +{ config, lib, ... }: + +let + cfg = config.power.sleep; + + types = lib.types; + + onOff = cond: if cond then "on" else "off"; +in + +{ + options = { + power.sleep.computer = lib.mkOption { + type = types.nullOr (types.either types.ints.positive (types.enum ["never"])); + default = null; + example = "never"; + description = '' + Amount of idle time (in minutes) until the computer sleeps. + + `"never"` disables computer sleeping. + + The system might not be considered idle before connected displays sleep, as + per the `power.sleep.display` option. + ''; + }; + + power.sleep.display = lib.mkOption { + type = types.nullOr (types.either types.ints.positive (types.enum ["never"])); + default = null; + example = "never"; + description = '' + Amount of idle time (in minutes) until displays sleep. + + `"never"` disables display sleeping. + ''; + }; + + power.sleep.harddisk = lib.mkOption { + type = types.nullOr (types.either types.ints.positive (types.enum ["never"])); + default = null; + example = "never"; + description = '' + Amount of idle time (in minutes) until hard disks sleep. + + `"never"` disables hard disk sleeping. + ''; + }; + + power.sleep.allowSleepByPowerButton = lib.mkOption { + type = types.nullOr types.bool; + default = null; + description = '' + Whether the power button can sleep the computer. + ''; + }; + }; + + config = { + + system.activationScripts.power.text = lib.mkAfter '' + ${lib.optionalString (cfg.computer != null) '' + systemsetup -setComputerSleep '${toString cfg.computer}' &> /dev/null + ''} + + ${lib.optionalString (cfg.display != null) '' + systemsetup -setDisplaySleep '${toString cfg.display}' &> /dev/null + ''} + + ${lib.optionalString (cfg.harddisk != null) '' + systemsetup -setHardDiskSleep '${toString cfg.harddisk}' &> /dev/null + ''} + + ${lib.optionalString (cfg.allowSleepByPowerButton != null) '' + systemsetup -setAllowPowerButtonToSleepComputer \ + '${onOff cfg.allowSleepByPowerButton}' &> /dev/null + ''} + ''; + + }; +} -- cgit v1.2.3 From 016b1608eec6c54cfaece96b63ec9d1a6cd4672b Mon Sep 17 00:00:00 2001 From: gnammix <71704832+gnammix@users.noreply.github.com> Date: Fri, 27 Dec 2024 21:36:31 +0100 Subject: power: restartAfterPowerFailure option is carried out in activation script only if supported Minor documentation change --- modules/power/default.nix | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'modules/power') diff --git a/modules/power/default.nix b/modules/power/default.nix index a99905f..e36a027 100644 --- a/modules/power/default.nix +++ b/modules/power/default.nix @@ -15,6 +15,8 @@ in default = null; description = '' Whether to restart the computer after a power failure. + + Option is not supported on all devices. ''; }; @@ -33,8 +35,10 @@ in echo "configuring power..." >&2 ${lib.optionalString (cfg.restartAfterPowerFailure != null) '' - systemsetup -setRestartPowerFailure \ - '${onOff cfg.restartAfterPowerFailure}' &> /dev/null + if ! systemsetup -getRestartPowerFailure | grep -q "Not supported"; then + systemsetup -setRestartPowerFailure \ + '${onOff cfg.restartAfterPowerFailure}' &> /dev/null + fi ''} ${lib.optionalString (cfg.restartAfterFreeze != null) '' -- cgit v1.2.3 From 62d8f5f289341497ea0fa21814e734cbea69a0a1 Mon Sep 17 00:00:00 2001 From: gnammix <71704832+gnammix@users.noreply.github.com> Date: Sun, 29 Dec 2024 12:13:54 +0100 Subject: power: move the check for restartPowerfailure support to checks.nix --- modules/power/default.nix | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'modules/power') diff --git a/modules/power/default.nix b/modules/power/default.nix index e36a027..c3e4974 100644 --- a/modules/power/default.nix +++ b/modules/power/default.nix @@ -35,10 +35,8 @@ in echo "configuring power..." >&2 ${lib.optionalString (cfg.restartAfterPowerFailure != null) '' - if ! systemsetup -getRestartPowerFailure | grep -q "Not supported"; then - systemsetup -setRestartPowerFailure \ - '${onOff cfg.restartAfterPowerFailure}' &> /dev/null - fi + systemsetup -setRestartPowerFailure \ + '${onOff cfg.restartAfterPowerFailure}' &> /dev/null ''} ${lib.optionalString (cfg.restartAfterFreeze != null) '' -- cgit v1.2.3