about summary refs log tree commit diff
path: root/modules/nixos/alertmanager.nix
blob: 9c8d60a5f2fca26d5cb85e7c775941c250b4fe6f (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
{
  config,
  inputs,
  lib,
  libNginx,
  ...
}:
with lib; let
  cfg = config.nixfiles.modules.alertmanager;
in {
  imports = [inputs.alertmanager-ntfy.nixosModules.default];

  options.nixfiles.modules.alertmanager = {
    enable = mkEnableOption "Alertmanager";

    port = mkOption {
      description = "Port.";
      type = with types; port;
      default = 30112;
    };

    domain = mkOption {
      description = "Domain name sans protocol scheme.";
      type = with types; nullOr str;
      default = "alertmanager.${config.networking.domain}";
    };
  };

  config = mkIf cfg.enable {
    nixfiles.modules = {
      ntfy.enable = true;
      nginx = {
        enable = true;
        upstreams.alertmanager.servers."127.0.0.1:${toString cfg.port}" = {};
        virtualHosts.${cfg.domain} = {
          locations."/".proxyPass = "http://alertmanager";
          extraConfig = libNginx.config.internalOnly;
        };
      };
    };

    services = {
      prometheus.alertmanager = {
        enable = true;

        listenAddress = "127.0.0.1";
        inherit (cfg) port;

        extraFlags = [
          "--cluster.listen-address=\"\""
          "--web.external-url=https://${cfg.domain}"
        ];

        configuration = {
          global = {
            smtp_from = "alertmanager@${my.domain.shire}";
            smtp_smarthost = "${my.domain.shire}:584";
          };

          route = {
            receiver = my.username;
            group_by = ["alertname"];
          };

          receivers = [
            {
              name = my.username;
              webhook_configs = [
                {
                  url = with config.pinpox.services.alertmanager-ntfy; "http://${httpAddress}:${httpPort}";
                }
              ];
            }
          ];
        };
      };
    };

    pinpox.services.alertmanager-ntfy = {
      enable = true;
      httpAddress = "127.0.0.1";
      httpPort = toString (config.nixfiles.modules.ntfy.port + 1);
      ntfyTopic = "${config.services.ntfy-sh.settings.base-url}/alertmanager";
      ntfyPriority = "high";
      envFile = "/dev/null";
    };
  };
}

Consider giving Nix/NixOS a try! <3