1{ config, pkgs, lib, ... }:
2with lib;
3let
4 cfg = config.services.karma;
5 yaml = pkgs.formats.yaml { };
6in
7{
8 options.services.karma = {
9 enable = mkEnableOption "the Karma dashboard service";
10
11 package = mkPackageOption pkgs "karma" { };
12
13 configFile = mkOption {
14 type = types.path;
15 default = yaml.generate "karma.yaml" cfg.settings;
16 defaultText = "A configuration file generated from the provided nix attributes settings option.";
17 description = ''
18 A YAML config file which can be used to configure karma instead of the nix-generated file.
19 '';
20 example = "/etc/karma/karma.conf";
21 };
22
23 environment = mkOption {
24 type = with types; attrsOf str;
25 default = {};
26 description = ''
27 Additional environment variables to provide to karma.
28 '';
29 example = {
30 ALERTMANAGER_URI = "https://alertmanager.example.com";
31 ALERTMANAGER_NAME= "single";
32 };
33 };
34
35 openFirewall = mkOption {
36 type = types.bool;
37 default = false;
38 description = ''
39 Whether to open ports in the firewall needed for karma to function.
40 '';
41 };
42
43 extraOptions = mkOption {
44 type = with types; listOf str;
45 default = [];
46 description = ''
47 Extra command line options.
48 '';
49 example = [
50 "--alertmanager.timeout 10s"
51 ];
52 };
53
54 settings = mkOption {
55 type = types.submodule {
56 freeformType = yaml.type;
57
58 options.listen = {
59 address = mkOption {
60 type = types.str;
61 default = "127.0.0.1";
62 description = ''
63 Hostname or IP to listen on.
64 '';
65 example = "[::]";
66 };
67
68 port = mkOption {
69 type = types.port;
70 default = 8080;
71 description = ''
72 HTTP port to listen on.
73 '';
74 example = 8182;
75 };
76 };
77 };
78 default = {
79 listen = {
80 address = "127.0.0.1";
81 };
82 };
83 description = ''
84 Karma dashboard configuration as nix attributes.
85
86 Reference: <https://github.com/prymitive/karma/blob/main/docs/CONFIGURATION.md>
87 '';
88 example = {
89 listen = {
90 address = "192.168.1.4";
91 port = "8000";
92 prefix = "/dashboard";
93 };
94 alertmanager = {
95 interval = "15s";
96 servers = [
97 {
98 name = "prod";
99 uri = "http://alertmanager.example.com";
100 }
101 ];
102 };
103 };
104 };
105 };
106
107 config = mkIf cfg.enable {
108 systemd.services.karma = {
109 description = "Alert dashboard for Prometheus Alertmanager";
110 wantedBy = [ "multi-user.target" ];
111 environment = cfg.environment;
112 serviceConfig = {
113 Type = "simple";
114 DynamicUser = true;
115 Restart = "on-failure";
116 ExecStart = "${pkgs.karma}/bin/karma --config.file ${cfg.configFile} ${concatStringsSep " " cfg.extraOptions}";
117 };
118 };
119 networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.settings.listen.port ];
120 };
121}