about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/monitoring/karma.nix
blob: b7ec5e4ae6fbb3216c9a6361bda6eaf4068c1511 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
{ config, pkgs, lib, ... }:
with lib;
let
  cfg = config.services.karma;
  yaml = pkgs.formats.yaml { };
in
{
  options.services.karma = {
    enable = mkEnableOption "the Karma dashboard service";

    package = mkPackageOption pkgs "karma" { };

    configFile = mkOption {
      type = types.path;
      default = yaml.generate "karma.yaml" cfg.settings;
      defaultText = "A configuration file generated from the provided nix attributes settings option.";
      description = ''
        A YAML config file which can be used to configure karma instead of the nix-generated file.
      '';
      example = "/etc/karma/karma.conf";
    };

    environment = mkOption {
      type = with types; attrsOf str;
      default = {};
      description = ''
        Additional environment variables to provide to karma.
      '';
      example = {
        ALERTMANAGER_URI = "https://alertmanager.example.com";
        ALERTMANAGER_NAME= "single";
      };
    };

    openFirewall = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Whether to open ports in the firewall needed for karma to function.
      '';
    };

    extraOptions = mkOption {
      type = with types; listOf str;
      default = [];
      description = ''
        Extra command line options.
      '';
      example = [
        "--alertmanager.timeout 10s"
      ];
    };

    settings = mkOption {
      type = types.submodule {
        freeformType = yaml.type;

        options.listen = {
          address = mkOption {
            type = types.str;
            default = "127.0.0.1";
            description = ''
              Hostname or IP to listen on.
            '';
            example = "[::]";
          };

          port = mkOption {
            type = types.port;
            default = 8080;
            description = ''
              HTTP port to listen on.
            '';
            example = 8182;
          };
        };
      };
      default = {
        listen = {
          address = "127.0.0.1";
        };
      };
      description = ''
        Karma dashboard configuration as nix attributes.

        Reference: <https://github.com/prymitive/karma/blob/main/docs/CONFIGURATION.md>
      '';
      example = {
        listen = {
          address = "192.168.1.4";
          port = "8000";
          prefix = "/dashboard";
        };
        alertmanager = {
          interval = "15s";
          servers = [
            {
              name = "prod";
              uri = "http://alertmanager.example.com";
            }
          ];
        };
      };
    };
  };

  config = mkIf cfg.enable {
    systemd.services.karma = {
      description = "Alert dashboard for Prometheus Alertmanager";
      wantedBy = [ "multi-user.target" ];
      environment = cfg.environment;
      serviceConfig = {
        Type = "simple";
        DynamicUser = true;
        Restart = "on-failure";
        ExecStart = "${pkgs.karma}/bin/karma --config.file ${cfg.configFile} ${concatStringsSep " " cfg.extraOptions}";
      };
    };
    networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.settings.listen.port ];
  };
}