Skip to content

Commit adde6fb

Browse files
feat(nix): add opencode-electron derivation
1 parent b78c3e3 commit adde6fb

5 files changed

Lines changed: 99 additions & 2 deletions

File tree

flake.nix

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
opencode-desktop = final.callPackage ./nix/desktop.nix {
4646
inherit opencode;
4747
};
48+
opencode-electron = final.callPackage ./nix/desktop-electron.nix {
49+
inherit opencode;
50+
};
4851
};
4952
};
5053

@@ -63,6 +66,9 @@
6366
opencode-desktop = pkgs.callPackage ./nix/desktop.nix {
6467
inherit opencode;
6568
};
69+
opencode-electron = pkgs.callPackage ./nix/desktop-electron.nix {
70+
inherit opencode;
71+
};
6672
# Updater derivation with fakeHash - build fails and reveals correct hash
6773
node_modules_updater = node_modules.override {
6874
hash = pkgs.lib.fakeHash;

nix/desktop-electron.nix

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

nix/hashes.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"nodeModules": {
3-
"x86_64-linux": "sha256-OPbZUo/fQv2Xsf+NEZV08GLBMN/DXovhRvn2JkesFtY=",
3+
"x86_64-linux": "sha256-60KBy/mySuIKbBD5aCS4ZAQqnwZ4PjLMAqZH7gpKCFY=",
44
"aarch64-linux": "sha256-WK7xlVLuirKDN5LaqjBn7qpv5bYVtYHZw0qRNKX4xXg=",
55
"aarch64-darwin": "sha256-BAoAdeLQ+lXDD7Klxoxij683OVVug8KXEMRUqIQAjc8=",
66
"x86_64-darwin": "sha256-ZOBwNR2gZgc5f+y3VIBBT4qZpeZfg7Of6AaGDOfqsG8="

nix/node_modules.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ stdenvNoCC.mkDerivation {
5555
--filter './packages/opencode' \
5656
--filter './packages/desktop' \
5757
--filter './packages/app' \
58-
--filter './packages/shared' \
58+
--filter './packages/desktop-electron' \
5959
--frozen-lockfile \
6060
--ignore-scripts \
6161
--no-progress

nix/opencode.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ stdenvNoCC.mkDerivation (finalAttrs: {
8888

8989
passthru = {
9090
jsonschema = "${placeholder "out"}/share/opencode/schema.json";
91+
env = finalAttrs.env;
9192
};
9293

9394
meta = {

0 commit comments

Comments
 (0)