From 399b858c0225f6a8ae0434225ce1d747244e2a8f Mon Sep 17 00:00:00 2001 From: shivaraj-bh Date: Tue, 11 Jun 2024 10:39:10 +0530 Subject: [PATCH] feat(open-webui): init ported from: https://github.com/shivaraj-bh/ollama-flake/blob/main/services/open-webui.nix This was also recently upstreamed to nixpkgs: https://github.com/NixOS/nixpkgs/pull/316248 --- nix/default.nix | 1 + nix/open-webui.nix | 95 +++++++++++++++++++++++++++++++++++++++++ nix/open-webui_test.nix | 21 +++++++++ test/flake.nix | 1 + 4 files changed, 118 insertions(+) create mode 100644 nix/open-webui.nix create mode 100644 nix/open-webui_test.nix diff --git a/nix/default.nix b/nix/default.nix index a1fc194f..389de905 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -10,6 +10,7 @@ in ./mysql ./nginx ./postgres + ./open-webui.nix ./redis-cluster.nix ./redis.nix ./zookeeper.nix diff --git a/nix/open-webui.nix b/nix/open-webui.nix new file mode 100644 index 00000000..15ff31a7 --- /dev/null +++ b/nix/open-webui.nix @@ -0,0 +1,95 @@ +# Based on: https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/misc/open-webui.nix +{ pkgs, lib, name, config, ... }: +let + inherit (lib) types; +in +{ + options = { + enable = lib.mkEnableOption "Open-WebUI server"; + package = lib.mkPackageOption pkgs "open-webui" { }; + + dataDir = lib.mkOption { + type = types.str; + default = "./data/${name}"; + description = "The Open-WebUI data directory"; + }; + + host = lib.mkOption { + type = types.str; + default = "127.0.0.1"; + example = "0.0.0.0"; + description = '' + The host address which the Open-WebUI server HTTP interface listens to. + ''; + }; + + port = lib.mkOption { + type = types.port; + default = 1111; + example = 11111; + description = '' + Which port the Open-WebUI server listens to. + ''; + }; + + environment = lib.mkOption { + type = types.attrsOf types.str; + default = { + SCARF_NO_ANALYTICS = "True"; + DO_NOT_TRACK = "True"; + ANONYMIZED_TELEMETRY = "False"; + }; + example = '' + { + OLLAMA_API_BASE_URL = "http://127.0.0.1:11434"; + # Disable authentication + WEBUI_AUTH = "False"; + } + ''; + description = "Extra environment variables for Open-WebUI"; + }; + + outputs.settings = lib.mkOption { + type = types.deferredModule; + internal = true; + readOnly = true; + default = { + processes = { + "${name}" = { + inherit (config) environment; + command = pkgs.writeShellApplication { + name = "open-webui-wrapper"; + runtimeInputs = [ pkgs.open-webui ]; + text = '' + set -x + if [ ! -d ${config.dataDir} ]; then + mkdir -p ${config.dataDir} + fi + + DATA_DIR=$(readlink -f ${config.dataDir}) + STATIC_DIR=$DATA_DIR + HF_HOME=$DATA_DIR + SENTENCE_TRANSFORMERS_HOME=$DATA_DIR + + export DATA_DIR STATIC_DIR HF_HOME SENTENCE_TRANSFORMERS_HOME + + open-webui serve --host ${config.host} --port ${builtins.toString config.port} + ''; + }; + readiness_probe = { + http_get = { + host = config.host; + port = config.port; + }; + initial_delay_seconds = 2; + period_seconds = 10; + timeout_seconds = 4; + success_threshold = 1; + failure_threshold = 5; + }; + }; + }; + }; + }; + }; +} diff --git a/nix/open-webui_test.nix b/nix/open-webui_test.nix new file mode 100644 index 00000000..692b788a --- /dev/null +++ b/nix/open-webui_test.nix @@ -0,0 +1,21 @@ +{ pkgs, ... }: { + services.open-webui."open-webui1" = { + enable = true; + environment = { + # Requires network connection + RAG_EMBEDDING_MODEL = ""; + }; + }; + + settings.processes.test = { + command = pkgs.writeShellApplication { + runtimeInputs = [ pkgs.curl ]; + text = '' + # Avoid printing the entire HTML page on the stdout, we just want to know if the page is active. + curl http://127.0.0.1:1111 > /dev/null + ''; + name = "open-webui-test"; + }; + depends_on."open-webui1".condition = "process_healthy"; + }; +} diff --git a/test/flake.nix b/test/flake.nix index 40e7c531..552987c1 100644 --- a/test/flake.nix +++ b/test/flake.nix @@ -38,6 +38,7 @@ "${inputs.services-flake}/nix/elasticsearch_test.nix" "${inputs.services-flake}/nix/mysql/mysql_test.nix" "${inputs.services-flake}/nix/nginx/nginx_test.nix" + "${inputs.services-flake}/nix/open-webui_test.nix" "${inputs.services-flake}/nix/postgres/postgres_test.nix" "${inputs.services-flake}/nix/redis_test.nix" "${inputs.services-flake}/nix/redis-cluster_test.nix"