batmon/nix/module.nix

60 lines
1.5 KiB
Nix
Raw Normal View History

2025-01-08 08:08:07 +00:00
self: {
2024-01-11 20:43:27 +00:00
config,
pkgs,
lib,
...
}: let
2025-01-08 08:08:07 +00:00
inherit (lib.modules) mkIf mkForce;
2024-01-11 20:43:27 +00:00
inherit (lib.options) mkOption mkEnableOption;
2025-01-08 08:08:07 +00:00
inherit (lib) types;
2024-01-11 20:43:27 +00:00
2025-01-08 08:08:07 +00:00
format = pkgs.formats.json {};
2024-01-11 20:43:27 +00:00
cfg = config.services.batmon;
in {
options.services.batmon = {
enable = mkEnableOption "batmon for real-time battery monitoring";
package = mkOption {
description = "The batmon package to use";
type = types.package;
default = self.packages.${pkgs.stdenv.hostPlatform.system}.batmon;
};
settings = mkOption {
2025-01-08 08:08:07 +00:00
type = format.type;
2024-01-11 20:43:27 +00:00
default = {};
2025-01-08 08:08:07 +00:00
example = {
batPaths = [
{
path = "/sys/class/power_supply/BAT0";
extraCommand = "notify-send 'State changed!'";
}
];
};
2025-01-08 08:38:12 +00:00
description = "Settings for Batmon";
2024-01-11 20:43:27 +00:00
};
};
config = mkIf cfg.enable {
2025-01-08 08:23:30 +00:00
environment.systemPackages = [cfg.package];
systemd.user.services.batmon = {
description = "Simple, reactive power management service";
2025-01-08 08:38:12 +00:00
documentation = ["https://github.com/NotAShelf/batmon"];
2025-01-08 08:08:07 +00:00
wants = ["power-profiles-daemon.service"];
2025-01-08 08:23:30 +00:00
requires = ["power-profiles-daemon.service"];
wantedBy = ["multi-user.target"];
environment.PATH = mkForce "/run/wrappers/bin:${lib.makeBinPath [cfg.package]}";
2024-01-11 20:43:27 +00:00
script = ''
${lib.getExe cfg.package} --config ${builtins.toJSON cfg.settings}
'';
serviceConfig = {
Type = "simple";
Restart = "on-failure";
2025-01-08 08:08:07 +00:00
DynamicUser = true;
2024-01-11 20:43:27 +00:00
};
};
};
}