From 8b570d4923ee00ff870ec54644a14138b278a34b Mon Sep 17 00:00:00 2001
From: Florian Sesser <florian@private.storage>
Date: Thu, 3 Feb 2022 19:35:04 +0000
Subject: [PATCH] Use mkIf cfg.enable pattern

Should be pure refactoring
---
 morph/lib/base.nix                            |  2 +
 morph/lib/issuer.nix                          |  1 -
 morph/lib/monitoring.nix                      |  1 -
 morph/lib/storage.nix                         |  2 -
 nixos/modules/default.nix                     |  1 +
 .../modules/monitoring/exporters/promtail.nix | 53 +++++++++++--------
 6 files changed, 33 insertions(+), 27 deletions(-)

diff --git a/morph/lib/base.nix b/morph/lib/base.nix
index 88056642..fd0e21c9 100644
--- a/morph/lib/base.nix
+++ b/morph/lib/base.nix
@@ -62,6 +62,8 @@
       "172.23.23.1" = [ "monitoring" "monitoring.monitoringvpn" ];
     };
 
+    services.private-storage.monitoring.exporters.promtail.enable = true;
+
     assertions = [
       # This is a check to save somebody in the future trying to debug why
       # setting `nixpkgs.config` is not having an effect.
diff --git a/morph/lib/issuer.nix b/morph/lib/issuer.nix
index e791ef34..69b0527c 100644
--- a/morph/lib/issuer.nix
+++ b/morph/lib/issuer.nix
@@ -8,7 +8,6 @@ in {
   imports = [
     ../../nixos/modules/monitoring/vpn/client.nix
     ../../nixos/modules/monitoring/exporters/node.nix
-    ../../nixos/modules/monitoring/exporters/promtail.nix
   ];
 
   options.grid.issuer = {
diff --git a/morph/lib/monitoring.nix b/morph/lib/monitoring.nix
index 9eb73825..c955f09b 100644
--- a/morph/lib/monitoring.nix
+++ b/morph/lib/monitoring.nix
@@ -33,7 +33,6 @@ in {
     ../../nixos/modules/monitoring/server/loki.nix
     ../../nixos/modules/monitoring/exporters/node.nix
     ../../nixos/modules/monitoring/exporters/blackbox.nix
-    ../../nixos/modules/monitoring/exporters/promtail.nix
   ];
 
   options.grid.monitoring = {
diff --git a/morph/lib/storage.nix b/morph/lib/storage.nix
index 32da92ec..71e3c223 100644
--- a/morph/lib/storage.nix
+++ b/morph/lib/storage.nix
@@ -15,8 +15,6 @@ in {
     ../../nixos/modules/monitoring/exporters/node.nix
     # Collect Tahoe OpenMetrics statistics.
     ../../nixos/modules/monitoring/exporters/tahoe.nix
-    # Send logs to central logging server.
-    ../../nixos/modules/monitoring/exporters/promtail.nix
   ];
 
   options.grid.storage = {
diff --git a/nixos/modules/default.nix b/nixos/modules/default.nix
index 1772d399..f7e247f9 100644
--- a/nixos/modules/default.nix
+++ b/nixos/modules/default.nix
@@ -12,5 +12,6 @@
   imports = [
     ./packages.nix
     ./issuer.nix
+    ./monitoring/exporters/promtail.nix
   ];
 }
diff --git a/nixos/modules/monitoring/exporters/promtail.nix b/nixos/modules/monitoring/exporters/promtail.nix
index 6668f180..c056ebeb 100644
--- a/nixos/modules/monitoring/exporters/promtail.nix
+++ b/nixos/modules/monitoring/exporters/promtail.nix
@@ -6,37 +6,44 @@
 #              exporters, but it is very similar in what it is doing -
 #              preparing local data and sending it off to a TSDB.
 
-{ config, ... }:
+{ config, options, lib, ... }:
 
 let
+  cfg = config.services.private-storage.monitoring.exporters.promtail;
   hostName = config.networking.hostName;
 
 in {
-  config.services.promtail.enable = true;
-  config.networking.firewall.interfaces.monitoringvpn.allowedTCPPorts = [ 9080 ];
-  config.services.promtail.configuration = {
-    server = {
-      http_listen_port = 9080; # Using /metrics for health check
-      grpc_listen_address = "127.0.0.1"; # unused, but no option to turn it off.
-      grpc_listen_port = 9094; # unused, but no option to turn it off.
-    };
+  options.services.private-storage.monitoring.exporters.promtail = {
+    enable = lib.mkEnableOption "Promtail log exporter service";
+  };
 
-    clients = [{
-        url = "http://monitoring:3100/loki/api/v1/push";
-    }];
+  config = lib.mkIf cfg.enable {
+    services.promtail.enable = true;
+    networking.firewall.interfaces.monitoringvpn.allowedTCPPorts = [ 9080 ];
+    services.promtail.configuration = {
+      server = {
+        http_listen_port = 9080; # Using /metrics for health check
+        grpc_listen_address = "127.0.0.1"; # unused, but no option to turn it off.
+        grpc_listen_port = 9094; # unused, but no option to turn it off.
+      };
 
-    scrape_configs = [{
-      job_name = "systemd-journal";
-      journal = {
-        labels = {
-          job = "systemd-journal";
-          host = hostName;
+      clients = [{
+          url = "http://monitoring:3100/loki/api/v1/push";
+      }];
+
+      scrape_configs = [{
+        job_name = "systemd-journal";
+        journal = {
+          labels = {
+            job = "systemd-journal";
+            host = hostName;
+          };
         };
-      };
-      relabel_configs = [{
-        source_labels = [ "__journal__systemd_unit" ];
-        target_label = "unit";
+        relabel_configs = [{
+          source_labels = [ "__journal__systemd_unit" ];
+          target_label = "unit";
+        }];
       }];
-    }];
+    };
   };
 }
-- 
GitLab