refactor grafana config

This commit is contained in:
Henry Hiles 2026-10-12 15:12:16 -04:00
commit dcaaa2d910
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs
3 changed files with 237 additions and 224 deletions

View file

@ -1,224 +0,0 @@
{
config,
pkgs,
lib,
...
}:
let
secretName = "grafanaSecret";
passwordName = "grafanaPassword";
credentialDirectory = "/run/credentials/grafana.service/";
in
{
systemd.services.grafana.serviceConfig.LoadCredential = [
"${secretName}:${config.age.secrets."grafanaSecret.age".path}"
"${passwordName}:${config.age.secrets."grafanaPassword.age".path}"
];
services =
let
domain = "status.federated.nexus";
in
{
grafana = {
enable = true;
declarativePlugins = with pkgs.grafanaPlugins; [
prometheus
];
settings = {
server = {
inherit domain;
root_url = "https://${domain}";
protocol = "socket";
};
security = {
cookie_secure = true;
secret_key = "$__file{${credentialDirectory}${secretName}}";
admin_user = "quadradical";
admin_password = "$__file{${credentialDirectory}${passwordName}}";
};
"auth.anonymous".enabled = true;
analytics.feedback_links_enabled = false;
users.default_theme = "system";
dashboards.default_home_dashboard_path = toString (
pkgs.writers.writeJSON "status.json" {
title = "Service Status";
refresh = "10s";
time = rec {
from = "now";
to = from;
};
panels =
let
status = null;
offset = if status == null then 8 else 0;
in
(lib.optionals (status != null) [
{
gridPos.w = 100;
title = "Status Update";
type = "text";
options.content = status;
}
])
++ [
{
collapsed = false;
title = "Federated Nexus Service Statuses";
type = "row";
gridPos = {
h = 8;
w = 24;
y = offset;
};
}
]
++ (lib.imap0
(
index:
{ name, service }:
{
title = name;
type = "stat";
gridPos = rec {
h = 3;
w = 4;
x = index * w;
y = (index * h) + offset + 8;
};
datasource = {
type = "prometheus";
uid = "prometheus";
};
options.graphMode = "none";
fieldConfig = {
defaults = {
color.mode = "thresholds";
mappings = [
{
options = {
"0".text = "Failed";
"1".text = "Running";
};
type = "value";
}
];
thresholds = {
mode = "absolute";
steps = [
{
color = "red";
value = 0;
}
{
color = "green";
value = 1;
}
];
};
};
};
targets = [
{
expr = "node_systemd_unit_state{name=\"${service}\",state=\"active\"}";
}
];
}
)
[
{
name = "Matrix";
service = "continuwuity.service";
}
{
name = "LaSuite Docs";
service = "lasuite-docs.service";
}
{
name = "Forgejo (Git)";
service = "forgejo.service";
}
{
name = "Degoog (Search)";
service = "degoog.service";
}
{
name = "Redlib";
service = "redlib.service";
}
]
);
}
);
};
provision = {
enable = true;
datasources.settings.datasources = [
{
name = "Prometheus";
type = "prometheus";
uid = "prometheus";
url = with config.services.prometheus; "http://${listenAddress}:${toString port}";
jsonData.timeInterval = config.services.prometheus.globalConfig.scrape_interval;
}
];
dashboards.settings.providers = [
{
name = "Node Exporter";
options.path = pkgs.fetchurl {
name = "dashboard-node-exporter-full.json";
url = "https://grafana.com/api/dashboards/1860/revisions/42/download";
hash = "sha256-pNgn6xgZBEu6LW0lc0cXX2gRkQ8lg/rer34SPE3yEl4=";
};
}
{
name = "Unbound Info";
options.path = pkgs.fetchurl {
name = "unbound-info.json";
url = "https://github.com/rfmoz/grafana-dashboards/raw/refs/heads/master/prometheus/unbound-full.json";
hash = "sha256-6bM0mqm1nF7xadc0ojNWIxJXCHio5db97NjENHIpwdI=";
};
}
{
name = "Continuwuity Stats";
options.path = pkgs.fetchurl {
name = "continuwuity-stats.json";
url = "https://continuwuity.org/guides/grafana-dashboard.json";
hash = "sha256-1s7vFEaZMiqH9h//BdrBwtWuYiqlxzUmYwdpMPYyuMo=";
};
}
{
name = "Caddy Hosts";
options.path = pkgs.stdenv.mkDerivation {
pname = "caddy-hosts-json";
version = "1.0.0";
dontUnpack = true;
src = pkgs.fetchurl {
name = "caddy-hosts.json";
url = "https://grafana.com/api/dashboards/24146/revisions/1/download";
hash = "sha256-vw/O3bNKQ0tJwhsZKYvkMU6jeqkG+sfCtA7YT0vp3YY=";
};
installPhase = ''
mkdir -p $out
sed 's|''${DS_PROMETHEUS}|prometheus|g' $src > $out/caddy-exporter.json
'';
};
}
];
};
};
caddy.virtualHosts."${domain}".extraConfig =
"reverse_proxy unix/${config.services.grafana.settings.server.socket}";
};
users.users.caddy.extraGroups = [ "grafana" ];
}

