diff --git a/asus/ally/rc71l/default.nix b/asus/ally/rc71l/default.nix index 9cc83fe..9855acb 100644 --- a/asus/ally/rc71l/default.nix +++ b/asus/ally/rc71l/default.nix @@ -6,7 +6,7 @@ ../../../common/gpu/amd ../../../common/pc/laptop ../../../common/pc/laptop/ssd - ../../battery.nix + ../../../common/pc/laptop/battery.nix ]; # 6.5 adds many fixes and improvements for the Ally diff --git a/asus/battery.nix b/asus/battery.nix deleted file mode 100644 index d61e350..0000000 --- a/asus/battery.nix +++ /dev/null @@ -1,37 +0,0 @@ -{ config, pkgs, lib, ... }: -let - p = pkgs.writeScriptBin "charge-upto" '' - echo ''${0:-100} > /sys/class/power_supply/BAT?/charge_control_end_threshold - ''; - cfg = config.hardware.asus.battery; -in - -{ - options.hardware.asus.battery = { - chargeUpto = lib.mkOption { - description = "Maximum level of charge for your battery, as a percentage."; - default = 100; - type = lib.types.int; - }; - enableChargeUptoScript = lib.mkOption { - 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; - }; - }; - config = { - environment.systemPackages = lib.mkIf cfg.enableChargeUptoScript [ p ]; - 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"; - ExecStart = "${pkgs.runtimeShell} -c 'echo ${toString cfg.chargeUpto} > /sys/class/power_supply/BAT?/charge_control_end_threshold'"; - }; - }; - }; -} diff --git a/asus/rog-strix/g513im/default.nix b/asus/rog-strix/g513im/default.nix index 04c3559..1f5f110 100644 --- a/asus/rog-strix/g513im/default.nix +++ b/asus/rog-strix/g513im/default.nix @@ -6,7 +6,7 @@ ../../../common/gpu/nvidia/prime.nix ../../../common/pc/laptop ../../../common/pc/ssd - ../../battery.nix + ../../../common/pc/laptop/battery.nix ]; hardware.nvidia.prime = { diff --git a/asus/rog-strix/g733qs/default.nix b/asus/rog-strix/g733qs/default.nix index 1515285..2fcf936 100644 --- a/asus/rog-strix/g733qs/default.nix +++ b/asus/rog-strix/g733qs/default.nix @@ -5,7 +5,7 @@ ../../../common/gpu/nvidia/prime.nix ../../../common/pc/laptop ../../../common/pc/ssd - ../../battery.nix + ../../../common/pc/laptop/battery.nix ]; # fixing audio by overriding pins as suggested in diff --git a/common/pc/laptop/battery.nix b/common/pc/laptop/battery.nix new file mode 100644 index 0000000..7ca4477 --- /dev/null +++ b/common/pc/laptop/battery.nix @@ -0,0 +1,69 @@ +{ + config, + pkgs, + lib, + ... +}: +let + # This interface works for most Asus, Lenovo Thinkpad, LG, System76, and Toshiba laptops + # A list of supported laptop models/vendors supporting the following interface can be found + # at https://linrunner.de/tlp/settings/bc-vendors.html + # If your laptop isn't supported, considering installing and using the tlp package's setcharge command instead + interfaces = "/sys/class/power_supply/BAT?/charge_control_end_threshold"; + charge-upto-script = pkgs.writeScriptBin "charge-upto" '' + echo ''${1:-100} >${interfaces} + ''; + cfg = config.hardware.battery; +in + +{ + options.hardware.battery = { + chargeUpto = lib.mkOption { + description = "Maximum level of charge for your battery, as a percentage. Applies threshold to all installed batteries"; + default = 100; + type = lib.types.int; + }; + enableChargeUptoScript = lib.mkOption { + description = "Whether to add charge-upto script to environment.systemPackages. `charge-upto 75` temporarily sets the charge limit to 75%."; + default = true; + type = lib.types.bool; + }; + }; + imports = ( + map + ( + option: + lib.mkRemovedOptionModule [ + "hardware" + "asus" + "battery" + option + ] "The hardware.asus.battery.* options were removed in favor of `hardware.battery.*`." + ) + [ + "chargeUpto" + "enableChargeUptoScript" + ] + ); + config = { + environment.systemPackages = lib.mkIf cfg.enableChargeUptoScript [ charge-upto-script ]; + 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"; + ExecStart = "${pkgs.runtimeShell} -c 'echo ${toString cfg.chargeUpto} > ${interfaces}'"; + }; + }; + }; +} diff --git a/flake.nix b/flake.nix index 8ac61b3..510d867 100644 --- a/flake.nix +++ b/flake.nix @@ -15,7 +15,6 @@ apple-macbook-pro-14-1 = import ./apple/macbook-pro/14-1; apple-macmini-4-1 = import ./apple/macmini/4; apple-t2 = import ./apple/t2; - asus-battery = import ./asus/battery.nix; asus-ally-rc71l = import ./asus/ally/rc71l; asus-fx504gd = import ./asus/fx504gd; asus-fa507nv = import ./asus/fa507nv; @@ -259,6 +258,7 @@ common-pc-hdd = import ./common/pc/hdd; common-pc-laptop = import ./common/pc/laptop; common-pc-laptop-acpi_call = import ./common/pc/laptop/acpi_call.nix; + common-pc-laptop-battery = import ./common/pc/laptop/battery.nix; common-pc-laptop-hdd = import ./common/pc/laptop/hdd; common-pc-laptop-ssd = import ./common/pc/ssd; common-pc-ssd = import ./common/pc/ssd;