diff options
-rw-r--r-- | lass/3modules/power-action.nix | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/lass/3modules/power-action.nix b/lass/3modules/power-action.nix index 631e651ff..06a316270 100644 --- a/lass/3modules/power-action.nix +++ b/lass/3modules/power-action.nix @@ -25,6 +25,16 @@ let plans = mkOption { type = with types; attrsOf (submodule { options = { + charging = mkOption { + type = nullOr bool; + default = null; + description = '' + check for charging status. + null = don't care + true = only if system is charging + false = only if system is discharging + ''; + }; upperLimit = mkOption { type = int; }; @@ -53,15 +63,31 @@ let }; startScript = pkgs.writeDash "power-action" '' + set -euf + power="$(${powerlvl})" + state="$(${state})" ${concatStringsSep "\n" (mapAttrsToList writeRule cfg.plans)} ''; + charging_check = plan: + if (plan.charging == null) then "" else + if plan.charging + then ''&& [ "$state" = "true" ]'' + else ''&& ! [ "$state" = "true" ]'' + ; writeRule = _: plan: - "if [ $power -ge ${toString plan.lowerLimit} ] && [ $power -le ${toString plan.upperLimit} ]; then ${plan.action}; fi"; + "if [ $power -ge ${toString plan.lowerLimit} ] && [ $power -le ${toString plan.upperLimit} ] ${charging_check plan}; then ${plan.action}; fi"; powerlvl = pkgs.writeDash "powerlvl" '' cat /sys/class/power_supply/BAT0/capacity ''; + state = pkgs.writeDash "state" '' + if [ "$(cat /sys/class/power_supply/BAT0/status)" = "Charging" ] + then echo "true" + else echo "false" + fi + ''; + in out |