services-flake/nix/open-webui.nix
shivaraj-bh e7eb9dec41 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/tree/master):
https://github.com/NixOS/nixpkgs/pull/316248

---------

Co-authored-by: Pol Dellaiera <pol.dellaiera@protonmail.com>
Co-authored-by: Sridhar Ratnakumar <3998+srid@users.noreply.github.com>
2024-06-14 03:11:07 +05:30

108 lines
3.0 KiB
Nix

# 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}" =
let
setupStateDirs = lib.concatMapStrings
(stateDir:
''
if [ ! -d "''$${stateDir}" ]; then
mkdir -p "''$${stateDir}"
fi
${stateDir}=$(readlink -f "''$${stateDir}")
export ${stateDir}
'') [ "DATA_DIR" "STATIC_DIR" "HF_HOME" "SENTENCE_TRANSFORMERS_HOME" ];
in
{
environment = {
DATA_DIR = config.dataDir;
STATIC_DIR = config.dataDir;
HF_HOME = config.dataDir;
SENTENCE_TRANSFORMERS_HOME = config.dataDir;
} // config.environment;
command = pkgs.writeShellApplication {
name = "open-webui-wrapper";
text = ''
${setupStateDirs}
${lib.getExe config.package} 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;
};
namespace = name;
availability.restart = "on_failure";
};
};
};
};
};
}