|
1 | 1 | { |
2 | 2 | config, |
3 | 3 | inputs, |
| 4 | + lib, |
4 | 5 | pkgs, |
5 | 6 | ... |
6 | | -}: { |
| 7 | +}: let |
| 8 | + port = 8888; |
| 9 | + baseUrl = "http://127.0.0.1:${toString port}"; |
| 10 | + |
| 11 | + # Shared model defaults |
| 12 | + defaultCapabilities = { |
| 13 | + file_context = true; |
| 14 | + vision = true; |
| 15 | + file_upload = true; |
| 16 | + web_search = true; |
| 17 | + image_generation = true; |
| 18 | + code_interpreter = true; |
| 19 | + citations = true; |
| 20 | + status_updates = true; |
| 21 | + builtin_tools = true; |
| 22 | + }; |
| 23 | + |
| 24 | + defaultBuiltinTools = { |
| 25 | + time = true; |
| 26 | + memory = true; |
| 27 | + chats = true; |
| 28 | + notes = true; |
| 29 | + knowledge = true; |
| 30 | + channels = true; |
| 31 | + web_search = true; |
| 32 | + image_generation = true; |
| 33 | + code_interpreter = true; |
| 34 | + }; |
| 35 | + |
| 36 | + defaultMeta = { |
| 37 | + capabilities = defaultCapabilities; |
| 38 | + toolIds = ["server:mcp:glyph"]; |
| 39 | + defaultFeatureIds = ["web_search" "code_interpreter"]; |
| 40 | + builtinTools = defaultBuiltinTools; |
| 41 | + }; |
| 42 | + |
| 43 | + # Active models — listed models are enabled with full config. |
| 44 | + # All other models from the API provider are deactivated automatically. |
| 45 | + models = { |
| 46 | + "claude-sonnet-4-6" = {}; |
| 47 | + "claude-opus-4-6" = {}; |
| 48 | + "claude-haiku-4-5-20251001" = {}; |
| 49 | + }; |
| 50 | + |
| 51 | + modelIds = builtins.toJSON (builtins.attrNames models); |
| 52 | +in { |
7 | 53 | age.secrets.open-webui-env.file = ./../secrets/open-webui-env.age; |
| 54 | + age.secrets.open-webui-api-key = { |
| 55 | + file = ./../secrets/open-webui-api-key.age; |
| 56 | + mode = "440"; |
| 57 | + }; |
8 | 58 |
|
9 | 59 | systemd.services.open-webui.restartTriggers = [config.age.secrets.open-webui-env.file]; |
10 | 60 |
|
| 61 | + # Sync model configuration after open-webui starts |
| 62 | + systemd.timers.open-webui-model-sync = { |
| 63 | + description = "Trigger Open WebUI model sync"; |
| 64 | + wantedBy = ["timers.target"]; |
| 65 | + restartTriggers = [(builtins.hashString "sha256" (builtins.toJSON models))]; |
| 66 | + timerConfig.OnActiveSec = "10s"; |
| 67 | + }; |
| 68 | + |
| 69 | + systemd.services.open-webui-model-sync = { |
| 70 | + description = "Sync model configuration with Open WebUI"; |
| 71 | + after = ["open-webui.service"]; |
| 72 | + requires = ["open-webui.service"]; |
| 73 | + restartIfChanged = false; |
| 74 | + path = [pkgs.curl pkgs.jq]; |
| 75 | + script = let |
| 76 | + mkModelForm = id: attrs: |
| 77 | + builtins.toJSON { |
| 78 | + inherit id; |
| 79 | + is_active = true; |
| 80 | + name = attrs.name or id; |
| 81 | + meta = defaultMeta // (attrs.meta or {}); |
| 82 | + params = attrs.params or {}; |
| 83 | + }; |
| 84 | + |
| 85 | + mkModelUpdate = id: attrs: let |
| 86 | + form = mkModelForm id attrs; |
| 87 | + in '' |
| 88 | + update_model "${id}" '${form}' & |
| 89 | + ''; |
| 90 | + in '' |
| 91 | + API_KEY=$(cat ${config.age.secrets.open-webui-api-key.path}) |
| 92 | + ACTIVE_IDS='${modelIds}' |
| 93 | +
|
| 94 | + update_model() { |
| 95 | + local id=$1 form=$2 |
| 96 | + echo "Configuring $id..." |
| 97 | + http_code=$(curl -s -o /dev/null -w '%{http_code}' -X POST \ |
| 98 | + -H "Authorization: Bearer $API_KEY" \ |
| 99 | + -H "Content-Type: application/json" \ |
| 100 | + -d "$form" \ |
| 101 | + "${baseUrl}/api/v1/models/model/update") |
| 102 | +
|
| 103 | + if [ "$http_code" = "200" ]; then |
| 104 | + echo "$id: updated." |
| 105 | + else |
| 106 | + echo "ERROR: failed to update $id (HTTP $http_code)" |
| 107 | + fi |
| 108 | + } |
| 109 | +
|
| 110 | + # Wait for open-webui to be ready |
| 111 | + for i in $(seq 1 30); do |
| 112 | + if curl -sf "${baseUrl}/api/models" -H "Authorization: Bearer $API_KEY" >/dev/null 2>&1; then |
| 113 | + break |
| 114 | + fi |
| 115 | + echo "Waiting for Open WebUI (attempt $i/30)..." |
| 116 | + sleep 2 |
| 117 | + done |
| 118 | +
|
| 119 | + # Activate and configure listed models |
| 120 | + ${lib.concatStringsSep "\n" (lib.mapAttrsToList mkModelUpdate models)} |
| 121 | + wait |
| 122 | +
|
| 123 | + # Deactivate all unlisted models that are currently active |
| 124 | + curl -sf -H "Authorization: Bearer $API_KEY" \ |
| 125 | + "${baseUrl}/api/v1/models/list" \ |
| 126 | + | jq -r '.data[] | select(.is_active == true) | .id' \ |
| 127 | + | while read -r id; do |
| 128 | + if ! echo "$ACTIVE_IDS" | jq -e --arg id "$id" 'index($id)' >/dev/null 2>&1; then |
| 129 | + curl -sf -X POST -H "Authorization: Bearer $API_KEY" \ |
| 130 | + "${baseUrl}/api/v1/models/model/toggle?id=$id" >/dev/null 2>&1 |
| 131 | + echo "$id: deactivated." |
| 132 | + fi |
| 133 | + done |
| 134 | + ''; |
| 135 | + }; |
| 136 | + |
11 | 137 | services.open-webui = { |
12 | 138 | enable = true; |
13 | 139 | package = pkgs.open-webui.overridePythonAttrs (old: { |
14 | 140 | dependencies = old.dependencies ++ old.optional-dependencies.postgres; |
15 | 141 | }); |
16 | | - port = 8888; |
| 142 | + inherit port; |
17 | 143 | host = "0.0.0.0"; |
18 | 144 | environmentFile = config.age.secrets.open-webui-env.path; |
19 | 145 | environment = { |
|
0 commit comments