2024-05-04 23:15:57 +03:00
|
|
|
# ThinkPad charge limits.
|
2024-06-25 04:04:39 +03:00
|
|
|
{ pkgs, lib, config, ... } @args: with lib; let
|
|
|
|
cfg = config.module.powerlimit.thinkpad;
|
|
|
|
|
2024-05-04 23:15:57 +03:00
|
|
|
controlFileMin = "/sys/class/power_supply/BAT0/charge_control_start_threshold";
|
|
|
|
controlFileMax = "/sys/class/power_supply/BAT0/charge_control_end_threshold";
|
|
|
|
|
2024-06-25 04:04:39 +03:00
|
|
|
script = pkgs.writeShellScriptBin "powerlimit" ''
|
|
|
|
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-09-11 18:27:04 +03:00
|
|
|
pkill -RTMIN+6 waybar
|
2024-06-25 04:04:39 +03:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2024-09-13 04:16:08 +03:00
|
|
|
function widget() {
|
2024-09-13 23:57:09 +03:00
|
|
|
status && printf '' || printf ''
|
2024-06-25 04:04:39 +03:00
|
|
|
}
|
2024-05-04 23:15:57 +03:00
|
|
|
|
2024-06-25 04:04:39 +03:00
|
|
|
function status() {
|
|
|
|
local current=$(cat ${controlFileMax})
|
|
|
|
local enabled="${toString cfg.onMax}"
|
|
|
|
|
|
|
|
[[ "''${current}" = "''${enabled}" ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
''${@}
|
|
|
|
'';
|
2024-05-04 23:15:57 +03:00
|
|
|
in {
|
2024-06-25 04:04:39 +03:00
|
|
|
options = {
|
|
|
|
module.powerlimit.thinkpad = {
|
|
|
|
enable = mkEnableOption "Powerlimit Service";
|
|
|
|
onMin = mkOption {
|
|
|
|
default = 40;
|
|
|
|
type = types.int;
|
|
|
|
};
|
|
|
|
onMax = mkOption {
|
|
|
|
default = 80;
|
|
|
|
type = types.int;
|
|
|
|
};
|
|
|
|
offMin = mkOption {
|
|
|
|
default = 90;
|
|
|
|
type = types.int;
|
|
|
|
};
|
|
|
|
offMax = mkOption {
|
|
|
|
default = 95;
|
|
|
|
type = types.int;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
environment.systemPackages = [ script ];
|
|
|
|
systemd = {
|
|
|
|
services.powerlimit = {
|
|
|
|
description = "Limit battery charge.";
|
|
|
|
enable = true;
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
|
|
|
RemainAfterExit = "yes";
|
|
|
|
ExecStart = "${getExe pkgs.bash} -c 'echo ${toString cfg.onMin} > ${controlFileMin}; echo ${toString cfg.onMax} > ${controlFileMax};'";
|
|
|
|
ExecStop = "${getExe pkgs.bash} -c 'echo ${toString cfg.offMax} > ${controlFileMax}; echo ${toString cfg.offMin} > ${controlFileMin};'";
|
|
|
|
};
|
|
|
|
};
|
2024-05-04 23:15:57 +03:00
|
|
|
|
2024-06-25 04:04:39 +03:00
|
|
|
# HACK: Allow user access.
|
|
|
|
tmpfiles.rules = [
|
|
|
|
"z ${controlFileMax} 0777 - - - -"
|
|
|
|
"z ${controlFileMin} 0777 - - - -"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
2024-05-04 23:15:57 +03:00
|
|
|
}
|