about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2021-02-25 15:36:58 +0100
committersternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2021-02-25 16:08:03 +0100
commita9cfac3bd38744b2da9cc62e90574113534a63a0 (patch)
treee6adb09edb52320d94ce5e41970792a4bcf9c0a8
parent7b66a7e9bdce92e259237c508e59a06e2859fb8b (diff)
modules/hardware/low-battery: suspend on low battery
-rw-r--r--machines/sternenseemann/base-laptop.nix5
-rw-r--r--modules/hardware/low-battery.nix45
-rw-r--r--modules/module-list.nix1
3 files changed, 51 insertions, 0 deletions
diff --git a/machines/sternenseemann/base-laptop.nix b/machines/sternenseemann/base-laptop.nix
index c25b3e06..a1529fb2 100644
--- a/machines/sternenseemann/base-laptop.nix
+++ b/machines/sternenseemann/base-laptop.nix
@@ -24,6 +24,11 @@
     powerManagement.cpuFreqGovernor = lib.mkDefault "powersave";
 
     vuizvui.hardware.thinkpad.enable = lib.mkDefault true;
+    vuizvui.hardware.low-battery = {
+      enable = true;
+      treshold = 3;
+      action = "hibernate";
+    };
 
     environment.systemPackages = with pkgs; [
       lr
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}"''
+    ];
+  };
+}
diff --git a/modules/module-list.nix b/modules/module-list.nix
index b4574594..ded01dab 100644
--- a/modules/module-list.nix
+++ b/modules/module-list.nix
@@ -4,6 +4,7 @@
   ./core/tests.nix
   ./core/lazy-packages.nix
   ./hardware/gamecontroller.nix
+  ./hardware/low-battery.nix
   ./hardware/rtl8192cu
   ./hardware/t100ha
   ./hardware/thinkpad.nix