Files
nix-config/modules/nixos/hardware/battery/default.nix
mjallen18 70002a19e2 hmm
2026-04-07 18:39:42 -05:00

55 lines
1.5 KiB
Nix
Executable File

# echo 81 | sudo tee /sys/class/power_supply/macsmc-battery/charge_control_end_threshold
{
config,
pkgs,
lib,
namespace,
...
}:
with lib;
let
inherit (lib.${namespace}) mkOpt;
cfg = config.${namespace}.hardware.battery;
in
{
options.${namespace}.hardware.battery = {
enable = mkEnableOption "battery charge limit service";
chargeLimit = mkOpt types.int 81 "Charge limit for the battery, ex: 81 = 80% charge limit";
battery = mkOpt types.str "" "Path to the battery charge limit file";
};
config = mkIf cfg.enable {
assertions = [
{
assertion = cfg.battery != "";
message = "mjallen.hardware.battery.battery must be set to the sysfs path of the battery charge limit file (e.g. \"/sys/class/power_supply/BAT0/charge_control_end_threshold\").";
}
{
assertion = cfg.chargeLimit > 0 && cfg.chargeLimit <= 100;
message = "mjallen.hardware.battery.chargeLimit must be between 1 and 100 (got ${toString cfg.chargeLimit}).";
}
];
systemd = {
services = {
set-charge-limit = {
enable = true;
wantedBy = [ "multi-user.target" ];
path = [
pkgs.uutils-coreutils-noprefix
];
script = ''
${pkgs.uutils-coreutils-noprefix}/bin/echo ${toString cfg.chargeLimit} | tee ${cfg.battery}
'';
serviceConfig = {
Type = "oneshot";
TimeoutSec = "5s";
};
};
};
};
};
}