From a9cfac3bd38744b2da9cc62e90574113534a63a0 Mon Sep 17 00:00:00 2001 From: sternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org> Date: Thu, 25 Feb 2021 15:36:58 +0100 Subject: modules/hardware/low-battery: suspend on low battery --- modules/hardware/low-battery.nix | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 modules/hardware/low-battery.nix (limited to 'modules/hardware') diff --git a/modules/hardware/low-battery.nix b/modules/hardware/low-battery.nix new file mode 100644 index 00000000..40c21178 --- /dev/null +++ b/modules/hardware/low-battery.nix @@ -0,0 +1,45 @@ +# based on https://code.tvl.fyi/tree/users/glittershark/system/system/modules/reusable/battery.nix +{ config, lib, pkgs, ... }: + +let + + inherit (pkgs.vuizvui.profpatsch) + getBins + ; + + cfg = config.vuizvui.hardware.low-battery; + + bins = getBins pkgs.systemd [ "systemctl" ]; + +in { + options = { + vuizvui.hardware.low-battery = { + enable = lib.mkEnableOption "suspend on low battery"; + + treshold = lib.mkOption { + description = "Percentage treshold on which to suspend"; + type = lib.types.int; + default = 5; + }; + + action = lib.mkOption { + description = "Type of suspend action to perform when treshold is reached"; + type = lib.types.enum [ + "hibernate" + "suspend" + "suspend-then-hibernate" + ]; + default = "suspend"; + }; + }; + }; + + config = lib.mkIf cfg.enable { + services.udev.extraRules = lib.concatStrings [ + ''SUBSYSTEM=="power_supply", '' + ''ATTR{status}=="Discharging", '' + ''ATTR{capacity}=="[0-${toString cfg.treshold}]", '' + ''RUN+="${bins.systemctl} ${cfg.action}"'' + ]; + }; +} -- cgit 1.4.1