From 9c4a5c3cf373cca71ee1306a4ecb92c288c7b9df Mon Sep 17 00:00:00 2001 From: fidgetingbits Date: Mon, 3 Jun 2024 23:36:38 +0800 Subject: [PATCH] Move battery options to non-asus specific folder --- asus/ally/rc71l/default.nix | 2 +- asus/battery.nix | 37 ----------------- asus/rog-strix/g513im/default.nix | 2 +- asus/rog-strix/g733qs/default.nix | 2 +- common/pc/laptop/battery.nix | 69 +++++++++++++++++++++++++++++++ flake.nix | 2 +- 6 files changed, 73 insertions(+), 41 deletions(-) delete mode 100644 asus/battery.nix create mode 100644 common/pc/laptop/battery.nix diff --git a/asus/ally/rc71l/default.nix b/asus/ally/rc71l/default.nix index 9cc83fed6..9855acb53 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 d61e35027..000000000 --- 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 04c3559d2..1f5f11060 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 1515285f1..2fcf93642 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 000000000..7ca44774d --- /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 8ac61b3f6..510d867a5 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;