|
| 1 | +{ lib, stdenv, stdenvNoCC, bun, fzf, ripgrep, makeBinaryWrapper }: |
| 2 | +args: |
| 3 | +let |
| 4 | + scripts = args.scripts; |
| 5 | + mkModules = |
| 6 | + attrs: |
| 7 | + args.mkNodeModules ( |
| 8 | + attrs |
| 9 | + // { |
| 10 | + canonicalizeScript = scripts + "/canonicalize-node-modules.ts"; |
| 11 | + normalizeBinsScript = scripts + "/normalize-bun-binaries.ts"; |
| 12 | + } |
| 13 | + ); |
| 14 | +in |
| 15 | +stdenvNoCC.mkDerivation (finalAttrs: { |
| 16 | + pname = "opencode"; |
| 17 | + version = args.version; |
| 18 | + |
| 19 | + src = args.src; |
| 20 | + |
| 21 | + node_modules = mkModules { |
| 22 | + version = finalAttrs.version; |
| 23 | + src = finalAttrs.src; |
| 24 | + }; |
| 25 | + |
| 26 | + nativeBuildInputs = [ |
| 27 | + bun |
| 28 | + makeBinaryWrapper |
| 29 | + ]; |
| 30 | + |
| 31 | + configurePhase = '' |
| 32 | + runHook preConfigure |
| 33 | + cp -R ${finalAttrs.node_modules}/. . |
| 34 | + runHook postConfigure |
| 35 | + ''; |
| 36 | + |
| 37 | + env.MODELS_DEV_API_JSON = args.modelsDev; |
| 38 | + env.OPENCODE_VERSION = args.version; |
| 39 | + env.OPENCODE_CHANNEL = "stable"; |
| 40 | + |
| 41 | + buildPhase = '' |
| 42 | + runHook preBuild |
| 43 | +
|
| 44 | + cp ${scripts + "/bun-build.ts"} bun-build.ts |
| 45 | +
|
| 46 | + substituteInPlace bun-build.ts \ |
| 47 | + --replace '@VERSION@' "${finalAttrs.version}" |
| 48 | +
|
| 49 | + export BUN_COMPILE_TARGET=${args.target} |
| 50 | + bun --bun bun-build.ts |
| 51 | +
|
| 52 | + runHook postBuild |
| 53 | + ''; |
| 54 | + |
| 55 | + dontStrip = true; |
| 56 | + |
| 57 | + installPhase = '' |
| 58 | + runHook preInstall |
| 59 | +
|
| 60 | + cd packages/opencode |
| 61 | + if [ ! -f opencode ]; then |
| 62 | + echo "ERROR: opencode binary not found in $(pwd)" |
| 63 | + ls -la |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | + if [ ! -f opencode-worker.js ]; then |
| 67 | + echo "ERROR: opencode worker bundle not found in $(pwd)" |
| 68 | + ls -la |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | +
|
| 72 | + install -Dm755 opencode $out/bin/opencode |
| 73 | + install -Dm644 opencode-worker.js $out/bin/opencode-worker.js |
| 74 | + if [ -f opencode-assets.manifest ]; then |
| 75 | + while IFS= read -r asset; do |
| 76 | + [ -z "$asset" ] && continue |
| 77 | + if [ ! -f "$asset" ]; then |
| 78 | + echo "ERROR: referenced asset \"$asset\" missing" |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | + install -Dm644 "$asset" "$out/bin/$(basename "$asset")" |
| 82 | + done < opencode-assets.manifest |
| 83 | + fi |
| 84 | + runHook postInstall |
| 85 | + ''; |
| 86 | + |
| 87 | + postFixup = '' |
| 88 | + wrapProgram "$out/bin/opencode" --prefix PATH : ${lib.makeBinPath [ fzf ripgrep ]} |
| 89 | + ''; |
| 90 | + |
| 91 | + meta = { |
| 92 | + description = "AI coding agent built for the terminal"; |
| 93 | + longDescription = '' |
| 94 | + OpenCode is a terminal-based agent that can build anything. |
| 95 | + It combines a TypeScript/JavaScript core with a Go-based TUI |
| 96 | + to provide an interactive AI coding experience. |
| 97 | + ''; |
| 98 | + homepage = "https://github.com/sst/opencode"; |
| 99 | + license = lib.licenses.mit; |
| 100 | + platforms = [ |
| 101 | + "aarch64-linux" |
| 102 | + "x86_64-linux" |
| 103 | + "aarch64-darwin" |
| 104 | + "x86_64-darwin" |
| 105 | + ]; |
| 106 | + mainProgram = "opencode"; |
| 107 | + }; |
| 108 | +}) |
0 commit comments