summaryrefslogtreecommitdiffstats
path: root/lass/3modules
diff options
context:
space:
mode:
authorlassulus <lass@aidsballs.de>2016-06-16 22:42:14 +0200
committerlassulus <lass@aidsballs.de>2016-06-16 22:42:14 +0200
commit3a92ac0e9b577be9f0d3d36fdfce977171b37560 (patch)
tree2c0a107b80abfd710770f9e473229deef8e59426 /lass/3modules
parentd5ccef77d4d61f9451945ee1501513670eea8b66 (diff)
l 3: add power-action
Diffstat (limited to 'lass/3modules')
-rw-r--r--lass/3modules/default.nix1
-rw-r--r--lass/3modules/power-action.nix67
2 files changed, 68 insertions, 0 deletions
diff --git a/lass/3modules/default.nix b/lass/3modules/default.nix
index 380d83a91..b3037205e 100644
--- a/lass/3modules/default.nix
+++ b/lass/3modules/default.nix
@@ -4,6 +4,7 @@ _:
./ejabberd
./folderPerms.nix
./mysql-backup.nix
+ ./power-action.nix
./urxvtd.nix
./wordpress_nginx.nix
./xresources.nix
diff --git a/lass/3modules/power-action.nix b/lass/3modules/power-action.nix
new file mode 100644
index 000000000..fd6dcf57e
--- /dev/null
+++ b/lass/3modules/power-action.nix
@@ -0,0 +1,67 @@
+{ config, lib, pkgs, ... }:
+
+with lib;
+
+let
+ cfg = config.lass.power-action;
+
+ out = {
+ options.lass.power-action = api;
+ config = mkIf cfg.enable imp;
+ };
+
+ api = {
+ enable = mkEnableOption "power-action";
+ user = mkOption {
+ type = types.user;
+ default = {
+ name = "power-action";
+ };
+ };
+ startAt = mkOption {
+ type = types.str;
+ default = "*:0/1";
+ };
+ plans = mkOption {
+ type = with types; attrsOf (submodule {
+ options = {
+ upperLimit = mkOption {
+ type = int;
+ };
+ lowerLimit = mkOption {
+ type = int;
+ };
+ action = mkOption {
+ type = path;
+ };
+ };
+ });
+ };
+ };
+
+ imp = {
+ systemd.services.power-action = {
+ serviceConfig = rec {
+ ExecStart = startScript;
+ User = cfg.user;
+ };
+ startAt = cfg.startAt;
+ };
+ users.users.${cfg.user.name} = {
+ inherit (cfg.user) name uid;
+ };
+ };
+
+ startScript = pkgs.writeDash "power-action" ''
+ power="$(${powerlvl})"
+ ${concatStringsSep "\n" (mapAttrsToList writeRule cfg.plans)}
+ '';
+
+ writeRule = _: plan:
+ "if [ $power -ge ${toString plan.lowerLimit} ] && [ $power -le ${toString plan.upperLimit} ]; then ${plan.action}; fi";
+
+ powerlvl = pkgs.writeDash "powerlvl" ''
+ cat /sys/class/power_supply/BAT0/capacity
+ '';
+
+in out