Power : Add user & waybar toggles for powersave and powerlimit.

This commit is contained in:
Dmitry Voronin 2024-05-06 19:34:00 +03:00
parent 366c0738bd
commit cab1b0f0b2
Signed by: voronind
SSH key fingerprint: SHA256:3kBb4iV2ahufEBNq+vFbUe4QYfHt98DHQjN7QaptY9k
8 changed files with 186 additions and 62 deletions

View file

@ -1,13 +1,58 @@
{ pkgs, lib, ... }: { { pkgs, lib, ... }: let
systemd.services.powerlimit = { controlFileMin = "/sys/class/power_supply/BAT0/charge_control_start_threshold";
description = "Limit battery charge."; controlFileMax = "/sys/class/power_supply/BAT0/charge_control_end_threshold";
enable = true;
wantedBy = [ "multi-user.target" ]; onMin = "40";
serviceConfig = { onMax = "80";
Type = "simple"; offMin = "90";
RemainAfterExit = "yes"; offMax = "95";
ExecStart = "${lib.getExe pkgs.bash} -c 'echo 40 > /sys/class/power_supply/BAT0/charge_control_start_threshold; echo 80 > /sys/class/power_supply/BAT0/charge_control_end_threshold;'";
ExecStop = "${lib.getExe pkgs.bash} -c 'echo 95 > /sys/class/power_supply/BAT0/charge_control_end_threshold; echo 90 > /sys/class/power_supply/BAT0/charge_control_start_threshold;'"; script = pkgs.writeShellScriptBin "powerlimit" ''
function _toggle() {
if _status; then
echo ${offMax} > ${controlFileMax}
echo ${offMin} > ${controlFileMin}
else
echo ${onMin} > ${controlFileMin}
echo ${onMax} > ${controlFileMax}
fi
true
}
function _waybar() {
_status || echo -n ""
}
function _status() {
local current=$(cat ${controlFileMax})
local enabled="${onMax}"
[[ "''${current}" = "''${enabled}" ]]
}
_''${1}
'';
in {
systemd = {
services.powerlimit = {
description = "Limit battery charge.";
enable = true;
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "simple";
RemainAfterExit = "yes";
ExecStart = "${lib.getExe pkgs.bash} -c 'echo 40 > ${controlFileMin}; echo 80 > ${controlFileMax};'";
ExecStop = "${lib.getExe pkgs.bash} -c 'echo 95 > ${controlFileMax}; echo 90 > ${controlFileMin};'";
};
}; };
# NOTE: Allow user access.
tmpfiles.rules = [
"z ${controlFileMax} 0777 - - - -"
"z ${controlFileMin} 0777 - - - -"
];
}; };
environment.systemPackages = [ script ];
} }

View file

@ -1,16 +1,24 @@
{ pkgs, lib, ... }: { { pkgs, ... } @args: let
controlFile = "/sys/devices/system/cpu/cpufreq/boost";
enable = "1";
disable = "0";
script = pkgs.writeShellScriptBin "powersave" (import ./powersave/Script.nix {
controlFile = controlFile;
disable = "1";
enable = "0";
}).script;
in {
# Requirements: # Requirements:
# CPPC (Collaborative Power Control) - Disabled. # CPPC (Collaborative Power Control) - Disabled.
# PSS (Cool and Quiet) - Enabled. # PSS (Cool and Quiet) - Enabled.
systemd.services.powersave = { imports = [
description = "AMD disable Boost"; (import ./powersave ({
enable = true; inherit controlFile;
wantedBy = [ "multi-user.target" ]; inherit enable;
serviceConfig = { inherit disable;
Type = "simple"; } // args))
RemainAfterExit = "yes"; ];
ExecStart = "${lib.getExe pkgs.bash} -c 'echo 0 > /sys/devices/system/cpu/cpufreq/boost'";
ExecStop = "${lib.getExe pkgs.bash} -c 'echo 1 > /sys/devices/system/cpu/cpufreq/boost'"; environment.systemPackages = [ script ];
};
};
} }

View file

@ -1,13 +1,21 @@
{ pkgs, lib, ... }: { { pkgs, ... } @args: let
systemd.services.powersave = { controlFile = "/sys/devices/system/cpu/intel_pstate/no_turbo";
description = "Intel disable Boost"; enable = "1";
enable = true; disable = "0";
wantedBy = [ "multi-user.target" ];
serviceConfig = { script = pkgs.writeShellScriptBin "powersave" (import ./powersave/Script.nix {
Type = "simple"; controlFile = controlFile;
RemainAfterExit = "yes"; disable = "0";
ExecStart = "${lib.getExe pkgs.bash} -c 'echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo'"; enable = "1";
ExecStop = "${lib.getExe pkgs.bash} -c 'echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo'"; }).script;
}; in {
}; imports = [
(import ./powersave ({
inherit controlFile;
inherit enable;
inherit disable;
} // args))
];
environment.systemPackages = [ script ];
} }

View file

@ -6,13 +6,15 @@
ids = [ "*" ]; ids = [ "*" ];
settings = { settings = {
main = { main = {
capslock = "overload(control, esc)"; backspace = "delete"; # Delete key on backspace.
compose = "layer(layer_macro)"; capslock = "overload(control, esc)"; # Ctrl/esc combo.
esc = "capslock"; compose = "layer(layer_macro)"; # Input macros.
rightalt = "layer(layer_rightalt)"; esc = "layer(layer_system)"; # System controls.
rightcontrol = "layer(layer_control)"; leftcontrol = "overload(layer_alternative, leftcontrol)"; # Alternative layer for home, end etc.
rightshift = "layer(layer_rightshift)"; print = "layer(layer_macro)"; # ThinkPad remap to compose.
leftcontrol = "overload(layer_alternative, leftcontrol)"; rightalt = "capslock"; # Language toggle.
rightcontrol = "layer(layer_control)"; # Media and other controls.
rightshift = "backspace"; # Backspace.
}; };
layer_alternative = { layer_alternative = {
@ -42,11 +44,12 @@
z = "stopcd"; z = "stopcd";
}; };
layer_macro = {}; layer_system = {
p = "command(powersave toggle)";
l = "command(powerlimit toggle)";
};
# Vacant layers. layer_macro = {};
layer_rightalt = {};
layer_rightshift = {};
}; };
}; };
}; };

