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