Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Nix packages for Grafana plugins and allow declarative installation #107980

Merged
merged 6 commits into from Jan 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion nixos/modules/services/monitoring/grafana.nix
Expand Up @@ -5,10 +5,11 @@ with lib;
let
cfg = config.services.grafana;
opt = options.services.grafana;
declarativePlugins = pkgs.linkFarm "grafana-plugins" (builtins.map (pkg: { name = pkg.pname; path = pkg; }) cfg.declarativePlugins);

envOptions = {
PATHS_DATA = cfg.dataDir;
PATHS_PLUGINS = "${cfg.dataDir}/plugins";
PATHS_PLUGINS = if builtins.isNull cfg.declarativePlugins then "${cfg.dataDir}/plugins" else declarativePlugins;
PATHS_LOGS = "${cfg.dataDir}/log";

SERVER_PROTOCOL = cfg.protocol;
Expand Down Expand Up @@ -260,6 +261,12 @@ in {
defaultText = "pkgs.grafana";
type = types.package;
};
declarativePlugins = 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 = literalExample "with pkgs.grafanaPlugins; [ grafana-piechart-panel ]";
};

dataDir = mkOption {
description = "Data directory.";
Expand Down
14 changes: 13 additions & 1 deletion nixos/tests/grafana.nix
Expand Up @@ -17,6 +17,10 @@ let
};

extraNodeConfs = {
declarativePlugins = {
services.grafana.declarativePlugins = [ pkgs.grafanaPlugins.grafana-clock-panel ];
};

postgresql = {
services.grafana.database = {
host = "127.0.0.1:5432";
Expand Down Expand Up @@ -52,7 +56,7 @@ let
nameValuePair dbName (mkMerge [
baseGrafanaConf
(extraNodeConfs.${dbName} or {})
])) [ "sqlite" "postgresql" "mysql" ]);
])) [ "sqlite" "declarativePlugins" "postgresql" "mysql" ]);

in {
name = "grafana";
Expand All @@ -66,6 +70,14 @@ in {
testScript = ''
start_all()

with subtest("Declarative plugins installed"):
declarativePlugins.wait_for_unit("grafana.service")
declarativePlugins.wait_for_open_port(3000)
declarativePlugins.succeed(
"curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/plugins | grep -q grafana-clock-panel"
)
declarativePlugins.shutdown()

with subtest("Successful API query as admin user with sqlite db"):
sqlite.wait_for_unit("grafana.service")
sqlite.wait_for_open_port(3000)
Expand Down
7 changes: 7 additions & 0 deletions pkgs/servers/monitoring/grafana/plugins/default.nix
@@ -0,0 +1,7 @@
{ newScope, pkgs }:

let
callPackage = newScope (pkgs // plugins);
plugins = import ./plugins.nix { inherit callPackage; };
in
plugins
@@ -0,0 +1,13 @@
{ grafanaPlugin, lib }:

grafanaPlugin rec {
pname = "grafana-clock-panel";
version = "1.1.1";
zipHash = "sha256-SvZyg7r+XG6i7jqYwxpPn6ZzJc7qmtfPtyphYppURDk=";
meta = with lib; {
description = "Clock panel for Grafana";
license = licenses.asl20;
maintainers = with maintainers; [ lukegb ];
platforms = platforms.unix;
};
}
@@ -0,0 +1,13 @@
{ grafanaPlugin, lib }:

grafanaPlugin rec {
pname = "grafana-piechart-panel";
version = "1.6.1";
zipHash = "sha256-64K/efoBKuBFp8Jw79hTdMyTurTZsL0qfgPDcUWz2jg=";
meta = with lib; {
description = "Pie chart panel for Grafana";
license = licenses.asl20;
maintainers = with maintainers; [ lukegb ];
platforms = platforms.unix;
};
}
28 changes: 28 additions & 0 deletions pkgs/servers/monitoring/grafana/plugins/grafana-plugin.nix
@@ -0,0 +1,28 @@
{ stdenvNoCC, fetchurl, unzip }:

{ pname, version, zipHash, meta ? {}, passthru ? {}, ... }@args:
stdenvNoCC.mkDerivation ({
inherit pname version;

src = fetchurl {
name = "${pname}-${version}.zip";
url = "https://grafana.com/api/plugins/${pname}/versions/${version}/download";
hash = zipHash;
};

nativeBuildInputs = [ unzip ];

installPhase = ''
cp -R "." "$out"
chmod -R a-w "$out"
chmod u+w "$out"
'';

passthru = {
updateScript = [ ./update-grafana-plugin.sh pname ];
} // passthru;

meta = {
homepage = "https://grafana.com/grafana/plugins/${pname}";
} // meta;
} // (builtins.removeAttrs args [ "pname" "version" "sha256" "meta" ]))
@@ -0,0 +1,13 @@
{ grafanaPlugin, lib }:

grafanaPlugin rec {
pname = "grafana-polystat-panel";
version = "1.2.2";
zipHash = "sha256-HWQdhstnrDuXPithZ8kOG2ZtSeAT215MJ1ftMCt6/tc=";
meta = with lib; {
description = "Hexagonal multi-stat panel for Grafana";
license = licenses.asl20;
maintainers = with maintainers; [ lukegb ];
platforms = platforms.unix;
};
}
@@ -0,0 +1,13 @@
{ grafanaPlugin, lib }:

grafanaPlugin rec {
pname = "grafana-worldmap-panel";
version = "0.3.2";
zipHash = "sha256-MGAJzS9X91x6wt305jH1chLoW3zd7pIYDwRnPg9qrgE=";
meta = with lib; {
description = "World Map panel for Grafana";
license = licenses.asl20;
maintainers = with maintainers; [ lukegb ];
platforms = platforms.unix;
};
}
11 changes: 11 additions & 0 deletions pkgs/servers/monitoring/grafana/plugins/plugins.nix
@@ -0,0 +1,11 @@
{ callPackage }:
{
inherit callPackage;

grafanaPlugin = callPackage ./grafana-plugin.nix { };

grafana-clock-panel = callPackage ./grafana-clock-panel { };
grafana-piechart-panel = callPackage ./grafana-piechart-panel { };
grafana-polystat-panel = callPackage ./grafana-polystat-panel { };
grafana-worldmap-panel = callPackage ./grafana-worldmap-panel { };
}
@@ -0,0 +1,8 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p curl jq common-updater-scripts

set -eu -o pipefail

readonly plugin_name="$1"
readonly latest_version="$(curl "https://grafana.com/api/plugins/${plugin_name}" | jq -r .version)"
update-source-version "grafanaPlugins.${plugin_name}" "$latest_version"
1 change: 1 addition & 0 deletions pkgs/top-level/all-packages.nix
Expand Up @@ -17164,6 +17164,7 @@ in
gofish = callPackage ../servers/gopher/gofish { };

grafana = callPackage ../servers/monitoring/grafana { };
grafanaPlugins = dontRecurseIntoAttrs (callPackage ../servers/monitoring/grafana/plugins { });

grafana-loki = callPackage ../servers/monitoring/loki { };

Expand Down