From 3a92ac0e9b577be9f0d3d36fdfce977171b37560 Mon Sep 17 00:00:00 2001 From: lassulus Date: Thu, 16 Jun 2016 22:42:14 +0200 Subject: l 3: add power-action --- lass/3modules/default.nix | 1 + lass/3modules/power-action.nix | 67 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 lass/3modules/power-action.nix (limited to 'lass/3modules') 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 -- cgit v1.2.3 From a6d1879739593d0bc53cf218f031b68410767d90 Mon Sep 17 00:00:00 2001 From: lassulus Date: Sat, 18 Jun 2016 19:51:07 +0200 Subject: =?UTF-8?q?l=203=20power-action:=20fix=20some=20stuff=20(=C2=AF\?= =?UTF-8?q?=5F(=E3=83=84)=5F/=C2=AF)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lass/3modules/power-action.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lass/3modules') diff --git a/lass/3modules/power-action.nix b/lass/3modules/power-action.nix index fd6dcf57e..631e651ff 100644 --- a/lass/3modules/power-action.nix +++ b/lass/3modules/power-action.nix @@ -1,13 +1,13 @@ { config, lib, pkgs, ... }: -with lib; +with config.krebs.lib; let cfg = config.lass.power-action; out = { options.lass.power-action = api; - config = mkIf cfg.enable imp; + config = lib.mkIf cfg.enable imp; }; api = { @@ -43,7 +43,7 @@ let systemd.services.power-action = { serviceConfig = rec { ExecStart = startScript; - User = cfg.user; + User = cfg.user.name; }; startAt = cfg.startAt; }; -- cgit v1.2.3 From 1b238cf556064996b9db9b4a86c232228a0e9114 Mon Sep 17 00:00:00 2001 From: lassulus Date: Sat, 18 Jun 2016 21:51:45 +0200 Subject: l 3 power-action: add charging check --- lass/3modules/power-action.nix | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'lass/3modules') diff --git a/lass/3modules/power-action.nix b/lass/3modules/power-action.nix index 631e651ff..06a316270 100644 --- a/lass/3modules/power-action.nix +++ b/lass/3modules/power-action.nix @@ -25,6 +25,16 @@ let 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; }; @@ -53,15 +63,31 @@ let }; 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} ]; then ${plan.action}; fi"; + "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 -- cgit v1.2.3 From 885ded6ca93837c17631e983eaffcf8f4a9aee8b Mon Sep 17 00:00:00 2001 From: lassulus Date: Tue, 28 Jun 2016 17:39:03 +0200 Subject: l 3 power-action: reverse status check --- lass/3modules/power-action.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lass/3modules') diff --git a/lass/3modules/power-action.nix b/lass/3modules/power-action.nix index 06a316270..3116514a8 100644 --- a/lass/3modules/power-action.nix +++ b/lass/3modules/power-action.nix @@ -84,9 +84,9 @@ let ''; state = pkgs.writeDash "state" '' - if [ "$(cat /sys/class/power_supply/BAT0/status)" = "Charging" ] - then echo "true" - else echo "false" + if [ "$(cat /sys/class/power_supply/BAT0/status)" = "Discharging" ] + then echo "false" + else echo "true" fi ''; -- cgit v1.2.3