summaryrefslogtreecommitdiffstats
path: root/lass/3modules/power-action.nix
diff options
context:
space:
mode:
authormakefu <github@syntax-fehler.de>2016-06-23 17:04:07 +0200
committermakefu <github@syntax-fehler.de>2016-06-23 17:04:07 +0200
commit271a59b1d6222f7336fac427631ee5bb569a000e (patch)
tree804b845cebb530001e79b16ddcb70d2d12d8c7f5 /lass/3modules/power-action.nix
parentd5e0dcccd074d7e63cfa7fff35782cb92c101270 (diff)
parenta8dcedbb99151ce396d58573f339fa34d7537fa1 (diff)
Merge remote-tracking branch 'prism/master'
Diffstat (limited to 'lass/3modules/power-action.nix')
-rw-r--r--lass/3modules/power-action.nix93
1 files changed, 93 insertions, 0 deletions
diff --git a/lass/3modules/power-action.nix b/lass/3modules/power-action.nix
new file mode 100644
index 000000000..06a316270
--- /dev/null
+++ b/lass/3modules/power-action.nix
@@ -0,0 +1,93 @@
+{ config, lib, pkgs, ... }:
+
+with config.krebs.lib;
+
+let
+ cfg = config.lass.power-action;
+
+ out = {
+ options.lass.power-action = api;
+ config = lib.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 = {
+ charging = mkOption {
+ type = nullOr bool;
+ default = null;
+ description = ''
+ check for charging status.
+ null = don't care
+ true = only if system is charging
+ false = only if system is discharging
+ '';
+ };
+ upperLimit = mkOption {
+ type = int;
+ };
+ lowerLimit = mkOption {
+ type = int;
+ };
+ action = mkOption {
+ type = path;
+ };
+ };
+ });
+ };
+ };
+
+ imp = {
+ systemd.services.power-action = {
+ serviceConfig = rec {
+ ExecStart = startScript;
+ User = cfg.user.name;
+ };
+ startAt = cfg.startAt;
+ };
+ users.users.${cfg.user.name} = {
+ inherit (cfg.user) name uid;
+ };
+ };
+
+ startScript = pkgs.writeDash "power-action" ''
+ set -euf
+
+ power="$(${powerlvl})"
+ state="$(${state})"
+ ${concatStringsSep "\n" (mapAttrsToList writeRule cfg.plans)}
+ '';
+ charging_check = plan:
+ if (plan.charging == null) then "" else
+ if plan.charging
+ then ''&& [ "$state" = "true" ]''
+ else ''&& ! [ "$state" = "true" ]''
+ ;
+
+ writeRule = _: plan:
+ "if [ $power -ge ${toString plan.lowerLimit} ] && [ $power -le ${toString plan.upperLimit} ] ${charging_check plan}; then ${plan.action}; fi";
+
+ powerlvl = pkgs.writeDash "powerlvl" ''
+ cat /sys/class/power_supply/BAT0/capacity
+ '';
+
+ state = pkgs.writeDash "state" ''
+ if [ "$(cat /sys/class/power_supply/BAT0/status)" = "Charging" ]
+ then echo "true"
+ else echo "false"
+ fi
+ '';
+
+in out