2016-06-16 20:42:14 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
2016-06-18 17:51:07 +00:00
|
|
|
with config.krebs.lib;
|
2016-06-16 20:42:14 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.lass.power-action;
|
|
|
|
|
|
|
|
out = {
|
|
|
|
options.lass.power-action = api;
|
2016-06-18 17:51:07 +00:00
|
|
|
config = lib.mkIf cfg.enable imp;
|
2016-06-16 20:42:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2016-06-18 17:51:07 +00:00
|
|
|
User = cfg.user.name;
|
2016-06-16 20:42:14 +00:00
|
|
|
};
|
|
|
|
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
|