From 317803eb5a33acdcfe8e1945ee6d74ecd1feda52 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Wed, 4 Jun 2025 15:44:27 +0100 Subject: [PATCH] refactor: Restructure LLM module options and vhost creation --- modules/services/llm/default.nix | 101 +++++++++++++++++++------------ 1 file changed, 61 insertions(+), 40 deletions(-) diff --git a/modules/services/llm/default.nix b/modules/services/llm/default.nix index 7ff5025..c29d8c0 100644 --- a/modules/services/llm/default.nix +++ b/modules/services/llm/default.nix @@ -1,29 +1,28 @@ { config, lib, pkgs, ... }: let - inherit (lib) mkIf; + inherit (lib) mkIf mkEnableOption mkPackageOption mkOption types optionalAttrs; cfg = config.pepe.services.llm; in { - options.pepe.services.llm = with lib; { - enable = mkEnableOption "Enable LLM services (Ollama and Open WebUI)"; + options.pepe.services.llm = { + enable = mkEnableOption "Enable LLM backend service (Ollama)"; + package = mkPackageOption pkgs "ollama" { }; - uiPackage = mkPackageOption pkgs "open-webui" { }; - tikaPackage = mkPackageOption pkgs "tika" { }; + backendDomain = mkOption { - type = types.str; - default = null; - }; - frontendDomain = mkOption { - type = types.str; + type = types.nullOr types.str; default = null; + description = "Domain for Ollama backend. If null, no vhost is created for the backend."; }; + acceleration = mkOption { type = types.enum [ "none" "cuda" "rocm" ]; default = "none"; description = "Acceleration type for Ollama"; }; + environmentVariables = mkOption { type = types.attrsOf types.str; default = { @@ -33,54 +32,76 @@ in }; description = "Environment variables for Ollama"; }; + + frontend = { + enable = mkEnableOption "Enable LLM frontend service (Open WebUI)"; + uiPackage = mkPackageOption pkgs "open-webui" { }; + tikaPackage = mkPackageOption pkgs "tika" { }; # Tika for document processing with Open WebUI + domain = mkOption { + type = types.nullOr types.str; + default = null; + description = "Domain for Open WebUI frontend. If null, no vhost is created for the frontend."; + }; + }; }; - config = mkIf cfg.enable { - environment.systemPackages = [ cfg.package ]; + config = mkIf cfg.enable + { + environment.systemPackages = [ cfg.package ]; - services = { - ollama = { + services.ollama = { enable = true; package = cfg.package; acceleration = cfg.acceleration; environmentVariables = cfg.environmentVariables; }; + pepe.core.vhost.hosts = optionalAttrs (cfg.backendDomain != null) { + "${cfg.backendDomain}" = { + locations."/" = { + host = config.services.ollama.host; + port = config.services.ollama.port; + allowLAN = true; + allowVPN = true; + allowWAN = true; + recommendedProxySettings = false; + extraConfig = '' + proxy_buffering off; + proxy_read_timeout 600s; # Ollama can take time to respond + proxy_set_header Host localhost:${toString config.services.ollama.port}; + ''; + }; + }; + }; + } + // mkIf (cfg.enable && cfg.frontend.enable) { + environment.systemPackages = [ + cfg.frontend.uiPackage + cfg.frontend.tikaPackage + ]; + + services = { open-webui = { enable = true; - package = cfg.uiPackage; + package = cfg.frontend.uiPackage; }; tika = { enable = true; - package = cfg.tikaPackage; + package = cfg.frontend.tikaPackage; }; }; - pepe.core.vhost.hosts.${cfg.backendDomain} = { - locations."/" = { - host = config.services.ollama.host; - port = config.services.ollama.port; - allowLAN = true; - allowVPN = true; - allowWAN = true; - recommendedProxySettings = false; - extraConfig = '' - proxy_buffering off; - proxy_read_timeout 600s; - proxy_set_header Host localhost:${toString config.services.ollama.port}; - ''; - }; - }; - - pepe.core.vhost.hosts.${cfg.frontendDomain} = { - locations."/" = { - host = config.services.open-webui.host; - port = config.services.open-webui.port; - allowLAN = true; - allowVPN = true; - allowWAN = true; - proxyWebsockets = true; + pepe.core.vhost.hosts = optionalAttrs (cfg.frontend.domain != null) { + "${cfg.frontend.domain}" = { + locations."/" = { + host = config.services.open-webui.host; + port = config.services.open-webui.port; + allowLAN = true; + allowVPN = true; + allowWAN = true; + proxyWebsockets = true; + }; }; }; };