Skip to content

Commit 4643e13

Browse files
feat(nix): add opencode-electron derivation (anomalyco#16163)
1 parent 042e6a5 commit 4643e13

3 files changed

Lines changed: 82 additions & 83 deletions

File tree

flake.nix

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,14 @@
3737
node_modules = final.callPackage ./nix/node_modules.nix {
3838
inherit rev;
3939
};
40+
in
41+
rec {
4042
opencode = final.callPackage ./nix/opencode.nix {
4143
inherit node_modules;
4244
};
43-
desktop = final.callPackage ./nix/desktop.nix {
45+
opencode-desktop = final.callPackage ./nix/desktop.nix {
4446
inherit opencode;
4547
};
46-
in
47-
{
48-
inherit opencode;
49-
opencode-desktop = desktop;
5048
};
5149
};
5250

@@ -56,16 +54,15 @@
5654
node_modules = pkgs.callPackage ./nix/node_modules.nix {
5755
inherit rev;
5856
};
57+
in
58+
rec {
59+
default = opencode;
5960
opencode = pkgs.callPackage ./nix/opencode.nix {
6061
inherit node_modules;
6162
};
62-
desktop = pkgs.callPackage ./nix/desktop.nix {
63+
opencode-desktop = pkgs.callPackage ./nix/desktop.nix {
6364
inherit opencode;
6465
};
65-
in
66-
{
67-
default = opencode;
68-
inherit opencode desktop;
6966
# Updater derivation with fakeHash - build fails and reveals correct hash
7067
node_modules_updater = node_modules.override {
7168
hash = pkgs.lib.fakeHash;

nix/desktop.nix

Lines changed: 72 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,101 @@
11
{
22
lib,
33
stdenv,
4-
rustPlatform,
5-
pkg-config,
6-
cargo-tauri,
74
bun,
85
nodejs,
9-
cargo,
10-
rustc,
11-
jq,
12-
wrapGAppsHook4,
6+
electron_41,
137
makeWrapper,
14-
dbus,
15-
glib,
16-
gtk4,
17-
libsoup_3,
18-
librsvg,
19-
libappindicator,
20-
glib-networking,
21-
openssl,
22-
webkitgtk_4_1,
23-
gst_all_1,
8+
writableTmpDirAsHomeHook,
9+
autoPatchelfHook,
2410
opencode,
2511
}:
26-
rustPlatform.buildRustPackage (finalAttrs: {
12+
let
13+
electron = electron_41;
14+
in
15+
stdenv.mkDerivation (finalAttrs: {
2716
pname = "opencode-desktop";
28-
inherit (opencode)
29-
version
30-
src
31-
node_modules
32-
patches
33-
;
34-
35-
cargoRoot = "packages/desktop/src-tauri";
36-
cargoLock.lockFile = ../packages/desktop/src-tauri/Cargo.lock;
37-
buildAndTestSubdir = finalAttrs.cargoRoot;
17+
inherit (opencode) version src node_modules;
3818

3919
nativeBuildInputs = [
40-
pkg-config
41-
cargo-tauri.hook
4220
bun
43-
nodejs # for patchShebangs node_modules
44-
cargo
45-
rustc
46-
jq
21+
nodejs
4722
makeWrapper
48-
] ++ lib.optionals stdenv.hostPlatform.isLinux [ wrapGAppsHook4 ];
23+
writableTmpDirAsHomeHook
24+
] ++ lib.optionals stdenv.hostPlatform.isLinux [
25+
autoPatchelfHook
26+
];
4927

50-
buildInputs = lib.optionals stdenv.isLinux [
51-
dbus
52-
glib
53-
gtk4
54-
libsoup_3
55-
librsvg
56-
libappindicator
57-
glib-networking
58-
openssl
59-
webkitgtk_4_1
60-
gst_all_1.gstreamer
61-
gst_all_1.gst-plugins-base
62-
gst_all_1.gst-plugins-good
63-
gst_all_1.gst-plugins-bad
28+
buildInputs = lib.optionals stdenv.hostPlatform.isLinux [
29+
(lib.getLib stdenv.cc.cc)
6430
];
6531

66-
strictDeps = true;
32+
env = opencode.env // {
33+
ELECTRON_SKIP_BINARY_DOWNLOAD = "1";
34+
};
35+
36+
# https://github.com/electron/electron/issues/31121
37+
# mac builds use a .app bundle which doesnt have this issue
38+
postPatch = lib.optionalString stdenv.isLinux ''
39+
BASE_PATH=packages/desktop
40+
FILES=(src/main/windows.ts)
41+
for file in "''${FILES[@]}"; do
42+
substituteInPlace $BASE_PATH/$file \
43+
--replace-fail "process.resourcesPath" "'$out/opt/opencode-desktop/resources'"
44+
done
45+
'';
6746

6847
preBuild = ''
69-
cp -a ${finalAttrs.node_modules}/{node_modules,packages} .
70-
chmod -R u+w node_modules packages
71-
patchShebangs node_modules
72-
patchShebangs packages/desktop/node_modules
48+
cp -r "${electron.dist}" $HOME/.electron-dist
49+
chmod -R u+w $HOME/.electron-dist
7350
74-
mkdir -p packages/desktop/src-tauri/sidecars
75-
cp ${opencode}/bin/opencode packages/desktop/src-tauri/sidecars/opencode-cli-${stdenv.hostPlatform.rust.rustcTarget}
51+
cp -R ${finalAttrs.node_modules}/. .
52+
patchShebangs node_modules
53+
patchShebangs packages/*/node_modules
7654
'';
7755

78-
# see publish-tauri job in .github/workflows/publish.yml
79-
tauriBuildFlags = [
80-
"--config"
81-
"tauri.prod.conf.json"
82-
"--no-sign" # no code signing or auto updates
83-
];
56+
buildPhase = ''
57+
runHook preBuild
58+
59+
cd packages/desktop
60+
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"
8466
85-
# FIXME: workaround for concerns about case insensitive filesystems
86-
# should be removed once binary is renamed or decided otherwise
87-
# darwin output is a .app bundle so no conflict
88-
postFixup = lib.optionalString stdenv.hostPlatform.isLinux ''
89-
mv $out/bin/OpenCode $out/bin/opencode-desktop
90-
sed -i 's|^Exec=OpenCode$|Exec=opencode-desktop|' $out/share/applications/OpenCode.desktop
67+
runHook postBuild
9168
'';
9269

70+
installPhase =
71+
''
72+
runHook preInstall
73+
''
74+
+ lib.optionalString stdenv.hostPlatform.isDarwin ''
75+
mkdir -p $out/Applications
76+
mv dist/mac*/*.app $out/Applications
77+
makeWrapper "$out/Applications/OpenCode.app/Contents/MacOS/OpenCode" $out/bin/opencode-desktop
78+
''
79+
+ lib.optionalString stdenv.hostPlatform.isLinux ''
80+
mkdir -p $out/opt/opencode-desktop
81+
cp -r dist/linux*-unpacked/{resources,LICENSE*} $out/opt/opencode-desktop
82+
makeWrapper ${lib.getExe electron} $out/bin/opencode-desktop \
83+
--inherit-argv0 \
84+
--set ELECTRON_FORCE_IS_PACKAGED 1 \
85+
--add-flags $out/opt/opencode-desktop/resources/app.asar \
86+
--add-flags "\''${NIXOS_OZONE_WL:+\''${WAYLAND_DISPLAY:+--ozone-platform-hint=auto --enable-features=WaylandWindowDecorations --enable-wayland-ime=true}}"
87+
''
88+
+ ''
89+
runHook postInstall
90+
'';
91+
92+
autoPatchelfIgnoreMissingDeps = [
93+
"libc.musl-x86_64.so.1"
94+
];
95+
9396
meta = {
9497
description = "OpenCode Desktop App";
95-
homepage = "https://opencode.ai";
96-
license = lib.licenses.mit;
9798
mainProgram = "opencode-desktop";
98-
inherit (opencode.meta) platforms;
99+
inherit (opencode.meta) homepage license platforms;
99100
};
100101
})

nix/opencode.nix

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ stdenvNoCC.mkDerivation (finalAttrs: {
4040
env.MODELS_DEV_API_JSON = "${models-dev}/dist/_api.json";
4141
env.OPENCODE_DISABLE_MODELS_FETCH = true;
4242
env.OPENCODE_VERSION = finalAttrs.version;
43-
env.OPENCODE_CHANNEL = "local";
43+
env.OPENCODE_CHANNEL = "prod";
4444

4545
buildPhase = ''
4646
runHook preBuild
@@ -89,11 +89,12 @@ stdenvNoCC.mkDerivation (finalAttrs: {
8989

9090
passthru = {
9191
jsonschema = "${placeholder "out"}/share/opencode/schema.json";
92+
env = finalAttrs.env;
9293
};
9394

9495
meta = {
9596
description = "The open source coding agent";
96-
homepage = "https://opencode.ai/";
97+
homepage = "https://opencode.ai";
9798
license = lib.licenses.mit;
9899
mainProgram = "opencode";
99100
inherit (node_modules.meta) platforms;

0 commit comments

Comments
 (0)