|
| 1 | +{ |
| 2 | + lib, |
| 3 | + stdenv, |
| 4 | + rustPlatform, |
| 5 | + bun, |
| 6 | + pkg-config, |
| 7 | + dbus ? null, |
| 8 | + openssl, |
| 9 | + glib ? null, |
| 10 | + gtk3 ? null, |
| 11 | + libsoup_3 ? null, |
| 12 | + webkitgtk_4_1 ? null, |
| 13 | + librsvg ? null, |
| 14 | + libappindicator-gtk3 ? null, |
| 15 | + cargo, |
| 16 | + rustc, |
| 17 | + makeBinaryWrapper, |
| 18 | + nodejs, |
| 19 | + jq, |
| 20 | +}: |
| 21 | +args: |
| 22 | +let |
| 23 | + scripts = args.scripts; |
| 24 | + mkModules = |
| 25 | + attrs: |
| 26 | + args.mkNodeModules ( |
| 27 | + attrs |
| 28 | + // { |
| 29 | + canonicalizeScript = scripts + "/canonicalize-node-modules.ts"; |
| 30 | + normalizeBinsScript = scripts + "/normalize-bun-binaries.ts"; |
| 31 | + } |
| 32 | + ); |
| 33 | +in |
| 34 | +rustPlatform.buildRustPackage rec { |
| 35 | + pname = "opencode-desktop"; |
| 36 | + version = args.version; |
| 37 | + |
| 38 | + src = args.src; |
| 39 | + |
| 40 | + # We need to set the root for cargo, but we also need access to the whole repo. |
| 41 | + postUnpack = '' |
| 42 | + # Update sourceRoot to point to the tauri app |
| 43 | + sourceRoot+=/packages/desktop/src-tauri |
| 44 | + ''; |
| 45 | + |
| 46 | + cargoLock = { |
| 47 | + lockFile = ../packages/desktop/src-tauri/Cargo.lock; |
| 48 | + allowBuiltinFetchGit = true; |
| 49 | + }; |
| 50 | + |
| 51 | + node_modules = mkModules { |
| 52 | + version = version; |
| 53 | + src = src; |
| 54 | + }; |
| 55 | + |
| 56 | + nativeBuildInputs = [ |
| 57 | + pkg-config |
| 58 | + bun |
| 59 | + makeBinaryWrapper |
| 60 | + cargo |
| 61 | + rustc |
| 62 | + nodejs |
| 63 | + jq |
| 64 | + ]; |
| 65 | + |
| 66 | + buildInputs = [ |
| 67 | + openssl |
| 68 | + ] |
| 69 | + ++ lib.optionals stdenv.isLinux [ |
| 70 | + dbus |
| 71 | + glib |
| 72 | + gtk3 |
| 73 | + libsoup_3 |
| 74 | + webkitgtk_4_1 |
| 75 | + librsvg |
| 76 | + libappindicator-gtk3 |
| 77 | + ]; |
| 78 | + |
| 79 | + preBuild = '' |
| 80 | + # Restore node_modules |
| 81 | + pushd ../../.. |
| 82 | +
|
| 83 | + # Copy node_modules from the fixed-output derivation |
| 84 | + # We use cp -r --no-preserve=mode to ensure we can write to them if needed, |
| 85 | + # though we usually just read. |
| 86 | + cp -r ${node_modules}/node_modules . |
| 87 | + cp -r ${node_modules}/packages . |
| 88 | +
|
| 89 | + # Ensure node_modules is writable so patchShebangs can update script headers |
| 90 | + chmod -R u+w node_modules |
| 91 | + # Ensure workspace packages are writable for tsgo incremental outputs (.tsbuildinfo) |
| 92 | + chmod -R u+w packages |
| 93 | + # Patch shebangs so scripts can run |
| 94 | + patchShebangs node_modules |
| 95 | +
|
| 96 | + # Copy sidecar |
| 97 | + mkdir -p packages/desktop/src-tauri/sidecars |
| 98 | + targetTriple=${stdenv.hostPlatform.rust.rustcTarget} |
| 99 | + cp ${args.opencode}/bin/opencode packages/desktop/src-tauri/sidecars/opencode-cli-$targetTriple |
| 100 | +
|
| 101 | + # Merge prod config into tauri.conf.json |
| 102 | + if ! jq -s '.[0] * .[1]' \ |
| 103 | + packages/desktop/src-tauri/tauri.conf.json \ |
| 104 | + packages/desktop/src-tauri/tauri.prod.conf.json \ |
| 105 | + > packages/desktop/src-tauri/tauri.conf.json.tmp; then |
| 106 | + echo "Error: failed to merge tauri.conf.json with tauri.prod.conf.json" >&2 |
| 107 | + exit 1 |
| 108 | + fi |
| 109 | + mv packages/desktop/src-tauri/tauri.conf.json.tmp packages/desktop/src-tauri/tauri.conf.json |
| 110 | +
|
| 111 | + # Build the frontend |
| 112 | + cd packages/desktop |
| 113 | +
|
| 114 | + # The 'build' script runs 'bun run typecheck && vite build'. |
| 115 | + bun run build |
| 116 | +
|
| 117 | + popd |
| 118 | + ''; |
| 119 | + |
| 120 | + # Tauri bundles the assets during the rust build phase (which happens after preBuild). |
| 121 | + # It looks for them in the location specified in tauri.conf.json. |
| 122 | + |
| 123 | + postInstall = lib.optionalString stdenv.isLinux '' |
| 124 | + # Wrap the binary to ensure it finds the libraries |
| 125 | + wrapProgram $out/bin/opencode-desktop \ |
| 126 | + --prefix LD_LIBRARY_PATH : ${ |
| 127 | + lib.makeLibraryPath [ |
| 128 | + gtk3 |
| 129 | + webkitgtk_4_1 |
| 130 | + librsvg |
| 131 | + glib |
| 132 | + libsoup_3 |
| 133 | + ] |
| 134 | + } |
| 135 | + ''; |
| 136 | + |
| 137 | + meta = with lib; { |
| 138 | + description = "OpenCode Desktop App"; |
| 139 | + homepage = "https://opencode.ai"; |
| 140 | + license = licenses.mit; |
| 141 | + maintainers = with maintainers; [ ]; |
| 142 | + mainProgram = "opencode-desktop"; |
| 143 | + platforms = platforms.linux ++ platforms.darwin; |
| 144 | + }; |
| 145 | +} |