From 4bad6610368798d308c4558657b811db33528fea Mon Sep 17 00:00:00 2001
From: Julien Malka <julien@malka.sh>
Date: Sun, 8 Dec 2024 13:23:45 +0100
Subject: [PATCH] feat: add initial setup for declarative uptime-kuma monitors

---
 lib/mkmachine.nix               |  3 ---
 lon.lock                        |  4 ++--
 machines/lambda/uptime-kuma.nix | 21 +++++---------------
 modules/nginx/default.nix       | 34 +++++++++++++++++++++++++++++++++
 4 files changed, 41 insertions(+), 21 deletions(-)

diff --git a/lib/mkmachine.nix b/lib/mkmachine.nix
index c14fb9d..6a7ca4b 100644
--- a/lib/mkmachine.nix
+++ b/lib/mkmachine.nix
@@ -50,9 +50,6 @@ import "${nixpkgs}/nixos/lib/eval-config.nix" {
           });
           # Packages comming from other repositories
           lila-build-hook = (import inputs.lila).packages.${system}.utils;
-          statelessUptimeKuma =
-            pkgs.callPackage "${inputs.stateless-uptime-kuma}/stateless-uptime-kuma.nix"
-              { };
           # My own packages
           keycloak-keywind = prev.pkgs.callPackage ../packages/keycloak-keywind { };
           hydrasect = prev.pkgs.callPackage ../packages/hydrasect { };
diff --git a/lon.lock b/lon.lock
index 4030dca..eaa3b41 100644
--- a/lon.lock
+++ b/lon.lock
@@ -165,9 +165,9 @@
       "type": "Git",
       "fetchType": "git",
       "branch": "truly-deterministic",
-      "revision": "880f444ff7862d6127b051cf1a993ad1585b1652",
+      "revision": "caf3169829647809805caaf968c8c0d4015ef187",
       "url": "https://git.dgnum.eu/Luj/stateless-uptime-kuma.git",
-      "hash": "sha256-l9fgwesnmFxasCaYUCD7L9bGGJXytLuwtx3CZMgpwJg=",
+      "hash": "sha256-tux43mzd1rrlpTEhcQ9PiJBse9+SGEcWY/9F8cxX+Po=",
       "submodules": false
     },
     "unstable": {
diff --git a/machines/lambda/uptime-kuma.nix b/machines/lambda/uptime-kuma.nix
index 56dda23..d2a0823 100644
--- a/machines/lambda/uptime-kuma.nix
+++ b/machines/lambda/uptime-kuma.nix
@@ -1,6 +1,7 @@
 {
   pkgs,
   config,
+  inputs,
   ...
 }:
 {
@@ -23,24 +24,12 @@
   };
 
   age.secrets."stateless-uptime-kuma-password".file = ../../secrets/stateless-uptime-kuma-password.age;
+  nixpkgs.overlays = [
+    (import "${inputs.stateless-uptime-kuma}/overlay.nix")
+  ];
+
   statelessUptimeKuma = {
     enableService = true;
-    probesConfig = {
-      monitors = {
-        "mdr" = {
-          url = "https://82.67.34.230";
-          keyword = "Ulm";
-          type = "keyword";
-          accepted_statuscodes = [ "200-299" ];
-          headers = ''
-            {
-              "Host": "julienmalka.me"
-            }
-          '';
-        };
-      };
-    };
-
     extraFlags = [
       "-s"
       "-v DEBUG"
diff --git a/modules/nginx/default.nix b/modules/nginx/default.nix
index dba183d..498a084 100644
--- a/modules/nginx/default.nix
+++ b/modules/nginx/default.nix
@@ -59,6 +59,39 @@ in
               '';
 
               systemConfig = _: {
+                statelessUptimeKuma.probesConfig.monitors = lib.mkIf (name != "default") {
+                  "${name} - IPv4" = {
+                    url = "https://${
+                      if (hasSuffix "luj" name) then
+                        config.machine.meta.ips.vpn.ipv4
+                      else
+                        config.machine.meta.ips.public.ipv4
+                    }";
+                    type = "http";
+                    accepted_statuscodes = [ "200-299" ];
+                    headers = ''
+                      {
+                        "Host": "${name}"
+                      }
+                    '';
+                  };
+                  "${name} - IPv6" = {
+                    url = "https://[${
+                      if (hasSuffix "luj" name) then
+                        config.machine.meta.ips.vpn.ipv6
+                      else
+                        config.machine.meta.ips.public.ipv6
+                    }]";
+                    type = "http";
+                    accepted_statuscodes = [ "200-299" ];
+                    headers = ''
+                      {
+                        "Host": "${name}"
+                      }
+                    '';
+
+                  };
+                };
                 security.acme.certs = lib.optionalAttrs (hasSuffix "luj" name) {
                   "${name}".server = lib.mkIf (hasSuffix "luj" name) "https://ca.luj/acme/acme/directory";
                 };
@@ -134,6 +167,7 @@ in
     ];
 
     machine = mergeSub (c: c.machine);
+    statelessUptimeKuma = mergeSub (c: c.statelessUptimeKuma);
 
   };
 }