desktop.nix 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. {
  2. lib,
  3. stdenv,
  4. bun,
  5. nodejs,
  6. darwin,
  7. electron_41,
  8. makeWrapper,
  9. writableTmpDirAsHomeHook,
  10. autoPatchelfHook,
  11. opencode,
  12. }:
  13. let
  14. electron = electron_41;
  15. in
  16. stdenv.mkDerivation (finalAttrs: {
  17. pname = "opencode-desktop";
  18. inherit (opencode)
  19. version
  20. src
  21. node_modules
  22. patches
  23. ;
  24. nativeBuildInputs = [
  25. bun
  26. nodejs
  27. makeWrapper
  28. writableTmpDirAsHomeHook
  29. ] ++ lib.optionals stdenv.hostPlatform.isLinux [
  30. autoPatchelfHook
  31. ] ++ lib.optionals stdenv.hostPlatform.isDarwin [
  32. # Ad-hoc sign the .app: --config.mac.identity=null below skips signing.
  33. darwin.autoSignDarwinBinariesHook
  34. ];
  35. buildInputs = lib.optionals stdenv.hostPlatform.isLinux [
  36. (lib.getLib stdenv.cc.cc)
  37. ];
  38. env = opencode.env // {
  39. ELECTRON_SKIP_BINARY_DOWNLOAD = "1";
  40. };
  41. # https://github.com/electron/electron/issues/31121
  42. # mac builds use a .app bundle which doesnt have this issue
  43. postPatch = lib.optionalString stdenv.isLinux ''
  44. BASE_PATH=packages/desktop
  45. FILES=(src/main/windows.ts)
  46. for file in "''${FILES[@]}"; do
  47. substituteInPlace $BASE_PATH/$file \
  48. --replace-fail "process.resourcesPath" "'$out/opt/opencode-desktop/resources'"
  49. done
  50. '';
  51. preBuild = ''
  52. cp -r "${electron.dist}" $HOME/.electron-dist
  53. chmod -R u+w $HOME/.electron-dist
  54. cp -R ${finalAttrs.node_modules}/. .
  55. patchShebangs node_modules
  56. patchShebangs packages/*/node_modules
  57. '';
  58. buildPhase = ''
  59. runHook preBuild
  60. cd packages/desktop
  61. bun run build
  62. npx electron-builder --dir \
  63. --config electron-builder.config.ts \
  64. --config.mac.identity=null \
  65. --config.electronDist="$HOME/.electron-dist"
  66. runHook postBuild
  67. '';
  68. installPhase =
  69. ''
  70. runHook preInstall
  71. ''
  72. + lib.optionalString stdenv.hostPlatform.isDarwin ''
  73. mkdir -p $out/Applications
  74. mv dist/mac*/*.app $out/Applications
  75. makeWrapper "$out/Applications/OpenCode.app/Contents/MacOS/OpenCode" $out/bin/opencode-desktop
  76. ''
  77. + lib.optionalString stdenv.hostPlatform.isLinux ''
  78. mkdir -p $out/opt/opencode-desktop
  79. cp -r dist/linux*-unpacked/{resources,LICENSE*} $out/opt/opencode-desktop
  80. makeWrapper ${lib.getExe electron} $out/bin/opencode-desktop \
  81. --inherit-argv0 \
  82. --set ELECTRON_FORCE_IS_PACKAGED 1 \
  83. --add-flags $out/opt/opencode-desktop/resources/app.asar \
  84. --add-flags "\''${NIXOS_OZONE_WL:+\''${WAYLAND_DISPLAY:+--ozone-platform-hint=auto --enable-features=WaylandWindowDecorations --enable-wayland-ime=true}}"
  85. ''
  86. + ''
  87. runHook postInstall
  88. '';
  89. autoPatchelfIgnoreMissingDeps = [
  90. "libc.musl-x86_64.so.1"
  91. ];
  92. meta = {
  93. description = "OpenCode Desktop App";
  94. mainProgram = "opencode-desktop";
  95. inherit (opencode.meta) homepage license platforms;
  96. };
  97. })