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