zaphyra's git: nixfiles

zaphyra and void's nixfiles

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 
{
  inputs,
  povSelf,
  hostConfig,
  config,
  lib,
  dnsNix,
  ...
}:

let
  inherit (lib) types;
  cfg = lib.getAttrFromPath povSelf config;

in
{

  options = {
    enable = {
      type = types.bool;
      default = false;
    };
    subdomain = {
      type = types.str;
      default = "prometheus.infra";
    };
    domain = {
      type = types.str;
      default = "zaphyra.eu";
    };
  };

  config = lib.mkIf cfg.enable {
    dns.zones."${cfg.domain}".subdomains."${cfg.subdomain}".CNAME = [ "${config.networking.fqdn}." ];

    modules.filesystem.impermanence.system.dirs = [
      {
        directory = "/var/lib/prometheus2";
        mode = "0700";
        user = "prometheus";
        group = "prometheus";
      }
    ];

    services.prometheus = {
      enable = true;
      webExternalUrl = "https://${cfg.subdomain}.${cfg.domain}/";
      listenAddress = "[::1]";
      port = 9090;
      scrapeConfigs = [
        {
          job_name = "node-exporter";
          scrape_interval = "30s";
          scheme = "https";
          metrics_path = "/node-exporter";
          static_configs = [
            {
              targets = (
                lib.mapAttrsToList (
                  name: host:
                  lib.mkIf (
                    host.config.services.prometheus.exporters.node.enable == true
                    && host.config.networking.hostName != ""
                    && host.config.networking.domain != ""
                  ) host.config.networking.fqdn
                ) inputs.self.zaphyraHosts
              );
            }
          ];
        }
        {
          job_name = "systemd-exporter";
          scrape_interval = "30s";
          scheme = "https";
          metrics_path = "/systemd-exporter";
          static_configs = [
            {
              targets = (
                lib.mapAttrsToList (
                  name: host:
                  lib.mkIf (
                    host.config.services.prometheus.exporters.node.enable == true
                    && host.config.networking.hostName != ""
                    && host.config.networking.domain != ""
                  ) host.config.networking.fqdn
                ) inputs.self.zaphyraHosts
              );
            }
          ];
        }
      ];
    };

    services.nginx.virtualHosts."${cfg.subdomain}.${cfg.domain}" = {
      useACMEHost = "${config.networking.fqdn}";
      forceSSL = true;
      kTLS = true;
      locations."/".proxyPass = "http://[::1]:${toString config.services.prometheus.port}/";
    };
  };

}