2021-11-19 00:01:56 +01:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
let
|
|
|
|
p = pkgs.writeScriptBin "charge-upto" ''
|
2022-11-09 13:47:07 +01:00
|
|
|
echo ''${0:-100} > /sys/class/power_supply/BAT?/charge_control_end_threshold
|
2021-11-19 00:01:56 +01:00
|
|
|
'';
|
2022-02-20 01:01:05 +01:00
|
|
|
cfg = config.hardware.asus.battery;
|
2021-11-19 00:01:56 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2022-02-20 01:01:05 +01:00
|
|
|
options.hardware.asus.battery = {
|
|
|
|
chargeUpto = lib.mkOption {
|
|
|
|
description = "Maximum level of charge for your battery, as a percentage.";
|
|
|
|
default = 100;
|
|
|
|
type = lib.types.int;
|
|
|
|
};
|
2022-02-23 10:23:56 +01:00
|
|
|
enableChargeUptoScript = lib.mkOption {
|
2022-02-20 01:01:05 +01:00
|
|
|
description = "Whether to add charge-upto to environment.systemPackages. `charge-upto 75` temporarily sets the charge limit to 75%.";
|
|
|
|
default = true;
|
|
|
|
type = lib.types.bool;
|
|
|
|
};
|
2021-11-19 00:01:56 +01:00
|
|
|
};
|
|
|
|
config = {
|
2022-02-23 10:23:56 +01:00
|
|
|
environment.systemPackages = lib.mkIf cfg.enableChargeUptoScript [ p ];
|
2022-02-20 01:01:05 +01:00
|
|
|
systemd.services.battery-charge-threshold = {
|
|
|
|
wantedBy = [ "local-fs.target" "suspend.target" ];
|
|
|
|
after = [ "local-fs.target" "suspend.target" ];
|
|
|
|
description = "Set the battery charge threshold to ${toString cfg.chargeUpto}%";
|
|
|
|
startLimitBurst = 5;
|
|
|
|
startLimitIntervalSec = 1;
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
Restart = "on-failure";
|
2022-11-09 13:47:07 +01:00
|
|
|
ExecStart = "${pkgs.runtimeShell} -c 'echo ${toString cfg.chargeUpto} > /sys/class/power_supply/BAT?/charge_control_end_threshold'";
|
2022-02-20 01:01:05 +01:00
|
|
|
};
|
|
|
|
};
|
2021-11-19 00:01:56 +01:00
|
|
|
};
|
|
|
|
}
|