{ lib, stdenv, fetchFromGitHub, bun, nodejs, ast-grep, makeWrapper, }: let version = "3.12.3"; # Fixed-output derivation to fetch node_modules with network access nodeModules = stdenv.mkDerivation { name = "oh-my-opencode-node-modules-${version}"; src = fetchFromGitHub { owner = "code-yeongyu"; repo = "oh-my-openagent"; rev = "v${version}"; hash = "sha256-8iIpKs0R42NclKqNLfllByHNcKljRn33W8Ij7h0wYfw="; }; nativeBuildInputs = [ bun nodejs ]; buildPhase = '' export HOME=$TMPDIR export BUN_INSTALL_CACHE_DIR=$TMPDIR/bun-cache # Install without optional dependencies (platform-specific binaries) # We'll use ast-grep from nixpkgs instead # Also ignore scripts to prevent build scripts from running with bad shebangs bun install --frozen-lockfile --no-progress --no-optional --ignore-scripts ''; installPhase = '' mkdir -p $out cp -r node_modules $out/ ''; dontFixup = true; outputHashMode = "recursive"; outputHashAlgo = "sha256"; outputHash = "sha256-P49FnMozYS1oTAfQEzh1bfayE0MiTJHAVcWcmsYc8I8="; }; in stdenv.mkDerivation rec { pname = "oh-my-opencode"; inherit version; src = fetchFromGitHub { owner = "code-yeongyu"; repo = "oh-my-openagent"; rev = "v${version}"; hash = "sha256-8iIpKs0R42NclKqNLfllByHNcKljRn33W8Ij7h0wYfw="; }; nativeBuildInputs = [ bun nodejs ast-grep makeWrapper ]; configurePhase = '' runHook preConfigure export HOME=$TMPDIR # Copy pre-fetched node_modules cp -r ${nodeModules}/node_modules . chmod -R u+w node_modules # Patch shebangs patchShebangs node_modules/.bin runHook postConfigure ''; buildPhase = '' runHook preBuild # Make ast-grep available during build export PATH="${ast-grep}/bin:$PATH" # Build: bun build src/index.ts --outdir dist --target bun --format esm --external @ast-grep/napi # && tsc --emitDeclarationOnly # && bun build src/cli/index.ts --outdir dist/cli --target bun --format esm --external @ast-grep/napi # && bun run build:schema # Run just the bun build commands, skip tsc and schema generation bun build src/index.ts --outdir dist --target bun --format esm --external @ast-grep/napi bun build src/cli/index.ts --outdir dist/cli --target bun --format esm --external @ast-grep/napi runHook postBuild ''; installPhase = '' runHook preInstall mkdir -p $out/lib/node_modules/${pname} cp -r dist package.json bin $out/lib/node_modules/${pname}/ cp README.md LICENSE $out/lib/node_modules/${pname}/ 2>/dev/null || true # The plugin includes a CLI - wrap it to ensure ast-grep is available mkdir -p $out/bin makeWrapper $out/lib/node_modules/${pname}/bin/oh-my-opencode.js \ $out/bin/oh-my-opencode \ --prefix PATH : ${ast-grep}/bin \ --set NODE_PATH $out/lib/node_modules runHook postInstall ''; meta = { description = "Advanced orchestration plugin for OpenCode"; homepage = "https://github.com/code-yeongyu/oh-my-openagent"; license = lib.licenses.unfree; mainProgram = "oh-my-opencode"; }; }