From c8fc9152fd8756bc1418b7489228153b769a4cfd Mon Sep 17 00:00:00 2001 From: Felix Buehler Date: Mon, 1 Sep 2025 23:45:41 +0200 Subject: [PATCH] hardware/action-on-low-power: init --- machines/thinkman/hardware-configuration.nix | 1 + .../hardware/action-on-low-power/default.nix | 45 +++++++++++++++++++ modules/hardware/default.nix | 1 + 3 files changed, 47 insertions(+) create mode 100644 modules/hardware/action-on-low-power/default.nix diff --git a/machines/thinkman/hardware-configuration.nix b/machines/thinkman/hardware-configuration.nix index af5463d..79c605c 100644 --- a/machines/thinkman/hardware-configuration.nix +++ b/machines/thinkman/hardware-configuration.nix @@ -15,6 +15,7 @@ in services.tlp.enable = true; my.hardware = { + action-on-low-power.enable = true; bluetooth.enable = true; debug.enable = true; drive-monitor.enable = true; diff --git a/modules/hardware/action-on-low-power/default.nix b/modules/hardware/action-on-low-power/default.nix new file mode 100644 index 0000000..e600778 --- /dev/null +++ b/modules/hardware/action-on-low-power/default.nix @@ -0,0 +1,45 @@ +# low power action +{ + config, + lib, + options, + pkgs, + ... +}: +let + cfg = config.my.hardware.action-on-low-power; +in +{ + options.my.hardware.action-on-low-power = { + enable = lib.mkEnableOption "action on low power"; + + action = lib.mkOption { + type = lib.types.enum [ + "hibernate" + "hybrid-sleep" + "poweroff" + "sleep" + "suspend-then-hibernate" + "suspend" + ]; + default = "sleep"; + description = '' + Action to take when power is low. + ''; + }; + + powerInPercent = lib.mkOption { + type = lib.types.int; + default = 10; + description = '' + Power percentage threshold to trigger the action. + ''; + }; + }; + + config = lib.mkIf cfg.enable { + services.udev.extraRules = '' + SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="${toString cfg.powerInPercent}", RUN+="${config.systemd.package}/bin/systemctl ${cfg.action}" + ''; + }; +} diff --git a/modules/hardware/default.nix b/modules/hardware/default.nix index 11dc7e9..50b9dbe 100644 --- a/modules/hardware/default.nix +++ b/modules/hardware/default.nix @@ -2,6 +2,7 @@ { ... }: { imports = [ + ./action-on-low-power ./bluetooth ./debug ./drive-monitor