summary refs log tree commit diff
path: root/nixos/modules/services
diff options
context:
space:
mode:
authorMaximilian Bosch <maximilian@mbosch.me>2022-11-14 11:44:08 +0100
committerMaximilian Bosch <maximilian@mbosch.me>2022-11-20 18:21:41 +0100
commit9d7e9c5965c72f516285fe52af14ceeefb315fdc (patch)
tree79320a788918a5683e4580592d329b4ab0ae952c /nixos/modules/services
parent2f1dfb0db379c1bf13785a5c7d94ad31bd0eb46b (diff)
downloadnixpkgs-9d7e9c5965c72f516285fe52af14ceeefb315fdc.tar
nixpkgs-9d7e9c5965c72f516285fe52af14ceeefb315fdc.tar.gz
nixpkgs-9d7e9c5965c72f516285fe52af14ceeefb315fdc.tar.bz2
nixpkgs-9d7e9c5965c72f516285fe52af14ceeefb315fdc.tar.lz
nixpkgs-9d7e9c5965c72f516285fe52af14ceeefb315fdc.tar.xz
nixpkgs-9d7e9c5965c72f516285fe52af14ceeefb315fdc.tar.zst
nixpkgs-9d7e9c5965c72f516285fe52af14ceeefb315fdc.zip
nixos/grafana: allow using both directories or single YAML files for non-Nix provisioning
Diffstat (limited to 'nixos/modules/services')
-rw-r--r--nixos/modules/services/monitoring/grafana.nix68
1 files changed, 36 insertions, 32 deletions
diff --git a/nixos/modules/services/monitoring/grafana.nix b/nixos/modules/services/monitoring/grafana.nix
index 8035abfc004..d8dc4d1124f 100644
--- a/nixos/modules/services/monitoring/grafana.nix
+++ b/nixos/modules/services/monitoring/grafana.nix
@@ -25,42 +25,51 @@ let
             ${attr} = [];
           });
 
-  datasourceFile = mkProvisionCfg "datasource" "datasources" cfg.provision.datasources;
-  dashboardFile = mkProvisionCfg "dashboard" "providers" cfg.provision.dashboards;
+  datasourceFileOrDir = mkProvisionCfg "datasource" "datasources" cfg.provision.datasources;
+  dashboardFileOrDir = mkProvisionCfg "dashboard" "providers" cfg.provision.dashboards;
 
   notifierConfiguration = {
     apiVersion = 1;
     notifiers = cfg.provision.notifiers;
   };
 
-  notifierFile = pkgs.writeText "notifier.yaml" (builtins.toJSON notifierConfiguration);
+  notifierFileOrDir = pkgs.writeText "notifier.yaml" (builtins.toJSON notifierConfiguration);
 
   generateAlertingProvisioningYaml = x: if (cfg.provision.alerting."${x}".path == null)
                                         then provisioningSettingsFormat.generate "${x}.yaml" cfg.provision.alerting."${x}".settings
                                         else cfg.provision.alerting."${x}".path;
-  rulesFile = generateAlertingProvisioningYaml "rules";
-  contactPointsFile = generateAlertingProvisioningYaml "contactPoints";
-  policiesFile = generateAlertingProvisioningYaml "policies";
-  templatesFile = generateAlertingProvisioningYaml "templates";
-  muteTimingsFile = generateAlertingProvisioningYaml "muteTimings";
-
-  provisionConfDir =  pkgs.runCommand "grafana-provisioning" { } ''
+  rulesFileOrDir = generateAlertingProvisioningYaml "rules";
+  contactPointsFileOrDir = generateAlertingProvisioningYaml "contactPoints";
+  policiesFileOrDir = generateAlertingProvisioningYaml "policies";
+  templatesFileOrDir = generateAlertingProvisioningYaml "templates";
+  muteTimingsFileOrDir = generateAlertingProvisioningYaml "muteTimings";
+
+  ln = { src, dir, filename }: ''
+    if [[ -d "${src}" ]]; then
+      pushd $out/${dir} &>/dev/null
+        lndir "${src}"
+      popd &>/dev/null
+    else
+      ln -sf ${src} $out/${dir}/${filename}.yaml
+    fi
+  '';
+  provisionConfDir = pkgs.runCommand "grafana-provisioning" { nativeBuildInputs = [ pkgs.xorg.lndir ]; } ''
     mkdir -p $out/{datasources,dashboards,notifiers,alerting}
