From 5ecb4868cf87d6cd1bed28536b58aa4edff1f503 Mon Sep 17 00:00:00 2001 From: JakobLichterfeld Date: Wed, 6 Aug 2025 08:09:47 +0200 Subject: [PATCH] feat(nix): use datasources.settings.datasources to allow merging Grafana sources from multiple modules, correctly set default Grafana dashboard path and allow disabling default Grafana dashboard with setDefaultDashboard option (#4870) * feat(nix): use datasources.settings.datasources to allow merging Grafana sources from multiple modules * docs: update changelog * style: linter findings * fix(nix): correctly set default Grafana dashboard path * feat(nix): allow disabling default Grafana dashboard with setDefaultDashboard option * fix(nix): avoid type error by configuring Grafana datasources via provision settings * fix(nix): set default_home_dashboard_path under dashboards section in Grafana settings --- CHANGELOG.md | 3 +++ nix/module.nix | 30 ++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12b17bd3..7c4c9b75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ - feat(webview): show offline duration in extended view on mobile as well to improve UX (#4848 - @JakobLichterfeld) - feat: use Grafana 12.1.0 (#4855 - @swiffer) +- feat(nix): use datasources.settings.datasources to allow merging Grafana sources from multiple modules (#4870 - @JakobLichterfeld) +- fix(nix): correctly set default Grafana dashboard path (#4870 - @JakobLichterfeld) +- feat(nix): allow disabling default Grafana dashboard with setDefaultDashboard option (#4870 - @JakobLichterfeld) #### Build, CI, internal diff --git a/nix/module.nix b/nix/module.nix index e120c94d..68dbeb23 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -137,6 +137,12 @@ in default = "/"; description = "Path that grafana is mounted on. Useful if using a reverse proxy to vend teslamate and grafana on the same port"; }; + + setDefaultDashboard = mkOption { + type = types.bool; + default = true; + description = "Whether to set the TeslaMate home dashboard as the default dashboard in Grafana"; + }; }; mqtt = { @@ -262,14 +268,34 @@ in "auth.anonymous".enabled = false; "auth.basic".enabled = false; analytics.reporting_enabled = false; - dashboards.default_home_dashboard_path = "../grafana/dashboards/internal/home.json"; + dashboards.default_home_dashboard_path = mkIf cfg.grafana.setDefaultDashboard "${pkgs.lib.sources.sourceFilesBySuffices ../grafana/dashboards/internal [".json"]}/home.json"; date_formats.use_browser_locale = true; plugins.preinstall_disabled = true; unified_alerting.enabled = false; }; provision = { enable = true; - datasources.path = ../grafana/datasource.yml; + datasources.settings.datasources = [ + # extracted from ../grafana/datasource.yml + { + name = "TeslaMate"; + type = "postgres"; + url = "http://${cfg.postgres.host}:${toString cfg.postgres.port}"; + user = cfg.postgres.user; + access = "proxy"; + basicAuth = false; + withCredentials = false; + isDefault = true; + secureJsonData.password = "\${DATABASE_PASS}"; + jsonData = { + postgresVersion = 1500; + sslmode = "disable"; + database = cfg.postgres.database; + }; + version = 1; + editable = true; + } + ]; # Need to duplicate dashboards.yml since it contains absolute paths # which are incompatible with NixOS dashboards.settings = {