Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@
node_modules = final.callPackage ./nix/node_modules.nix {
inherit rev;
};
in
rec {
opencode = final.callPackage ./nix/opencode.nix {
inherit node_modules;
};
desktop = final.callPackage ./nix/desktop.nix {
opencode-desktop = final.callPackage ./nix/desktop.nix {
inherit opencode;
};
opencode-electron = final.callPackage ./nix/desktop-electron.nix {
inherit opencode;
};
in
{
inherit opencode;
opencode-desktop = desktop;
};
};

Expand All @@ -56,16 +57,18 @@
node_modules = pkgs.callPackage ./nix/node_modules.nix {
inherit rev;
};
in
rec {
default = opencode;
opencode = pkgs.callPackage ./nix/opencode.nix {
inherit node_modules;
};
desktop = pkgs.callPackage ./nix/desktop.nix {
opencode-desktop = pkgs.callPackage ./nix/desktop.nix {
inherit opencode;
};
opencode-electron = pkgs.callPackage ./nix/desktop-electron.nix {
inherit opencode;
};
in
{
default = opencode;
inherit opencode desktop;
# Updater derivation with fakeHash - build fails and reveals correct hash
node_modules_updater = node_modules.override {
hash = pkgs.lib.fakeHash;
Expand Down
89 changes: 89 additions & 0 deletions nix/desktop-electron.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
lib,
stdenv,
bun,
nodejs,
electron_40,
makeWrapper,
writableTmpDirAsHomeHook,
opencode,
}:
let
electron = electron_40;
in
stdenv.mkDerivation (finalAttrs: {
pname = "opencode-electron";
inherit (opencode) version src node_modules;

nativeBuildInputs = [
bun
nodejs
makeWrapper
writableTmpDirAsHomeHook
];

env.ELECTRON_SKIP_BINARY_DOWNLOAD = "1";

# https://github.com/electron/electron/issues/31121
postPatch = lib.optionalString stdenv.isLinux ''
BASE_PATH=packages/desktop-electron
FILES=(src/main/cli.ts src/main/windows.ts)
for file in "''${FILES[@]}"; do
substituteInPlace $BASE_PATH/$file \
--replace-fail "process.resourcesPath" "'$out/opt/opencode-electron/resources'"
done
'';

preBuild = ''
cp -r "${electron.dist}" $HOME/.electron-dist
chmod -R u+w $HOME/.electron-dist

cp -R ${finalAttrs.node_modules}/. .
patchShebangs node_modules
patchShebangs packages/desktop-electron/node_modules

cp ${lib.getExe opencode} packages/desktop-electron/resources/opencode-cli
'';

buildPhase = ''
runHook preBuild

cd packages/desktop-electron

bun run build
npx electron-builder --dir \
--config electron-builder.config.ts \
--config.mac.identity=null \
--config.electronDist="$HOME/.electron-dist"

runHook postBuild
'';

installPhase =
''
runHook preInstall
''
+ lib.optionalString stdenv.hostPlatform.isDarwin ''
mkdir -p $out/Applications
mv "dist/mac*/*.app" "$out/Applications/"
makeWrapper $out/Applications/OpenCode.app/Contents/MacOS/OpenCode $out/bin/opencode-electron
''
+ lib.optionalString stdenv.hostPlatform.isLinux ''
mkdir -p $out/opt/opencode-electron
cp -r dist/linux*-unpacked/{resources,LICENSE*} $out/opt/opencode-electron
makeWrapper ${lib.getExe electron} $out/bin/opencode-electron \
--inherit-argv0 \
--set ELECTRON_FORCE_IS_PACKAGED 1 \
--add-flags $out/opt/opencode-electron/resources/app.asar \
--add-flags "\''${NIXOS_OZONE_WL:+\''${WAYLAND_DISPLAY:+--ozone-platform-hint=auto --enable-features=WaylandWindowDecorations --enable-wayland-ime=true}}"
''
+ ''
runHook postInstall
'';

meta = {
description = "OpenCode Electron Desktop App";
mainProgram = "opencode-electron";
inherit (opencode.meta) homepage license platforms;
};
})
4 changes: 1 addition & 3 deletions nix/desktop.nix
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ rustPlatform.buildRustPackage (finalAttrs: {

meta = {
description = "OpenCode Desktop App";
homepage = "https://opencode.ai";
license = lib.licenses.mit;
mainProgram = "opencode-desktop";
inherit (opencode.meta) platforms;
inherit (opencode.meta) homepage license platforms;
};
})
1 change: 1 addition & 0 deletions nix/node_modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ stdenvNoCC.mkDerivation {
--filter '!./' \
--filter './packages/opencode' \
--filter './packages/desktop' \
--filter './packages/desktop-electron' \
--frozen-lockfile \
--ignore-scripts \
--no-progress
Expand Down
1 change: 0 additions & 1 deletion nix/opencode.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ stdenvNoCC.mkDerivation (finalAttrs: {
env.MODELS_DEV_API_JSON = "${models-dev}/dist/_api.json";
env.OPENCODE_DISABLE_MODELS_FETCH = true;
env.OPENCODE_VERSION = finalAttrs.version;
env.OPENCODE_CHANNEL = "local";

buildPhase = ''
runHook preBuild
Expand Down
Loading