|
16 | 16 | cfg = config.programs.opencode; |
17 | 17 |
|
18 | 18 | jsonFormat = pkgs.formats.json { }; |
| 19 | + |
| 20 | + transformMcpServer = name: server: { |
| 21 | + name = name; |
| 22 | + value = { |
| 23 | + enabled = !(server.disabled or false); |
| 24 | + } |
| 25 | + // ( |
| 26 | + if server ? url then |
| 27 | + { |
| 28 | + type = "remote"; |
| 29 | + url = server.url; |
| 30 | + } |
| 31 | + // (lib.optionalAttrs (server ? headers) { headers = server.headers; }) |
| 32 | + else if server ? command then |
| 33 | + { |
| 34 | + type = "local"; |
| 35 | + command = [ server.command ] ++ (server.args or [ ]); |
| 36 | + } |
| 37 | + // (lib.optionalAttrs (server ? env) { environment = server.env; }) |
| 38 | + else |
| 39 | + { } |
| 40 | + ); |
| 41 | + }; |
| 42 | + |
| 43 | + transformedMcpServers = |
| 44 | + if cfg.enableMcpIntegration && config.programs.mcp.enable && config.programs.mcp.servers != { } then |
| 45 | + lib.listToAttrs (lib.mapAttrsToList transformMcpServer config.programs.mcp.servers) |
| 46 | + else |
| 47 | + { }; |
19 | 48 | in |
20 | 49 | { |
21 | 50 | meta.maintainers = with lib.maintainers; [ delafthi ]; |
|
25 | 54 |
|
26 | 55 | package = mkPackageOption pkgs "opencode" { nullable = true; }; |
27 | 56 |
|
| 57 | + enableMcpIntegration = mkOption { |
| 58 | + type = lib.types.bool; |
| 59 | + default = false; |
| 60 | + description = '' |
| 61 | + Whether to integrate the MCP servers config from |
| 62 | + {option}`programs.mcp.servers` into |
| 63 | + {option}`programs.opencode.settings.mcp`. |
| 64 | +
|
| 65 | + Note: Settings defined in {option}`programs.mcp.servers` are merged |
| 66 | + with {option}`programs.opencode.settings.mcp`, with OpenCode settings |
| 67 | + taking precedence. |
| 68 | + ''; |
| 69 | + }; |
| 70 | + |
28 | 71 | settings = mkOption { |
29 | 72 | inherit (jsonFormat) type; |
30 | 73 | default = { }; |
|
147 | 190 | Custom themes for opencode. The attribute name becomes the theme |
148 | 191 | filename, and the value is either: |
149 | 192 | - An attribute set, that is converted to a json |
150 | | - - A path to a file conaining the content |
| 193 | + - A path to a file containing the content |
151 | 194 | Themes are stored in {file}`$XDG_CONFIG_HOME/opencode/themes/` directory. |
152 | 195 | Set `programs.opencode.settings.theme` to enable the custom theme. |
153 | 196 | See <https://opencode.ai/docs/themes/> for the documentation. |
|
159 | 202 | home.packages = mkIf (cfg.package != null) [ cfg.package ]; |
160 | 203 |
|
161 | 204 | xdg.configFile = { |
162 | | - "opencode/config.json" = mkIf (cfg.settings != { }) { |
163 | | - source = jsonFormat.generate "config.json" ( |
164 | | - { |
165 | | - "$schema" = "https://opencode.ai/config.json"; |
166 | | - } |
167 | | - // cfg.settings |
168 | | - ); |
| 205 | + "opencode/config.json" = mkIf (cfg.settings != { } || transformedMcpServers != { }) { |
| 206 | + source = |
| 207 | + let |
| 208 | + # Merge MCP servers: transformed servers + user settings, with user settings taking precedence |
| 209 | + mergedMcpServers = transformedMcpServers // (cfg.settings.mcp or { }); |
| 210 | + # Merge all settings |
| 211 | + mergedSettings = |
| 212 | + cfg.settings // (lib.optionalAttrs (mergedMcpServers != { }) { mcp = mergedMcpServers; }); |
| 213 | + in |
| 214 | + jsonFormat.generate "config.json" ( |
| 215 | + { |
| 216 | + "$schema" = "https://opencode.ai/config.json"; |
| 217 | + } |
| 218 | + // mergedSettings |
| 219 | + ); |
169 | 220 | }; |
170 | 221 |
|
171 | 222 | "opencode/AGENTS.md" = ( |
|
0 commit comments