about summary refs log tree commit diff
path: root/modules/postgresql.nix
diff options
context:
space:
mode:
Diffstat (limited to 'modules/postgresql.nix')
-rw-r--r--modules/postgresql.nix61
1 files changed, 39 insertions, 22 deletions
diff --git a/modules/postgresql.nix b/modules/postgresql.nix
index d5b712c..211d078 100644
--- a/modules/postgresql.nix
+++ b/modules/postgresql.nix
@@ -69,31 +69,48 @@ in
 
       prometheus.exporters.postgres = {
         enable = true;
-        listenAddress = lib.mkDefault this.wireguard.ipv4.address;
-        port = lib.mkDefault 9187;
+        listenAddress = "127.0.0.1";
+        port = 9187;
       };
     };
 
-    systemd.services.postgresql.postStart = lib.optionalString (
-      cfg.extraPostStart != [ ]
-    ) lib.concatLines cfg.extraPostStart;
-
-    environment.variables.PSQLRC = toString (
-      pkgs.writeText "psqlrc" ''
-        \set QUIET 1
-
-        \timing
-        \x auto
-        \pset null '[NULL]'
-        \set PROMPT1 '%[%033[1m%]%M %n@%/%R%[%033[0m%]% λ '
-        \set PROMPT2 '    … > '
-        \set VERBOSITY verbose
-        \set HISTCONTROL ignoredups
-        \set HISTFILE /dev/null
-
-        \unset QUIET
-      ''
-    );
+    systemd.services = {
+      postgresql.postStart =
+        lib.optionalString (cfg.extraPostStart != [ ]) cfg.extraPostStart |> lib.concatLines;
+
+      alloy.reloadTriggers = [ config.environment.etc."alloy/postgres.alloy".source ];
+    };
+
+    environment = {
+      etc."alloy/postgres.alloy".text = with config.services.prometheus.exporters.postgres; ''
+        prometheus.scrape "postgres" {
+          targets = [
+            {
+              __address__ = "${listenAddress}:${toString port}",
+              instance    = "${config.networking.hostName}",
+            },
+          ]
+          forward_to = [prometheus.relabel.default.receiver]
+        }
+      '';
+
+      variables.PSQLRC = toString (
+        pkgs.writeText "psqlrc" ''
+          \set QUIET 1
+
+          \timing
+          \x auto
+          \pset null '[NULL]'
+          \set PROMPT1 '%[%033[1m%]%M %n@%/%R%[%033[0m%]% λ '
+          \set PROMPT2 '    … > '
+          \set VERBOSITY verbose
+          \set HISTCONTROL ignoredups
+          \set HISTFILE /dev/null
+
+          \unset QUIET
+        ''
+      );
+    };
 
     topology = {
       nodes.${this.hostname}.services.postgresql = {

Consider giving Nix/NixOS a try! <3