-    ln -sf ${datasourceFile} $out/datasources/datasource.yaml
-    ln -sf ${dashboardFile} $out/dashboards/dashboard.yaml
-    ln -sf ${notifierFile} $out/notifiers/notifier.yaml
-    ln -sf ${rulesFile} $out/alerting/rules.yaml
-    ln -sf ${contactPointsFile} $out/alerting/contactPoints.yaml
-    ln -sf ${policiesFile} $out/alerting/policies.yaml
-    ln -sf ${templatesFile} $out/alerting/templates.yaml
-    ln -sf ${muteTimingsFile} $out/alerting/muteTimings.yaml
+    ${ln { src = datasourceFileOrDir;    dir = "datasources"; filename = "datasource"; }}
+    ${ln { src = dashboardFileOrDir;     dir = "dashboards";  filename = "dashbaord"; }}
+    ${ln { src = notifierFileOrDir;      dir = "notifiers";   filename = "notifier"; }}
+    ${ln { src = rulesFileOrDir;         dir = "alerting";    filename = "rules"; }}
+    ${ln { src = contactPointsFileOrDir; dir = "alerting";    filename = "contactPoints"; }}
+    ${ln { src = policiesFileOrDir;      dir = "alerting";    filename = "policies"; }}
+    ${ln { src = templatesFileOrDir;     dir = "alerting";    filename = "templates"; }}
+    ${ln { src = muteTimingsFileOrDir;   dir = "alerting";    filename = "muteTimings"; }}
   '';
 
   # Get a submodule without any embedded metadata:
   _filter = x: filterAttrs (k: v: k != "_module") x;
 
   # FIXME(@Ma27) remove before 23.05. This is just a helper-type
-  # because `mkRenamedOptionModule` doesn't work if `foo.bar` is renamed
+  # because `mkRenamedOptionMthere's there's odule` doesn't work if `foo.bar` is renamed
   # to `foo.bar.baz`.
   submodule' = module: types.coercedTo
     (mkOptionType {
@@ -342,19 +351,7 @@ in {
                 Don't change the value of this option if you are planning to use `services.grafana.provision` options.
               '';
               default = provisionConfDir;
-              defaultText = literalExpression ''
-                pkgs.runCommand "grafana-provisioning" { } \'\'
-                  mkdir -p $out/{datasources,dashboards,notifiers,alerting}
-                  ln -sf ''${datasourceFile} $out/datasources/datasource.yaml
-                  ln -sf ''${dashboardFile} $out/dashboards/dashboard.yaml
-                  ln -sf ''${notifierFile} $out/notifiers/notifier.yaml
-                  ln -sf ''${rulesFile} $out/alerting/rules.yaml
-                  ln -sf ''${contactPointsFile} $out/alerting/contactPoints.yaml
-                  ln -sf ''${policiesFile} $out/alerting/policies.yaml
-                  ln -sf ''${templatesFile} $out/alerting/templates.yaml
-                  ln -sf ''${muteTimingsFile} $out/alerting/muteTimings.yaml
-                  \'\'
-              '';
+              defaultText = "directory with all provisioning files linked together";
               type = types.path;
             };
           };
@@ -641,6 +638,7 @@ in {
             description = lib.mdDoc ''
               Path to YAML datasource configuration. Can't be used with
               [](#opt-services.grafana.provision.datasources.settings) simultaneously.
+              Can be either a directory or a single YAML file. Will end up in the store.
             '';
             default = null;
             type = types.nullOr types.path;
@@ -692,6 +690,7 @@ in {
             description = lib.mdDoc ''
               Path to YAML dashboard configuration. Can't be used with
               [](#opt-services.grafana.provision.dashboards.settings) simultaneously.
+              Can be either a directory or a single YAML file. Will end up in the store.
             '';
             default = null;
             type = types.nullOr types.path;
@@ -714,6 +713,7 @@ in {
             description = lib.mdDoc ''
               Path to YAML rules configuration. Can't be used with
               [](#opt-services.grafana.provision.alerting.rules.settings) simultaneously.
+              Can be either a directory or a single YAML file. Will end up in the store.
             '';
             default = null;
             type = types.nullOr types.path;
@@ -837,6 +837,7 @@ in {
             description = lib.mdDoc ''
               Path to YAML contact points configuration. Can't be used with
               [](#opt-services.grafana.provision.alerting.contactPoints.settings) simultaneously.
+              Can be either a directory or a single YAML file. Will end up in the store.
             '';
             default = null;
             type = types.nullOr types.path;
@@ -917,6 +918,7 @@ in {
             description = lib.mdDoc ''
               Path to YAML notification policies configuration. Can't be used with
               [](#opt-services.grafana.provision.alerting.policies.settings) simultaneously.
+              Can be either a directory or a single YAML file. Will end up in the store.
             '';
             default = null;
             type = types.nullOr types.path;
@@ -986,6 +988,7 @@ in {
             description = lib.mdDoc ''
               Path to YAML templates configuration. Can't be used with
               [](#opt-services.grafana.provision.alerting.templates.settings) simultaneously.
+              Can be either a directory or a single YAML file. Will end up in the store.
             '';
             default = null;
             type = types.nullOr types.path;
@@ -1067,6 +1070,7 @@ in {
             description = lib.mdDoc ''
               Path to YAML mute timings configuration. Can't be used with
               [](#opt-services.grafana.provision.alerting.muteTimings.settings) simultaneously.
+              Can be either a directory or a single YAML file. Will end up in the store.
             '';
             default = null;
             type = types.nullOr types.path;