|
1 | | -{ lib, stdenv, stdenvNoCC, bun, fzf, ripgrep, makeBinaryWrapper }: |
| 1 | +{ lib, stdenvNoCC, bun, fzf, ripgrep, makeBinaryWrapper }: |
2 | 2 | args: |
3 | 3 | let |
4 | 4 | scripts = args.scripts; |
@@ -28,64 +28,89 @@ stdenvNoCC.mkDerivation (finalAttrs: { |
28 | 28 | makeBinaryWrapper |
29 | 29 | ]; |
30 | 30 |
|
31 | | - configurePhase = '' |
32 | | - runHook preConfigure |
33 | | - cp -R ${finalAttrs.node_modules}/. . |
34 | | - runHook postConfigure |
35 | | - ''; |
36 | | - |
37 | 31 | env.MODELS_DEV_API_JSON = args.modelsDev; |
38 | 32 | env.OPENCODE_VERSION = args.version; |
39 | 33 | env.OPENCODE_CHANNEL = "stable"; |
| 34 | + dontConfigure = true; |
40 | 35 |
|
41 | 36 | buildPhase = '' |
42 | 37 | runHook preBuild |
43 | 38 |
|
44 | | - cp ${scripts + "/bun-build.ts"} bun-build.ts |
| 39 | + cp -r ${finalAttrs.node_modules}/node_modules . |
| 40 | + cp -r ${finalAttrs.node_modules}/packages . |
45 | 41 |
|
46 | | - substituteInPlace bun-build.ts \ |
47 | | - --replace '@VERSION@' "${finalAttrs.version}" |
| 42 | + ( |
| 43 | + cd packages/opencode |
48 | 44 |
|
49 | | - export BUN_COMPILE_TARGET=${args.target} |
50 | | - bun --bun bun-build.ts |
| 45 | + chmod -R u+w ./node_modules |
| 46 | + mkdir -p ./node_modules/@opencode-ai |
| 47 | + rm -f ./node_modules/@opencode-ai/{script,sdk,plugin} |
| 48 | + ln -s $(pwd)/../../packages/script ./node_modules/@opencode-ai/script |
| 49 | + ln -s $(pwd)/../../packages/sdk/js ./node_modules/@opencode-ai/sdk |
| 50 | + ln -s $(pwd)/../../packages/plugin ./node_modules/@opencode-ai/plugin |
| 51 | +
|
| 52 | + cp ${./bundle.ts} ./bundle.ts |
| 53 | + chmod +x ./bundle.ts |
| 54 | + bun run ./bundle.ts |
| 55 | + ) |
51 | 56 |
|
52 | 57 | runHook postBuild |
53 | 58 | ''; |
54 | 59 |
|
55 | | - dontStrip = true; |
56 | | - |
57 | 60 | installPhase = '' |
58 | 61 | runHook preInstall |
59 | 62 |
|
60 | 63 | cd packages/opencode |
61 | | - if [ ! -f opencode ]; then |
62 | | - echo "ERROR: opencode binary not found in $(pwd)" |
63 | | - ls -la |
| 64 | + if [ ! -d dist ]; then |
| 65 | + echo "ERROR: dist directory missing after bundle step" |
64 | 66 | exit 1 |
65 | 67 | fi |
66 | | - if [ ! -f opencode-worker.js ]; then |
67 | | - echo "ERROR: opencode worker bundle not found in $(pwd)" |
68 | | - ls -la |
| 68 | +
|
| 69 | + mkdir -p $out/lib/opencode |
| 70 | + cp -r dist $out/lib/opencode/ |
| 71 | + chmod -R u+w $out/lib/opencode/dist |
| 72 | +
|
| 73 | + # Select bundled worker assets deterministically (sorted find output) |
| 74 | + worker_file=$(find "$out/lib/opencode/dist" -type f \( -path '*/tui/worker.*' -o -name 'worker.*' \) | sort | head -n1) |
| 75 | + parser_worker_file=$(find "$out/lib/opencode/dist" -type f -name 'parser.worker.*' | sort | head -n1) |
| 76 | + if [ -z "$worker_file" ]; then |
| 77 | + echo "ERROR: bundled worker not found" |
69 | 78 | exit 1 |
70 | 79 | fi |
71 | 80 |
|
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 |
| 81 | + main_wasm=$(printf '%s\n' "$out"/lib/opencode/dist/tree-sitter-*.wasm | sort | head -n1) |
| 82 | + wasm_list=$(find "$out/lib/opencode/dist" -maxdepth 1 -name 'tree-sitter-*.wasm' -print) |
| 83 | + for patch_file in "$worker_file" "$parser_worker_file"; do |
| 84 | + [ -z "$patch_file" ] && continue |
| 85 | + [ ! -f "$patch_file" ] && continue |
| 86 | + if [ -n "$wasm_list" ] && grep -q 'tree-sitter' "$patch_file"; then |
| 87 | + # Rewrite wasm references to absolute store paths to avoid runtime resolve failures. |
| 88 | + bun --bun ${scripts + "/patch-wasm.ts"} "$patch_file" "$main_wasm" $wasm_list |
| 89 | + fi |
| 90 | + done |
| 91 | +
|
| 92 | + mkdir -p $out/lib/opencode/node_modules |
| 93 | + cp -r ../../node_modules/.bun $out/lib/opencode/node_modules/ |
| 94 | + mkdir -p $out/lib/opencode/node_modules/@opentui |
| 95 | +
|
| 96 | + mkdir -p $out/bin |
| 97 | + makeWrapper ${bun}/bin/bun $out/bin/opencode \ |
| 98 | + --add-flags "run" \ |
| 99 | + --add-flags "$out/lib/opencode/dist/src/index.js" \ |
| 100 | + --prefix PATH : ${lib.makeBinPath [ fzf ripgrep ]} \ |
| 101 | + --argv0 opencode |
| 102 | +
|
84 | 103 | runHook postInstall |
85 | 104 | ''; |
86 | 105 |
|
87 | | - postFixup = '' |
88 | | - wrapProgram "$out/bin/opencode" --prefix PATH : ${lib.makeBinPath [ fzf ripgrep ]} |
| 106 | + postInstall = '' |
| 107 | + for pkg in $out/lib/opencode/node_modules/.bun/@opentui+core-* $out/lib/opencode/node_modules/.bun/@opentui+solid-* $out/lib/opencode/node_modules/.bun/@opentui+core@* $out/lib/opencode/node_modules/.bun/@opentui+solid@*; do |
| 108 | + if [ -d "$pkg" ]; then |
| 109 | + pkgName=$(basename "$pkg" | sed 's/@opentui+\([^@]*\)@.*/\1/') |
| 110 | + ln -sf ../.bun/$(basename "$pkg")/node_modules/@opentui/$pkgName \ |
| 111 | + $out/lib/opencode/node_modules/@opentui/$pkgName |
| 112 | + fi |
| 113 | + done |
89 | 114 | ''; |
90 | 115 |
|
91 | 116 | meta = { |
|
0 commit comments