View file

@ -0,0 +1,115 @@
{
config,
pkgs,
...
}:
let
secretName = "grafanaSecret";
passwordName = "grafanaPassword";
credentialDirectory = "/run/credentials/grafana.service/";
in
{
systemd.services.grafana.serviceConfig.LoadCredential = [
"${secretName}:${config.age.secrets."grafanaSecret.age".path}"
"${passwordName}:${config.age.secrets."grafanaPassword.age".path}"
];
services =
let
domain = "status.federated.nexus";
in
{
grafana = {
enable = true;
declarativePlugins = with pkgs.grafanaPlugins; [
prometheus
];
settings = {
unified_alerting.enabled = false;
server = {
inherit domain;
root_url = "https://${domain}";
protocol = "socket";
};
security = {
cookie_secure = true;
secret_key = "$__file{${credentialDirectory}${secretName}}";
admin_user = "quadradical";
admin_password = "$__file{${credentialDirectory}${passwordName}}";
};
"auth.anonymous".enabled = true;
analytics.feedback_links_enabled = false;
users.default_theme = "system";
};
provision = {
enable = true;
datasources.settings.datasources = [
{
name = "Prometheus";
type = "prometheus";
uid = "prometheus";
url = with config.services.prometheus; "http://${listenAddress}:${toString port}";
jsonData.timeInterval = config.services.prometheus.globalConfig.scrape_interval;
}
];
dashboards.settings.providers = [
{
name = "Node Exporter";
options.path = pkgs.fetchurl {
name = "dashboard-node-exporter-full.json";
url = "https://grafana.com/api/dashboards/1860/revisions/42/download";
hash = "sha256-pNgn6xgZBEu6LW0lc0cXX2gRkQ8lg/rer34SPE3yEl4=";
};
}
{
name = "Unbound Info";
options.path = pkgs.fetchurl {
name = "unbound-info.json";
url = "https://github.com/rfmoz/grafana-dashboards/raw/refs/heads/master/prometheus/unbound-full.json";
hash = "sha256-6bM0mqm1nF7xadc0ojNWIxJXCHio5db97NjENHIpwdI=";
};
}
{
name = "Continuwuity Stats";
options.path = pkgs.fetchurl {
name = "continuwuity-stats.json";
url = "https://continuwuity.org/guides/grafana-dashboard.json";
hash = "sha256-1s7vFEaZMiqH9h//BdrBwtWuYiqlxzUmYwdpMPYyuMo=";
};
}
{
name = "Caddy Hosts";
options.path = pkgs.stdenv.mkDerivation {
pname = "caddy-hosts-json";
version = "1.0.0";
dontUnpack = true;
src = pkgs.fetchurl {
name = "caddy-hosts.json";
url = "https://grafana.com/api/dashboards/24146/revisions/1/download";
hash = "sha256-vw/O3bNKQ0tJwhsZKYvkMU6jeqkG+sfCtA7YT0vp3YY=";
};
installPhase = ''
mkdir -p $out
sed 's|''${DS_PROMETHEUS}|prometheus|g' $src > $out/caddy-exporter.json
'';
};
}
];
};
};
caddy.virtualHosts."${domain}".extraConfig =
"reverse_proxy unix/${config.services.grafana.settings.server.socket}";
};
users.users.caddy.extraGroups = [ "grafana" ];
}

View file

@ -0,0 +1,122 @@
{ pkgs, ... }:
{
services.grafana.settings.dashboards.default_home_dashboard_path = toString (
pkgs.writers.writeJSON "status.json" {
title = "Federated Nexus Status";
uid = "federated-nexus-status";
editable = true;
refresh = "30s";
schemaVersion = 39;
version = 1;
time = {
from = "now-7d";
to = "now";
};
panels = [
{
id = 1;
title = "Service uptime";
type = "state-timeline";
datasource = {
type = "prometheus";
uid = "prometheus";
};
gridPos = {
x = 0;
y = 0;
w = 24;
h = 12;
};
targets = [
{
refId = "A";
datasource = {
type = "prometheus";
uid = "prometheus";
};
expr = ''
node_systemd_unit_state{
job="node",
name=~"continuwuity\\.service|lasuite-docs\\.service|forgejo\\.service|degoog\\.service|redlib\\.service",
state="active"
}
'';
instant = false;
range = true;
format = "time_series";
interval = "5m";
legendFormat = "{{name}}";
}
];
fieldConfig = {
defaults = {
min = 0;
max = 1;
color = {
mode = "thresholds";
};
thresholds = {
mode = "absolute";
steps = [
{
value = null;
color = "red";
}
{
value = 0.5;
color = "green";
}
];
};
mappings = [
{
type = "value";
options = {
"0" = {
text = "DOWN";
color = "red";
};
"1" = {
text = "UP";
color = "green";
};
};
}
];
};
overrides = [ ];
};
options = {
mergeValues = true;
showValue = "never";
alignValue = "center";
rowHeight = 0.8;
tooltip = {
mode = "single";
sort = "none";
};
legend = {
showLegend = false;
};
};
}
];
}
);
}