diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..c1549aa --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,69 @@ +{ lib }: +rec { + mkFqdn = _: cfg: cfg.networking.fqdn; + + fromHive = + { + builder, + nodes, + excludes ? [ ], + }: + lib.mkMerge ( + builtins.map (node: builder node nodes.${node}) ( + lib.subtractLists excludes (builtins.attrNames nodes) + ) + ); + + pingProbesFromHive = + { + mkHost, + nodes, + prefix ? "Ping ", + excludes ? [ ], + tags ? [ ], + }: + fromHive { + builder = ( + node: module: { + monitors = { + ${prefix + node} = { + type = "ping"; + inherit tags; + hostname = mkHost node module.config; + }; + }; + } + ); + inherit nodes excludes; + }; + + httpProbesFromConfig = + { + config, + excludes ? [ ], + prefix ? "", + type ? "keyword", + tags ? [ ], + }: + let + filter = k: v: !builtins.elem k excludes && v.globalRedirect == null; + in + { + monitors = lib.mapAttrs' ( + vhostName: vhost: + let + hasSSL = vhost.onlySSL || vhost.enableSSL || vhost.addSSL || vhost.forceSSL; + serverName = if vhost.serverName != null then vhost.serverName else vhostName; + in + { + name = prefix + serverName; + value = { + inherit type; + inherit tags; + url = "http${lib.optionalString hasSSL "s"}://${serverName}"; + method = "get"; + }; + } + ) (lib.filterAttrs filter config.services.nginx.virtualHosts); + }; +} diff --git a/nixos/module.nix b/nixos/module.nix index 7c4e43b..eef273c 100644 --- a/nixos/module.nix +++ b/nixos/module.nix @@ -22,6 +22,7 @@ in Extra arguments to use for executing `stateless-uptime-kuma`. ''; }; + lib = lib.mkOption { type = lib.types.raw; }; probesConfig = { monitors = lib.mkOption { inherit (probesFormat) type; @@ -38,6 +39,7 @@ in }; }; config.statelessUptimeKuma = { + lib = import ../lib { inherit lib; }; build = { json = probesFormat.generate "probes.json" cfg.probesConfig; script = pkgs.writeShellApplication {