2
0

desktop.nix 3.4 KB

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