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, ... }: {
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 > /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;'";
{ pkgs, lib, ... }: let
controlFileMin = "/sys/class/power_supply/BAT0/charge_control_start_threshold";
controlFileMax = "/sys/class/power_supply/BAT0/charge_control_end_threshold";
onMin = "40";
onMax = "80";
offMin = "90";
offMax = "95";
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:
# CPPC (Collaborative Power Control) - Disabled.
# PSS (Cool and Quiet) - Enabled.
systemd.services.powersave = {
description = "AMD disable Boost";
enable = true;
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "simple";
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'";
};
};
imports = [
(import ./powersave ({
inherit controlFile;
inherit enable;
inherit disable;
} // args))
];
environment.systemPackages = [ script ];
}

View file

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

View file

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

View file

@ -21,9 +21,11 @@
"sway/language",
"pulseaudio",
"battery",
"custom/powerlimit",
"cpu",
"temperature",
"memory",
"custom/powersave",
"tray"
],
"sway/scratchpad": {
@ -54,7 +56,13 @@
"format-charging": "{capacity}% ",
"format-plugged": "{capacity}% ",
"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": {
"scroll-step": ${toString(setting.volume.step)},
@ -83,16 +91,25 @@
"cpu": {
"format": "{usage}% ({load})",
"tooltip": false,
"on-click": "foot -e bash -c btop"
"on-click": "foot -e bash -c btop",
"on-click-right": "powersave toggle"
},
"memory": {
"format": "{percentage}%",
"on-click": "foot -e bash -c btop"
"on-click": "foot -e bash -c btop",
"on-click-right": "powersave toggle"
},
"temperature": {
"hwmon-path": "/sys/class/hwmon/hwmon1/temp1_input",
"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, ... }: {
text = ''
#clock,
#battery,
#pulseaudio,
#tray,
#mpris,
#cpu,
#battery,
#language {
padding: 8px 12px;
padding: 8px 0 8px 24px;
}
#temperature,
#memory,
#custom-powersave,
#custom-powerlimit {
padding: 8px 0 8px 0;
}
#clock {
font-weight: bold;
}
#cpu {
padding: 8px 0 8px 12px;
}
#temperature {
padding: 8px 0 8px 0;
}
#memory {
padding: 8px 12px 8px 0;
#tray {
padding-right: 12px;
}
'';
}

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 - - - -" ];
};
}