mirror of
https://github.com/ItsDrike/nixdots
synced 2025-06-30 02:40:43 +00:00
Restructure & rework power management config
This commit is contained in:
parent
6545dda7f8
commit
08864662bb
6 changed files with 47 additions and 74 deletions
41
system/roles/laptop/power/power-profiles-daemon/default.nix
Normal file
41
system/roles/laptop/power/power-profiles-daemon/default.nix
Normal file
|
@ -0,0 +1,41 @@
|
|||
# This sets up power management using auto-cpufreq,
|
||||
# alongside with upower and power-profiles-daemon.
|
||||
# Together, this provides a complete alternative to TLP
|
||||
{ pkgs, lib, config, ...}: let
|
||||
inherit (lib) mkIf;
|
||||
inherit (lib.modules) mkForce;
|
||||
inherit (lib.strings) makeBinPath;
|
||||
deviceType = config.myOptions.device.roles.type;
|
||||
acceptedTypes = ["laptop"];
|
||||
in {
|
||||
config = mkIf (builtins.elem deviceType acceptedTypes) {
|
||||
# allows changing system behavior based upon user-selected power profiles
|
||||
# (with `powerprofilesctl` command)
|
||||
services.power-profiles-daemon.enable = true;
|
||||
|
||||
# Power state monitor. Switches power profiles based on charging state.
|
||||
# Plugged in - performance (if available, falls back to balance)
|
||||
# Unplugged - balanced, until below 50%, then power-saver
|
||||
systemd.services."power-monitor" = let
|
||||
dependencies = with pkgs; [
|
||||
coreutils
|
||||
gnugrep
|
||||
power-profiles-daemon
|
||||
inotify-tools
|
||||
jaq
|
||||
];
|
||||
in {
|
||||
description = "Power Monitoring Service";
|
||||
environment.PATH = mkForce "/run/wrappers/bin:${makeBinPath dependencies}";
|
||||
script = builtins.readFile ./scripts/power_monitor.sh;
|
||||
|
||||
serviceConfig = {
|
||||
Type = "simple";
|
||||
Restart = "on-failure";
|
||||
};
|
||||
|
||||
wants = ["power-profiles-daemon.service"];
|
||||
wantedBy = ["default.target"];
|
||||
};
|
||||
};
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
BAT=$(echo /sys/class/power_supply/BAT*) # only supports single-battery systems
|
||||
BAT_STATUS="$BAT/status"
|
||||
BAT_CAP="$BAT/capacity"
|
||||
|
||||
POWER_SAVE_PERCENT=50 # Enter power-save mode if on bat and below this capacity
|
||||
|
||||
HAS_PERFORMANCE="$(powerprofilesctl list | grep "performance" || true)" # the || true ignores grep failing with non-zero code
|
||||
|
||||
# monitor loop
|
||||
prev=0
|
||||
while true; do
|
||||
# read the current state
|
||||
status="$(cat "$BAT_STATUS")"
|
||||
capacity="$(cat "$BAT_CAP")"
|
||||
|
||||
if [[ $status == "Discharging" ]]; then
|
||||
if [[ $capacity -le $POWER_SAVE_PERCENT ]]; then
|
||||
profile="power-saver"
|
||||
else
|
||||
profile="balanced"
|
||||
fi
|
||||
else
|
||||
if [[ -n $HAS_PERFORMANCE ]]; then
|
||||
profile="performance"
|
||||
else
|
||||
profile="balanced"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Set the new profile
|
||||
if [[ $profile != $prev ]]; then
|
||||
echo -en "Setting power profile to ${profile}\n"
|
||||
powerprofilesctl set $profile
|
||||
prev=$profile
|
||||
fi
|
||||
|
||||
# wait for changes in status or capacity files
|
||||
# i.e. for the next power change event
|
||||
inotifywait -qq "$BAT_STATUS" "$BAT_CAP"
|
||||
done
|
Loading…
Add table
Add a link
Reference in a new issue