From 28a0c514b1b4e30dad6883876d9f75bb1a659468 Mon Sep 17 00:00:00 2001 From: pazpi Date: Tue, 7 Jan 2025 22:32:35 +0100 Subject: [PATCH] Setup Perplexica (requires manual set up of docker image) --- hosts/caddy/default.nix | 5 ++- modules/services/searx.nix | 62 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/hosts/caddy/default.nix b/hosts/caddy/default.nix index 5e114b0..15db5c9 100644 --- a/hosts/caddy/default.nix +++ b/hosts/caddy/default.nix @@ -56,6 +56,8 @@ in searx = { enable = true; secretFile = config.age.secrets.searx-secret.path; + enableAI = true; + perplexicaUrl = "portainer.internal"; proxy = { enable = true; domain = tsDomain; @@ -107,9 +109,6 @@ in cloudflareApiKeyFile = config.age.secrets.cloudflare-tegola-apiKey.path; } ]; - # email = "pasettodavide@gmail.com"; - # domain = tsDomain; - # claudflareApiKeyFile = config.age.secrets.cloudflare-tegola-apiKey.path; }; }; diff --git a/modules/services/searx.nix b/modules/services/searx.nix index b4234c8..eae8310 100644 --- a/modules/services/searx.nix +++ b/modules/services/searx.nix @@ -20,6 +20,22 @@ in ''; }; + enableAI = lib.mkOption { + default = false; + type = lib.types.bool; + description = '' + Enable alternative search engine with AI. Requires [Perplexica](https://github.com/ItzCrazyKns/Perplexica) + ''; + }; + + perplexicaUrl = lib.mkOption { + default = ""; + type = lib.types.str; + description = '' + URL to the Perplexica instance. It assumes that the Perplexica instance is running on port 3000 and the API on port 3001 + ''; + }; + proxy = { enable = lib.mkEnableOption "Set the proxy entry for this service"; @@ -31,6 +47,22 @@ in ''; }; + subdomain = lib.mkOption { + default = "search"; + type = lib.types.str; + description = '' + Search subdomain + ''; + }; + + perplexicaSubdomain = lib.mkOption { + default = "sai"; + type = lib.types.str; + description = '' + Perplexica subdomain + ''; + }; + host = lib.mkOption { default = "localhost"; type = lib.types.str; @@ -55,11 +87,19 @@ in }; server = { - base_url = "https://search.${cfg.proxy.domain}"; - bind_address = "::1"; + base_url = with cfg.proxy; "https://${subdomain}.${domain}"; + bind_address = "0.0.0.0"; port = 8080; secret_key = "@SEARX_SECRET_KEY@"; }; + + search = { + formats = [ + "html" + "json" + ]; + }; + }; }; @@ -69,10 +109,26 @@ in (lib.mkIf cfg.proxy.enable { services.caddy = with cfg.proxy; { - virtualHosts."search.${domain}".extraConfig = '' + virtualHosts."${subdomain}.${domain}".extraConfig = '' reverse_proxy http://${host}:8080 import cloudflare_${domain} ''; + + virtualHosts."${perplexicaSubdomain}.${domain}".extraConfig = lib.mkIf cfg.enableAI '' + @websockets { + header Connection *Upgrade* + header Upgrade websocket + } + reverse_proxy ${cfg.perplexicaUrl}:3000 + reverse_proxy /api* ${cfg.perplexicaUrl}:3001 + reverse_proxy @websockets { + header_up Host ${cfg.perplexicaUrl} + to ${cfg.perplexicaUrl}:3001 + } + + import cloudflare_${domain} + ''; + }; }) ];