{ lib, stdenv, fetchzip, patchelf, glibc, }: let version = "1.4.0"; srcs = { aarch64-darwin = { url = "https://github.com/anomalyco/opencode/releases/download/v${version}/opencode-darwin-arm64.zip"; sha256 = "0m97j2vln8yhhvnsjl92phx6dac24y7hgh75csmbkbhawkz9xm4l"; }; x86_64-darwin = { url = "https://github.com/anomalyco/opencode/releases/download/v${version}/opencode-darwin-x64.zip"; sha256 = "17n04j06pdc2raxjm91y6p87gwpnra0liabpbjwdmyd1iqgqv0q8"; }; x86_64-linux = { url = "https://github.com/anomalyco/opencode/releases/download/v${version}/opencode-linux-x64.tar.gz"; sha256 = "16117lwfj2lb8wjbq5cyf77vhi52ada5ys3212hjqw3qw3wrcc0r"; }; aarch64-linux = { url = "https://github.com/anomalyco/opencode/releases/download/v${version}/opencode-linux-arm64.tar.gz"; sha256 = "06lvm1qiji74xdd3psqn6lwxak65gqsbmkib1pjb4n65f9246jwm"; }; }; src = srcs.${stdenv.hostPlatform.system} or (throw "Unsupported system: ${stdenv.hostPlatform.system}"); in stdenv.mkDerivation { pname = "opencode"; inherit version; src = fetchzip { inherit (src) url sha256; }; # Bun standalone binaries have JS code appended after the ELF sections # stripping/patching would remove or corrupt this appended data dontStrip = true; dontPatchELF = true; nativeBuildInputs = lib.optionals stdenv.isLinux [ patchelf ]; installPhase = '' runHook preInstall install -Dm755 $src/opencode $out/bin/opencode runHook postInstall ''; # Manually patch the interpreter for bun standalone binaries on Linux postFixup = lib.optionalString stdenv.isLinux '' interpreter="${glibc}/lib/${ if stdenv.hostPlatform.system == "aarch64-linux" then "ld-linux-aarch64.so.1" else "ld-linux-x86-64.so.2" }" patchelf --set-interpreter "$interpreter" $out/bin/opencode ''; meta = with lib; { description = "Terminal-based AI coding assistant"; homepage = "https://opencode.ai"; license = licenses.mit; maintainers = [ ]; platforms = [ "aarch64-darwin" "x86_64-darwin" "x86_64-linux" "aarch64-linux" ]; mainProgram = "opencode"; }; }