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