From acded13f27aaabc91702f184ea147420f53b691f Mon Sep 17 00:00:00 2001 From: Luna Nova Date: Sat, 19 Feb 2022 16:01:05 -0800 Subject: [PATCH 1/3] asus/battery.nix: fix chargeUpto after suspend/resume, make script optional --- asus/battery.nix | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/asus/battery.nix b/asus/battery.nix index e9bb254..79f480a 100644 --- a/asus/battery.nix +++ b/asus/battery.nix @@ -3,19 +3,35 @@ let p = pkgs.writeScriptBin "charge-upto" '' echo ''${0:-100} > /sys/class/power_supply/BAT0/charge_control_end_threshold ''; - cfg = config.hardware.asus; + 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; + options.hardware.asus.battery = { + chargeUpto = lib.mkOption { + description = "Maximum level of charge for your battery, as a percentage."; + default = 100; + type = lib.types.int; + }; + addChargeUptoScript = 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 = [ p ]; - systemd.tmpfiles.rules = [ - "w /sys/class/power_supply/BAT0/charge_control_end_threshold - - - - ${toString cfg.battery.chargeUpto}" - ]; + environment.systemPackages = lib.mkIf cfg.addChargeUptoScript [ 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 = "/bin/sh -c 'echo ${toString cfg.chargeUpto} > /sys/class/power_supply/BAT0/charge_control_end_threshold'"; + }; + }; }; } From 472f72a42d14a598f7a4f4c7423e9805ab4ee489 Mon Sep 17 00:00:00 2001 From: Luna Nova Date: Sun, 20 Feb 2022 06:16:38 -0800 Subject: [PATCH 2/3] Apply @Mic92's pkgs.runtimeShell suggestion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jörg Thalheim --- asus/battery.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asus/battery.nix b/asus/battery.nix index 79f480a..14c45e3 100644 --- a/asus/battery.nix +++ b/asus/battery.nix @@ -30,7 +30,7 @@ in serviceConfig = { Type = "oneshot"; Restart = "on-failure"; - ExecStart = "/bin/sh -c 'echo ${toString cfg.chargeUpto} > /sys/class/power_supply/BAT0/charge_control_end_threshold'"; + ExecStart = "${pkgs.runtimeShell} -c 'echo ${toString cfg.chargeUpto} > /sys/class/power_supply/BAT0/charge_control_end_threshold'"; }; }; }; From ca893110b3f842cc54135dc796922317e87f6ff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 23 Feb 2022 10:23:56 +0100 Subject: [PATCH 3/3] assus/battery: rename option to match nixpkgs convention --- asus/battery.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/asus/battery.nix b/asus/battery.nix index 14c45e3..c9a45b1 100644 --- a/asus/battery.nix +++ b/asus/battery.nix @@ -13,14 +13,14 @@ in default = 100; type = lib.types.int; }; - addChargeUptoScript = lib.mkOption { + 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.addChargeUptoScript [ p ]; + environment.systemPackages = lib.mkIf cfg.enableChargeUptoScript [ p ]; systemd.services.battery-charge-threshold = { wantedBy = [ "local-fs.target" "suspend.target" ]; after = [ "local-fs.target" "suspend.target" ];