Skip to content

Commit

Permalink
Add more powerdevil options for laptops (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
ribru17 authored Aug 4, 2024
1 parent b17a266 commit f1137ff
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
23 changes: 16 additions & 7 deletions examples/home.nix
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,23 @@
];

powerdevil = {
powerButtonAction = "lockScreen";
autoSuspend = {
action = "shutDown";
idleTimeout = 1000;
AC = {
powerButtonAction = "lockScreen";
autoSuspend = {
action = "shutDown";
idleTimeout = 1000;
};
turnOffDisplay = {
idleTimeout = 1000;
idleTimeoutWhenLocked = "immediately";
};
};
battery = {
powerButtonAction = "sleep";
whenSleepingEnter = "standbyThenHibernate";
};
turnOffDisplay = {
idleTimeout = 1000;
idleTimeoutWhenLocked = "immediately";
lowBattery = {
whenLaptopLidClosed = "hibernate";
};
};

Expand Down
44 changes: 42 additions & 2 deletions modules/powerdevil.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let
powerButtonActions = {
nothing = 0;
sleep = 1;
hibernate = 2;
shutDown = 8;
lockScreen = 32;
showLogoutScreen = null;
Expand All @@ -13,10 +14,26 @@ let

autoSuspendActions = {
nothing = 0;
hibernate = 2;
sleep = null;
shutDown = 8;
};

whenSleepingEnterActions = {
standby = null;
hybridSleep = 2;
standbyThenHibernate = 3;
};

whenLaptopLidClosedActions = {
doNothing = 0;
sleep = null;
hibernate = 2;
shutdown = 8;
lockScreen = 32;
turnOffScreen = 64;
};

# Since AC and battery allows the same options we create a function here which
# can generate the options by just specifying the type (i.e. "AC" or
# "battery").
Expand Down Expand Up @@ -50,6 +67,24 @@ let
'';
};
};
whenSleepingEnter = lib.mkOption {
type = with lib.types; nullOr (enum (builtins.attrNames whenSleepingEnterActions));
default = null;
example = "standbyThenHibernate";
description = ''
The state, when on ${type}, to enter when sleeping.
'';
apply = action: if (action == null) then null else whenSleepingEnterActions."${action}";
};
whenLaptopLidClosed = lib.mkOption {
type = with lib.types; nullOr (enum (builtins.attrNames whenLaptopLidClosedActions));
default = null;
example = "shutdown";
description = ''
The action, when on ${type}, to perform when the laptop lid is closed.
'';
apply = action: if (action == null) then null else whenLaptopLidClosedActions."${action}";
};
turnOffDisplay = {
idleTimeout = lib.mkOption {
type = with lib.types; nullOr (either (enum [ "never" ]) (ints.between 30 600000));
Expand Down Expand Up @@ -107,6 +142,8 @@ let
PowerButtonAction = cfg.powerdevil.${optionsName}.powerButtonAction;
AutoSuspendAction = cfg.powerdevil.${optionsName}.autoSuspend.action;
AutoSuspendIdleTimeoutSec = cfg.powerdevil.${optionsName}.autoSuspend.idleTimeout;
SleepMode = cfg.powerdevil.${optionsName}.whenSleepingEnter;
LidAction = cfg.powerdevil.${optionsName}.whenLaptopLidClosed;
};
"${cfgSectName}/Display" = {
TurnOffDisplayIdleTimeoutSec = cfg.powerdevil.${optionsName}.turnOffDisplay.idleTimeout;
Expand Down Expand Up @@ -146,16 +183,19 @@ in
}
];
in
(createAssertions "AC") ++ (createAssertions "battery");
(createAssertions "AC") ++ (createAssertions "battery") ++ (createAssertions "lowBattery");

options = {
programs.plasma.powerdevil = {
AC = (createPowerDevilOptions "AC");
battery = (createPowerDevilOptions "battery");
lowBattery = (createPowerDevilOptions "lowBattery");
};
};

config.programs.plasma.configFile = lib.mkIf cfg.enable {
powerdevilrc = lib.filterAttrs (k: v: v != null) ((createPowerDevilConfig "AC" "AC") // (createPowerDevilConfig "Battery" "battery"));
powerdevilrc = lib.filterAttrs (k: v: v != null) ((createPowerDevilConfig "AC" "AC")
// (createPowerDevilConfig "Battery" "battery")
// (createPowerDevilConfig "LowBattery" "lowBattery"));
};
}

0 comments on commit f1137ff

Please sign in to comment.