nix/module/PowerlimitThinkpad.nix

89 lines
2 KiB
Nix
Raw Normal View History

# ThinkPad charge limits.
{
2024-11-04 04:37:29 +03:00
config,
lib,
pkgs,
util,
...
}: let
cfg = config.module.powerlimit.thinkpad;
2024-06-25 04:04:39 +03:00
2024-11-04 04:37:29 +03:00
controlFileMax = "/sys/class/power_supply/BAT0/charge_control_end_threshold";
controlFileMin = "/sys/class/power_supply/BAT0/charge_control_start_threshold";
2024-11-04 04:37:29 +03:00
script = pkgs.writeShellScriptBin "powerlimit" (util.trimTabs ''
function toggle() {
if status; then
echo ${toString cfg.offMax} > ${controlFileMax}
echo ${toString cfg.offMin} > ${controlFileMin}
else
echo ${toString cfg.onMin} > ${controlFileMin}
echo ${toString cfg.onMax} > ${controlFileMax}
fi
2024-06-25 04:04:39 +03:00
2024-11-04 04:37:29 +03:00
pkill -RTMIN+6 waybar
true
}
2024-06-25 04:04:39 +03:00
2024-11-04 04:37:29 +03:00
function widget() {
status && printf '' || printf ''
}
2024-11-04 04:37:29 +03:00
function status() {
local current=$(cat ${controlFileMax})
local enabled="${toString cfg.onMax}"
2024-06-25 04:04:39 +03:00
2024-11-04 04:37:29 +03:00
[[ "''${current}" = "''${enabled}" ]]
}
2024-06-25 04:04:39 +03:00
2024-11-04 04:37:29 +03:00
''${@}
'');
in {
options.module.powerlimit.thinkpad = {
enable = lib.mkEnableOption "the powerlimit service";
onMin = lib.mkOption {
default = 40;
type = lib.types.int;
};
onMax = lib.mkOption {
default = 80;
type = lib.types.int;
};
offMin = lib.mkOption {
default = 90;
type = lib.types.int;
};
offMax = lib.mkOption {
default = 95;
type = lib.types.int;
};
};
2024-06-25 04:04:39 +03:00
2024-11-04 04:37:29 +03:00
config = lib.mkIf cfg.enable {
environment.systemPackages = [
script
];
systemd = {
services.powerlimit = {
enable = true;
description = "Limit battery charge";
wantedBy = [
"multi-user.target"
];
serviceConfig = {
Type = "simple";
RemainAfterExit = "yes";
ExecStart = "${lib.getExe pkgs.bash} -c 'echo ${toString cfg.onMin} > ${controlFileMin}; echo ${toString cfg.onMax} > ${controlFileMax};'";
ExecStop = "${lib.getExe pkgs.bash} -c 'echo ${toString cfg.offMax} > ${controlFileMax}; echo ${toString cfg.offMin} > ${controlFileMin};'";
};
};
2024-11-04 04:37:29 +03:00
# HACK: Allow user access.
tmpfiles.rules = [
"z ${controlFileMax} 0777 - - - -"
"z ${controlFileMin} 0777 - - - -"
];
};
};
}