2023-09-08 02:37:05 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
2024-08-03 17:32:02 +00:00
|
|
|
namespace,
|
2023-09-08 02:37:05 +00:00
|
|
|
...
|
2024-08-03 17:32:02 +00:00
|
|
|
}: let
|
|
|
|
inherit (lib) mkIf mkEnableOption mkOption types;
|
|
|
|
|
|
|
|
cfg = config.${namespace}.services.grafana;
|
2023-09-08 02:37:05 +00:00
|
|
|
server_settings = config.services.grafana.settings.server;
|
2024-08-03 17:32:02 +00:00
|
|
|
grafana_dashboards = pkgs.${namespace}.teslamate-grafana-dashboards;
|
2023-09-08 02:37:05 +00:00
|
|
|
in {
|
2024-08-03 17:32:02 +00:00
|
|
|
options.${namespace}.services.grafana = {
|
2023-09-08 02:37:05 +00:00
|
|
|
enable = mkEnableOption "grafana";
|
|
|
|
acmeCertName = mkOption {
|
2024-08-03 17:32:02 +00:00
|
|
|
type = types.str;
|
2023-09-08 02:37:05 +00:00
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
If set to a non-empty string, forces SSL with the supplied acme
|
|
|
|
certificate.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
config = mkIf cfg.enable {
|
2024-02-06 04:20:02 +00:00
|
|
|
age.secrets = {
|
|
|
|
teslamate_db_for_grafana = {
|
|
|
|
file = ../../../../secrets/teslamate_db.age;
|
|
|
|
owner = "grafana";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-09-08 02:37:05 +00:00
|
|
|
services.grafana = {
|
|
|
|
enable = true;
|
|
|
|
settings.server = {
|
|
|
|
domain = "grafana.kilonull.com";
|
|
|
|
http_port = 2342;
|
|
|
|
http_addr = "0.0.0.0";
|
|
|
|
};
|
2023-09-10 21:37:51 +00:00
|
|
|
|
|
|
|
provision = {
|
|
|
|
enable = true;
|
|
|
|
datasources = {
|
|
|
|
# This assumes prometheus, loki, and grafana are all running on the
|
|
|
|
# same host.
|
|
|
|
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}";
|
|
|
|
}
|
2024-02-06 04:20:02 +00:00
|
|
|
{
|
|
|
|
name = "TeslaMate";
|
|
|
|
type = "postgres";
|
|
|
|
access = "proxy";
|
|
|
|
url = "localhost:5432";
|
|
|
|
user = "teslamate";
|
|
|
|
database = "teslamate";
|
|
|
|
secureJsonData = {
|
|
|
|
password = "$__file{${config.age.secrets.teslamate_db_for_grafana.path}}";
|
|
|
|
};
|
|
|
|
jsonData = {
|
|
|
|
# TODO: Automate this somehow?
|
|
|
|
postgresVersion = "1400";
|
|
|
|
sslmode = "disable";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
dashboards = {
|
|
|
|
settings.providers = [
|
|
|
|
{
|
|
|
|
name = "teslamate";
|
|
|
|
orgId = 1;
|
|
|
|
folder = "TeslaMate";
|
|
|
|
folderUid = "Nr4ofiDZk";
|
|
|
|
type = "file";
|
|
|
|
disableDeletion = false;
|
|
|
|
editable = true;
|
|
|
|
updateIntervalSeconds = 86400;
|
2024-08-03 17:32:02 +00:00
|
|
|
options.path = "${grafana_dashboards}/dashboards";
|
2024-02-06 04:20:02 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
name = "teslamate_internal";
|
|
|
|
orgId = 1;
|
|
|
|
folder = "TeslaMate/Internal";
|
|
|
|
folderUid = "Nr5ofiDZk";
|
|
|
|
type = "file";
|
|
|
|
disableDeletion = false;
|
|
|
|
editable = true;
|
|
|
|
updateIntervalSeconds = 86400;
|
2024-08-03 17:32:02 +00:00
|
|
|
options.path = "${grafana_dashboards}/dashboards/internal";
|
2024-02-06 04:20:02 +00:00
|
|
|
}
|
2023-09-10 21:37:51 +00:00
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
2023-09-08 02:37:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
services.nginx = {
|
|
|
|
enable = true;
|
2023-11-20 02:35:07 +00:00
|
|
|
virtualHosts."${server_settings.domain}" =
|
2023-09-08 02:37:05 +00:00
|
|
|
{
|
|
|
|
locations."/" = {
|
|
|
|
proxyPass = "http://${server_settings.http_addr}:${toString server_settings.http_port}";
|
|
|
|
proxyWebsockets = true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// lib.optionalAttrs (cfg.acmeCertName != "") {
|
|
|
|
forceSSL = true;
|
|
|
|
useACMEHost = cfg.acmeCertName;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
networking.firewall = {
|
|
|
|
allowedTCPPorts = [80 443];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|