2024-05-06 19:34:00 +03:00
|
|
|
{ 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" ''
|
2024-05-08 18:26:33 +03:00
|
|
|
function toggle() {
|
|
|
|
if status; then
|
2024-05-06 19:34:00 +03:00
|
|
|
echo ${offMax} > ${controlFileMax}
|
|
|
|
echo ${offMin} > ${controlFileMin}
|
|
|
|
else
|
|
|
|
echo ${onMin} > ${controlFileMin}
|
|
|
|
echo ${onMax} > ${controlFileMax}
|
|
|
|
fi
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2024-05-08 18:26:33 +03:00
|
|
|
function waybar() {
|
|
|
|
status || echo -n ""
|
2024-05-06 19:34:00 +03:00
|
|
|
}
|
|
|
|
|
2024-05-08 18:26:33 +03:00
|
|
|
function status() {
|
2024-05-06 19:34:00 +03:00
|
|
|
local current=$(cat ${controlFileMax})
|
|
|
|
local enabled="${onMax}"
|
|
|
|
|
|
|
|
[[ "''${current}" = "''${enabled}" ]]
|
|
|
|
}
|
|
|
|
|
2024-05-08 18:26:33 +03:00
|
|
|
''${@}
|
2024-05-06 19:34:00 +03:00
|
|
|
'';
|
|
|
|
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};'";
|
|
|
|
};
|
2024-03-04 00:34:39 +03:00
|
|
|
};
|
2024-05-06 19:34:00 +03:00
|
|
|
|
2024-05-06 22:30:55 +03:00
|
|
|
# HACK: Allow user access.
|
2024-05-06 19:34:00 +03:00
|
|
|
tmpfiles.rules = [
|
|
|
|
"z ${controlFileMax} 0777 - - - -"
|
|
|
|
"z ${controlFileMin} 0777 - - - -"
|
|
|
|
];
|
2024-03-04 00:34:39 +03:00
|
|
|
};
|
2024-05-06 19:34:00 +03:00
|
|
|
|
2024-05-06 22:11:33 +03:00
|
|
|
systemd.services.keyd.path = [ script ];
|
2024-05-06 19:34:00 +03:00
|
|
|
environment.systemPackages = [ script ];
|
2024-03-04 00:34:39 +03:00
|
|
|
}
|