View file

@ -21,9 +21,11 @@
"sway/language", "sway/language",
"pulseaudio", "pulseaudio",
"battery", "battery",
"custom/powerlimit",
"cpu", "cpu",
"temperature", "temperature",
"memory", "memory",
"custom/powersave",
"tray" "tray"
], ],
"sway/scratchpad": { "sway/scratchpad": {
@ -54,7 +56,13 @@
"format-charging": "{capacity}% ", "format-charging": "{capacity}% ",
"format-plugged": "{capacity}% ", "format-plugged": "{capacity}% ",
"format-alt": "{time} {icon}", "format-alt": "{time} {icon}",
"format-icons": ["󰂎", "󱊡", "󱊢", "󱊣", "󱊣"] "format-icons": ["󰂎", "󱊡", "󱊢", "󱊣", "󱊣"],
"on-click-right": "powerlimit toggle"
},
"custom/powerlimit": {
"exec": "powerlimit waybar",
"interval": 1,
"on-click-right": "powerlimit toggle"
}, },
"pulseaudio": { "pulseaudio": {
"scroll-step": ${toString(setting.volume.step)}, "scroll-step": ${toString(setting.volume.step)},
@ -83,16 +91,25 @@
"cpu": { "cpu": {
"format": "{usage}% ({load})", "format": "{usage}% ({load})",
"tooltip": false, "tooltip": false,
"on-click": "foot -e bash -c btop" "on-click": "foot -e bash -c btop",
"on-click-right": "powersave toggle"
}, },
"memory": { "memory": {
"format": "{percentage}%", "format": "{percentage}%",
"on-click": "foot -e bash -c btop" "on-click": "foot -e bash -c btop",
"on-click-right": "powersave toggle"
}, },
"temperature": { "temperature": {
"hwmon-path": "/sys/class/hwmon/hwmon1/temp1_input", "hwmon-path": "/sys/class/hwmon/hwmon1/temp1_input",
"format": "{temperatureC}°C", "format": "{temperatureC}°C",
"on-click": "foot -e bash -c btop" "on-click": "foot -e bash -c btop",
"on-click-right": "powersave toggle"
},
"custom/powersave": {
"exec": "powersave waybar",
"interval": 1,
"on-click": "foot -e bash -c btop",
"on-click-right": "powersave toggle"
} }
} }
''; '';

View file

@ -1,28 +1,27 @@
{ style, ... }: { { style, ... }: {
text = '' text = ''
#clock,
#battery,
#pulseaudio, #pulseaudio,
#tray, #tray,
#mpris, #mpris,
#cpu,
#battery,
#language { #language {
padding: 8px 12px; padding: 8px 0 8px 24px;
}
#temperature,
#memory,
#custom-powersave,
#custom-powerlimit {
padding: 8px 0 8px 0;
} }
#clock { #clock {
font-weight: bold; font-weight: bold;
} }
#cpu { #tray {
padding: 8px 0 8px 12px; padding-right: 12px;
}
#temperature {
padding: 8px 0 8px 0;
}
#memory {
padding: 8px 12px 8px 0;
} }
''; '';
} }

View file

@ -0,0 +1,26 @@
{ controlFile, enable, disable, ... }: {
script = ''
function _toggle() {
if _status; then
echo ${disable} > ${controlFile}
else
echo ${enable} > ${controlFile}
fi
true
}
function _waybar() {
_status || echo -n "󰓅"
}
function _status() {
local current=$(cat ${controlFile})
local enabled="${enable}"
[[ "''${current}" = "''${enabled}" ]]
}
_''${1}
'';
}

View file

@ -0,0 +1,18 @@
{ lib, pkgs, controlFile, enable, disable, ... }: {
systemd = {
services.powersave = {
description = "Disable CPU Boost";
enable = true;
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "simple";
RemainAfterExit = "yes";
ExecStart = "${lib.getExe pkgs.bash} -c 'echo ${enable} > ${controlFile}'";
ExecStop = "${lib.getExe pkgs.bash} -c 'echo ${disable} > ${controlFile}'";
};
};
# NOTE: Allow user access.
tmpfiles.rules = [ "z ${controlFile} 0777 - - - -" ];
};
}