desktop.nix 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. {
  2. lib,
  3. stdenv,
  4. bun,
  5. nodejs,
  6. darwin,
  7. electron_41,
  8. makeWrapper,
  9. writableTmpDirAsHomeHook,
  10. autoPatchelfHook,
  11. copyDesktopItems,
  12. makeDesktopItem,
  13. opencode,
  14. }:
  15. let
  16. electron = electron_41;
  17. in
  18. stdenv.mkDerivation (finalAttrs: {
  19. pname = "opencode-desktop";
  20. inherit (opencode)
  21. version
  22. src
  23. node_modules
  24. patches
  25. ;
  26. nativeBuildInputs = [
  27. bun
  28. nodejs
  29. makeWrapper
  30. writableTmpDirAsHomeHook
  31. ]
  32. ++ lib.optionals stdenv.hostPlatform.isLinux [
  33. autoPatchelfHook
  34. copyDesktopItems
  35. ]
  36. ++ lib.optionals stdenv.hostPlatform.isDarwin [
  37. # Ad-hoc sign the .app: --config.mac.identity=null below skips signing.
  38. darwin.autoSignDarwinBinariesHook
  39. ];
  40. buildInputs = lib.optionals stdenv.hostPlatform.isLinux [
  41. (lib.getLib stdenv.cc.cc)
  42. ];
  43. desktopItems = lib.optional stdenv.hostPlatform.isLinux (makeDesktopItem {
  44. name = "ai.opencode.desktop";
  45. desktopName = "OpenCode";
  46. exec = "opencode-desktop %U";
  47. icon = "ai.opencode.desktop";
  48. # Electron 41 derives X11 WM_CLASS from app.name.
  49. startupWMClass = "OpenCode";
  50. categories = [ "Development" ];
  51. });
  52. env = opencode.env // {
  53. ELECTRON_SKIP_BINARY_DOWNLOAD = "1";
  54. };
  55. postPatch =
  56. # NOTE: Relax Bun version check to be a warning instead of an error
  57. ''
  58. substituteInPlace packages/script/src/index.ts \
  59. --replace-fail 'throw new Error(`This script requires bun@''${expectedBunVersionRange}' \
  60. 'console.warn(`Warning: This script requires bun@''${expectedBunVersionRange}'
  61. ''
  62. # https://github.com/electron/electron/issues/31121
  63. # mac builds use a .app bundle which doesnt have this issue
  64. + lib.optionalString stdenv.isLinux ''
  65. BASE_PATH=packages/desktop
  66. FILES=(src/main/windows.ts)
  67. for file in "''${FILES[@]}"; do
  68. substituteInPlace $BASE_PATH/$file \
  69. --replace-fail "process.resourcesPath" "'$out/opt/opencode-desktop/resources'"
  70. done
  71. '';
  72. preBuild = ''
  73. cp -r "${electron.dist}" $HOME/.electron-dist
  74. chmod -R u+w $HOME/.electron-dist
  75. cp -R ${finalAttrs.node_modules}/. .
  76. patchShebangs node_modules
  77. patchShebangs packages/*/node_modules
  78. '';
  79. buildPhase = ''
  80. runHook preBuild
  81. cd packages/desktop
  82. bun run build
  83. npx electron-builder --dir \
  84. --config electron-builder.config.ts \
  85. --config.mac.identity=null \
  86. --config.electronDist="$HOME/.electron-dist"
  87. runHook postBuild
  88. '';
  89. installPhase = ''
  90. runHook preInstall
  91. ''
  92. + lib.optionalString stdenv.hostPlatform.isDarwin ''
  93. mkdir -p $out/Applications
  94. mv dist/mac*/*.app $out/Applications
  95. makeWrapper "$out/Applications/OpenCode.app/Contents/MacOS/OpenCode" $out/bin/opencode-desktop
  96. ''
  97. + lib.optionalString stdenv.hostPlatform.isLinux ''
  98. mkdir -p $out/opt/opencode-desktop
  99. cp -r dist/linux*-unpacked/{resources,LICENSE*} $out/opt/opencode-desktop
  100. install -Dm644 resources/icons/32x32.png \
  101. "$out/share/icons/hicolor/32x32/apps/ai.opencode.desktop.png"
  102. install -Dm644 resources/icons/64x64.png \
  103. "$out/share/icons/hicolor/64x64/apps/ai.opencode.desktop.png"
  104. install -Dm644 resources/icons/128x128.png \
  105. "$out/share/icons/hicolor/128x128/apps/ai.opencode.desktop.png"
  106. install -Dm644 resources/icons/128x128@2x.png \
  107. "$out/share/icons/hicolor/256x256/apps/ai.opencode.desktop.png"
  108. install -Dm644 resources/icons/icon.png \
  109. "$out/share/icons/hicolor/512x512/apps/ai.opencode.desktop.png"
  110. install -Dm644 resources/ai.opencode.desktop.metainfo.xml \
  111. "$out/share/metainfo/ai.opencode.desktop.metainfo.xml"
  112. makeWrapper ${lib.getExe electron} $out/bin/opencode-desktop \
  113. --inherit-argv0 \
  114. --set ELECTRON_FORCE_IS_PACKAGED 1 \
  115. --add-flags $out/opt/opencode-desktop/resources/app.asar \
  116. --add-flags "\''${NIXOS_OZONE_WL:+\''${WAYLAND_DISPLAY:+--ozone-platform-hint=auto --enable-features=WaylandWindowDecorations --enable-wayland-ime=true}}"
  117. ''
  118. + ''
  119. runHook postInstall
  120. '';
  121. autoPatchelfIgnoreMissingDeps = [
  122. "libc.musl-x86_64.so.1"
  123. ];
  124. meta = {
  125. description = "OpenCode Desktop App";
  126. mainProgram = "opencode-desktop";
  127. inherit (opencode.meta) homepage license platforms;
  128. };
  129. })