Skip to content

Commit

Permalink
power,sleep: Add options to control restart and sleep behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipBorgesen committed Oct 23, 2024
1 parent b415501 commit 4c40a38
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modules/module-list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
./environment
./fonts
./launchd
./power
./power/sleep.nix
./services/activate-system
./services/autossh.nix
./services/buildkite-agents.nix
Expand Down
47 changes: 47 additions & 0 deletions modules/power/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{ config, lib, ... }:

with lib;

let
cfg = config.power;

onOff = cond: if cond then "on" else "off";
in

{
options = {
power.restartAfterPowerFailure = mkOption {
type = types.nullOr types.bool;
default = null;
description = lib.mdDoc ''
Whether to restart the computer after a power failure.
'';
};

power.restartAfterFreeze = mkOption {
type = types.nullOr types.bool;
default = null;
description = lib.mdDoc ''
Whether to restart the computer after a system freeze.
'';
};
};

config = {

system.activationScripts.power.text = ''
echo "configuring power..." >&2
${optionalString (cfg.restartAfterPowerFailure != null) ''
systemsetup -setRestartPowerFailure \
'${onOff cfg.restartAfterPowerFailure}' &> /dev/null
''}
${optionalString (cfg.restartAfterFreeze != null) ''
systemsetup -setRestartFreeze \
'${onOff cfg.restartAfterFreeze}' &> /dev/null
''}
'';

};
}
80 changes: 80 additions & 0 deletions modules/power/sleep.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{ config, lib, ... }:

with lib;

let
cfg = config.power.sleep;

onOff = cond: if cond then "on" else "off";
in

{
options = {
power.sleep.computer = mkOption {
type = types.nullOr (types.either types.ints.positive (types.enum ["never"]));
default = null;
example = "never";
description = lib.mdDoc ''
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 = mkOption {
type = types.nullOr (types.either types.ints.positive (types.enum ["never"]));
default = null;
example = "never";
description = lib.mdDoc ''
Amount of idle time (in minutes) until displays sleep.
`"never"` disables display sleeping.
'';
};

power.sleep.harddisk = mkOption {
type = types.nullOr (types.either types.ints.positive (types.enum ["never"]));
default = null;
example = "never";
description = lib.mdDoc ''
Amount of idle time (in minutes) until hard disks sleep.
`"never"` disables hard disk sleeping.
'';
};

power.sleep.allowSleepByPowerButton = mkOption {
type = types.nullOr types.bool;
default = null;
description = lib.mdDoc ''
Whether the power button can sleep the computer.
'';
};
};

config = {

system.activationScripts.sleep.text = ''
${optionalString (cfg.computer != null) ''
systemsetup -setComputerSleep '${toString cfg.computer}' &> /dev/null
''}
${optionalString (cfg.display != null) ''
systemsetup -setDisplaySleep '${toString cfg.display}' &> /dev/null
''}
${optionalString (cfg.harddisk != null) ''
systemsetup -setHardDiskSleep '${toString cfg.harddisk}' &> /dev/null
''}
${optionalString (cfg.allowSleepByPowerButton != null) ''
systemsetup -setAllowPowerButtonToSleepComputer \
'${onOff cfg.allowSleepByPowerButton}' &> /dev/null
''}
'';

};
}
2 changes: 2 additions & 0 deletions modules/system/activation-scripts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ in
${cfg.activationScripts.nix-daemon.text}
${cfg.activationScripts.time.text}
${cfg.activationScripts.networking.text}
${cfg.activationScripts.power.text}
${cfg.activationScripts.sleep.text}
${cfg.activationScripts.keyboard.text}
${cfg.activationScripts.fonts.text}
${cfg.activationScripts.nvram.text}
Expand Down

0 comments on commit 4c40a38

Please sign in to comment.