diff --git a/nix/services/grafana.nix b/nix/services/grafana.nix index daf4fa80..c89e52e9 100644 --- a/nix/services/grafana.nix +++ b/nix/services/grafana.nix @@ -71,6 +71,17 @@ in ''; }; + declarativePlugins = lib.mkOption { + type = with types; nullOr (listOf path); + default = null; + description = "If non-null, then a list of packages containing Grafana plugins to install. If set, plugins cannot be manually installed."; + example = "with pkgs.grafanaPlugins; [ grafana-piechart-panel ]"; + # Make sure each plugin is added only once; otherwise building + # the link farm fails, since the same path is added multiple + # times. + apply = x: if lib.isList x then lib.unique x else x; + }; + providers = lib.mkOption { type = types.listOf yamlFormat.type; description = '' @@ -129,6 +140,7 @@ in mkdir -p $out/plugins ''; }; + declarativePlugins = pkgs.linkFarm "grafana-plugins" (builtins.map (pkg: { name = pkg.pname; path = pkg; }) config.declarativePlugins); startScript = pkgs.writeShellApplication { name = "start-grafana"; runtimeInputs = @@ -140,6 +152,7 @@ in grafana server --config ${grafanaConfigIni} \ --homepath ${config.package}/share/grafana \ cfg:paths.data="$(readlink -m ${config.dataDir})" \ + ${lib.optionalString (config.declarativePlugins != null) "cfg:paths.plugins=${declarativePlugins}"} \ cfg:paths.provisioning="${provisioningConfig}" ''; }; diff --git a/nix/services/grafana_test.nix b/nix/services/grafana_test.nix index 1b358428..dc12c6ee 100644 --- a/nix/services/grafana_test.nix +++ b/nix/services/grafana_test.nix @@ -62,6 +62,7 @@ in }; } ]; + declarativePlugins = with pkgs.grafanaPlugins; [ grafana-clickhouse-datasource ]; }; settings.processes.test = @@ -71,7 +72,7 @@ in { # Tests based on: https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/grafana/basic.nix command = pkgs.writeShellApplication { - runtimeInputs = [ cfg.package pkgs.gnugrep pkgs.curl pkgs.uutils-coreutils-noprefix ]; + runtimeInputs = [ cfg.package pkgs.gnugrep pkgs.curl pkgs.uutils-coreutils-noprefix pkgs.jq ]; text = '' ADMIN=${cfg.extraConf.security.admin_user} @@ -83,6 +84,8 @@ in # The dashboard provisioner was used to create a dashboard. curl -sSfN -u $ADMIN:$PASSWORD $ROOT_URL/api/dashboards/uid/${dashboardUid} -i curl -sSfN -u $ADMIN:$PASSWORD $ROOT_URL/api/dashboards/uid/${dashboardUid} | grep '"title":"${dashboardTitle}"' + # Test for extra plugins added to grafana. + curl -sSfN -u $ADMIN:$PASSWORD $ROOT_URL/api/plugins | jq '.[] | select(.id == "grafana-clickhouse-datasource") | .enabled' | grep "true" ''; name = "grafana-test"; };