Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Power related options (sleep, wake-on-lan, restart after power failure, ...) #813

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/aerospace
./services/autossh.nix
Expand Down
16 changes: 16 additions & 0 deletions modules/networking/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ let

emptyList = lst: if lst != [] then lst else ["empty"];

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

setNetworkServices = optionalString (cfg.knownNetworkServices != []) ''
networkservices=$(networksetup -listallnetworkservices)
${concatMapStringsSep "\n" (srv: ''
Expand Down Expand Up @@ -93,6 +95,16 @@ in
default = [];
description = "The list of search paths used when resolving domain names.";
};

networking.wakeOnLan.enable = mkOption {
type = types.nullOr types.bool;
default = null;
description = ''
Enable Wake-on-LAN for the device.

Battery powered devices may require being connected to power.
'';
};
};

config = {
Expand All @@ -116,6 +128,10 @@ in
''}

${setNetworkServices}

${optionalString (cfg.wakeOnLan.enable != null) ''
systemsetup -setWakeOnNetworkAccess '${onOff cfg.wakeOnLan.enable}' &> /dev/null
''}
'';

};
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, ... }:

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
''}
'';

};
}
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, ... }:

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
''}
'';

};
}
1 change: 1 addition & 0 deletions modules/system/activation-scripts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ in
${cfg.activationScripts.nix-daemon.text}
${cfg.activationScripts.time.text}
${cfg.activationScripts.networking.text}
${cfg.activationScripts.power.text}
${cfg.activationScripts.keyboard.text}
${cfg.activationScripts.fonts.text}
${cfg.activationScripts.nvram.text}
Expand Down
10 changes: 10 additions & 0 deletions tests/networking-wakeonlan.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{ config, pkgs, ... }:

{
networking.wakeOnLan.enable = true;

test = ''
echo checking wake on network access settings in /activate >&2
grep "systemsetup -setWakeOnNetworkAccess 'on'" ${config.out}/activate
'';
}
12 changes: 12 additions & 0 deletions tests/power-restart.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ config, pkgs, ... }:

{
power.restartAfterPowerFailure = true;
power.restartAfterFreeze = true;

test = ''
echo checking restart power settings in /activate >&2
grep "systemsetup -setRestartPowerFailure 'on'" ${config.out}/activate
grep "systemsetup -setRestartFreeze 'on'" ${config.out}/activate
'';
}
16 changes: 16 additions & 0 deletions tests/power-sleep.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{ config, pkgs, ... }:

{
power.sleep.computer = "never";
power.sleep.display = 15;
power.sleep.harddisk = 5;
power.sleep.allowSleepByPowerButton = false;

test = ''
echo checking power sleep settings in /activate >&2
grep "systemsetup -setComputerSleep 'never'" ${config.out}/activate
grep "systemsetup -setDisplaySleep '15'" ${config.out}/activate
grep "systemsetup -setHardDiskSleep '5'" ${config.out}/activate
grep "systemsetup -setAllowPowerButtonToSleepComputer 'off'" ${config.out}/activate
'';
}
Loading