nix/module/Powerlimit.nix

60 lines
1.4 KiB
Nix
Raw Normal View History

{ 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}" ]]
}
''${@}
'';
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-05-06 22:30:55 +03:00
# HACK: Allow user access.
tmpfiles.rules = [
"z ${controlFileMax} 0777 - - - -"
"z ${controlFileMin} 0777 - - - -"
];
};
2024-05-06 22:11:33 +03:00
systemd.services.keyd.path = [ script ];
environment.systemPackages = [ script ];
}