summaryrefslogtreecommitdiff
path: root/modules/nixfiles/prometheus.nix
blob: e816b7475a7dfdde74153bd2306015071c270a2a (plain)
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
{
  config,
  lib,
  ...
}:
with lib; let
  cfg = config.nixfiles.modules.prometheus;
in {
  options.nixfiles.modules.prometheus = {
    enable = mkEnableOption "Prometheus";

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

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

  config = mkIf cfg.enable {
    nixfiles.modules.nginx = with cfg; {
      enable = true;
      upstreams.prometheus.servers."127.0.0.1:${toString cfg.port}" = {};
      virtualHosts.${domain}.locations."/" = {
        proxyPass = "http://prometheus";
        extraConfig = ''
          if ($internal != 1) {
            return 403;
          }
        '';
      };
    };

    services.prometheus = with cfg; {
      enable = true;

      listenAddress = "127.0.0.1";
      inherit port;

      extraFlags = [
        "--web.external-url=https://${domain}"
        "--storage.tsdb.retention.size=50GB"
        "--storage.tsdb.retention.time=1y"
        "--storage.tsdb.wal-compression"
      ];
    };
  };
}