about summary refs log tree commit diff
path: root/modules/hardware/low-battery.nix
blob: 40c211781648d3398bbf7af26e123e230d0cfa62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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}"''
    ];
  };
}