{ config, pkgs, ... }:

let
  addr = "127.0.0.1";
  port = 33002;
  domain = "grafana.pleshevski.ru";

in
{
  services.grafana = {
    enable = true;
    package = pkgs.unstable.grafana;
    settings = {
      server = {
        http_addr = addr;
        http_port = port;
        inherit domain;
      };
    };
    provision = {
      enable = true;
      datasources.settings = {
        datasources =
          [
            {
              name = "Prometheus";
              type = "prometheus";
              access = "proxy";
              url = "http://127.0.0.1:${toString config.services.prometheus.port}";
            }
            {
              name = "Loki";
              type = "loki";
              access = "proxy";
              url = "http://127.0.0.1:${toString config.services.loki.configuration.server.http_listen_port}";
            }
          ];
      };
    };
  };

  services.nginx.virtualHosts."${domain}" = {
    enableACME = true;
    forceSSL = true;
    locations."/" = {
      proxyPass = "http://${addr}:${toString port}";
      proxyWebsockets = true;
    };
  };
}