{ autoPatchelfHook, fetchurl, lib, makeBinaryWrapper, ripgrep, stdenv, stdenvNoCC, }: let version = "2.0.26"; artifacts = { "aarch64-darwin" = { artifact = "cli-darwin-arm64"; hash = "sha512-XklldeO6eWgG8vkPNLcdlBPEm1Y+/tyhEXGZvXbk8uytpPP7SEVV2R9oZ98NMSSPH9kBAO/+Xo4sKfvD6CHAsw=="; }; "x86_64-darwin" = { artifact = "cli-darwin-x64-baseline"; hash = "sha512-A4t57iwRo8gh4bKX/tyn/zTmVCdPZMPLNsoJeR+TbskpC1idpjc7qZsdYRPIBBTgzK++LjWhe9vyxqWOFglCXQ=="; }; "aarch64-linux" = { artifact = "cli-linux-arm64"; hash = "sha512-IG316I8wVqndohzFAoWNeH83eMA8sKQFRtMeuk2Wfg4DU1haCSgzAJz3/gbytjwh6bXdWR7V3FCSrfjoraKDgw=="; }; "x86_64-linux" = { artifact = "cli-linux-x64"; hash = "sha512-UIA2/1Ik8HaN54C4xp7H+OHgRfq995XUixrX+kLmFs8EPn1fAcmn3OoiN+3c3Vc3jEV/LHmj0Cy1SL6nW4wdGw=="; }; }; inherit (artifacts.${stdenvNoCC.hostPlatform.system}) artifact hash; wrapperArgs = [ "--prefix" "PATH" ":" (lib.makeBinPath [ ripgrep ]) ] ++ lib.optionals stdenv.hostPlatform.isLinux [ "--prefix" "LD_LIBRARY_PATH" ":" (lib.makeLibraryPath [ stdenv.cc.cc.lib ]) ] ++ [ "--set" "OPENCODE_DISABLE_AUTOUPDATE" "true" ]; in stdenvNoCC.mkDerivation { pname = "opencode-v2"; inherit version; src = fetchurl { url = "https://registry.npmjs.org/@opencode/${artifact}/-/${artifact}-${version}.tgz"; inherit hash; }; sourceRoot = "package"; nativeBuildInputs = [ makeBinaryWrapper ] ++ lib.optionals stdenv.hostPlatform.isLinux [ autoPatchelfHook ]; buildInputs = lib.optionals stdenv.hostPlatform.isLinux [ stdenv.cc.cc.lib ]; dontBuild = true; # Bun standalone executables store their application payload after the ELF # image. Stripping the binary discards that payload and leaves a plain Bun # runtime that only prints Bun's help. dontStrip = true; doInstallCheck = stdenv.buildPlatform.canExecute stdenv.hostPlatform; installPhase = '' runHook preInstall install -Dm755 bin/opencode $out/bin/opencode wrapProgram $out/bin/opencode ${lib.escapeShellArgs wrapperArgs} runHook postInstall ''; installCheckPhase = '' export HOME="$TMPDIR" $out/bin/opencode --help > help.txt grep -q 'opencode' help.txt $out/bin/opencode serve --stdio > serve.txt grep -q '"url"' serve.txt if grep -q 'Bun is a fast JavaScript runtime' help.txt; then echo 'opencode payload was removed from the Bun standalone executable' >&2 exit 1 fi ''; meta = { description = "OpenCode 2.0"; homepage = "https://opencode.ai"; license = lib.licenses.mit; mainProgram = "opencode"; maintainers = [ { email = "noah@packetlost.dev"; github = "chiefnoah"; githubId = 3588683; name = "Noah Pederson"; } ]; platforms = builtins.attrNames artifacts; }; }