From 4b0e0f5adccfce88547f28ead631d2788f1885b3 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Mon, 31 Aug 2026 22:32:05 +0200 Subject: [PATCH] opencode: use production channel OpenCode only recognizes `dev`, `beta`, `prod`, and `latest` as channel values. Unknown values fall back to `dev`, while `latest` maps to `prod`. The selected channel controls channel-gated behavior, e.g. workspaces, is exposed as a `dev` marker in `opencode web`, and affects the database path. Nixpkgs currently sets `OPENCODE_CHANNEL = "stable"`, so the package unintentionally uses development-channel behavior. It also causes OpenCode to use the nixpkgs-specific `opencode-stable.db` database path. Change the channel to `prod`, which uses the upstream production behavior and canonical `opencode.db` path. To avoid making existing sessions appear lost after upgrading, switch from `makeBinaryWrapper` to `makeWrapper` and add a temporary wrapper workaround. When `opencode-stable.db` exists and `opencode.db` does not, the wrapper continues using the legacy database and points the user at migration instructions. Explicit `OPENCODE_DB` configuration takes precedence, and the workaround can be disabled with `NIXPKGS_OPENCODE_DISABLE_LEGACY_DB_WORKAROUND=1`. The wrapper does not rename or modify the database. Relevant upstream code: - https://github.com/anomalyco/opencode/blob/v1.18.25/packages/app/vite.js#L8-L13 - https://github.com/anomalyco/opencode/blob/v1.18.25/packages/core/src/database/database.ts --- pkgs/by-name/op/opencode/package.nix | 40 +++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/pkgs/by-name/op/opencode/package.nix b/pkgs/by-name/op/opencode/package.nix index 9798bb86d5a4..5c22b285ff18 100644 --- a/pkgs/by-name/op/opencode/package.nix +++ b/pkgs/by-name/op/opencode/package.nix @@ -4,7 +4,7 @@ bun, darwin, fetchFromGitHub, - makeBinaryWrapper, + makeWrapper, models-dev, nodejs, nix-update-script, @@ -116,7 +116,7 @@ stdenv.mkDerivation (finalAttrs: { bun nodejs installShellFiles - makeBinaryWrapper + makeWrapper writableTmpDirAsHomeHook ] ++ lib.optionals stdenv.hostPlatform.isDarwin [ @@ -136,7 +136,7 @@ stdenv.mkDerivation (finalAttrs: { env.MODELS_DEV_API_JSON = "${models-dev}/dist/_api.json"; env.OPENCODE_DISABLE_MODELS_FETCH = true; env.OPENCODE_VERSION = finalAttrs.version; - env.OPENCODE_CHANNEL = "stable"; + env.OPENCODE_CHANNEL = "prod"; buildPhase = '' runHook preBuild @@ -166,7 +166,39 @@ stdenv.mkDerivation (finalAttrs: { ] ) } \ - --set OPENCODE_DISABLE_AUTOUPDATE true + --set OPENCODE_DISABLE_AUTOUPDATE true \ + --run ' + # NOTE: Once this workaround is removed we should switch back to using + # makeBinaryWrapper here. + + # nixpkgs previously built OpenCode with OPENCODE_CHANNEL=stable. "stable" + # is no longer an upstream channel, so it caused OpenCode to store its database + # as opencode-stable.db. After switching to the upstream production channel, + # OpenCode would normally use opencode.db instead, making existing sessions + # appear to be lost. Keep using the legacy database until the user migrates + # it, unless they explicitly configured OPENCODE_DB or disabled this workaround. + + data_home="''${XDG_DATA_HOME:-$HOME/.local/share}" + legacy="$data_home/opencode/opencode-stable.db" + canonical="$data_home/opencode/opencode.db" + + if [ -z "''${OPENCODE_DB:-}" ] \ + && [ -z "''${NIXPKGS_OPENCODE_DISABLE_LEGACY_DB_WORKAROUND:-}" ] \ + && [ -e "$legacy" ] \ + && [ ! -e "$canonical" ]; then + export OPENCODE_DB="opencode-stable.db" + + # Only show migration guidance when stderr is attached to a terminal. + # Non-interactive uses such as `opencode web`, services, or scripts + # should continue starting normally with the legacy database selected. + if [ -t 2 ]; then + echo "Detected legacy nixpkgs OpenCode database at $legacy." >&2 + echo "Continuing to use it for compatibility." >&2 + echo "See https://github.com/NixOS/nixpkgs/pull/558549 for migration instructions." >&2 + echo "Set NIXPKGS_OPENCODE_DISABLE_LEGACY_DB_WORKAROUND=1 to disable this workaround." >&2 + fi + fi + ' install -Dm644 ${models-dev.jsonschema} $out/share/model-schema.json install -Dm644 config.json $out/share/config.json