Files
nix/packages/opencode/default.nix
Evan Reichard 6a48a33caa chore(opencode): update to v1.1.51 and refactor build configuration
- Update opencode from v1.1.48 to v1.1.51
- Refactor build dependencies: remove fzf, add sysctl conditionally for Darwin
- Fix bun to use nixpkgs-unstable input instead of direct dependency
- Add platform-specific outputHash for Darwin and Linux
- Move wrapProgram from postFixup to installPhase
- Remove unnecessary build patch and postPatch/postBuild workarounds
- Fix config path to use absolute home directory
- Add git and home-manager to default shell inputs
- Minor README documentation reordering
2026-02-04 10:04:37 -05:00

181 lines
4.0 KiB
Nix

{ lib
, inputs
, system
, stdenvNoCC
, fetchFromGitHub
, makeBinaryWrapper
, models-dev
, nix-update-script
, ripgrep
, sysctl
, installShellFiles
, versionCheckHook
, writableTmpDirAsHomeHook
,
}:
let
bun = inputs.nixpkgs-unstable.legacyPackages.${system}.bun;
in
stdenvNoCC.mkDerivation (finalAttrs: {
pname = "opencode";
version = "1.1.51";
src = fetchFromGitHub {
owner = "anomalyco";
repo = "opencode";
tag = "v${finalAttrs.version}";
hash = "sha256-i9KR5n6bT0p7xLErlgaq2TAj/B7ZbLd9a+4Czg8q/cI=";
};
node_modules = stdenvNoCC.mkDerivation {
pname = "${finalAttrs.pname}-node_modules";
inherit (finalAttrs) version src;
impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [
"GIT_PROXY_COMMAND"
"SOCKS_SERVER"
];
nativeBuildInputs = [
bun
writableTmpDirAsHomeHook
];
dontConfigure = true;
buildPhase = ''
runHook preBuild
bun install \
--cpu="*" \
--frozen-lockfile \
--filter ./packages/opencode \
--filter ./packages/desktop \
--ignore-scripts \
--no-progress \
--os="*"
bun --bun ./nix/scripts/canonicalize-node-modules.ts
bun --bun ./nix/scripts/normalize-bun-binaries.ts
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p $out
find . -type d -name node_modules -exec cp -R --parents {} $out \;
runHook postInstall
'';
# NOTE: Required else we get errors that our fixed-output derivation references store paths
dontFixup = true;
outputHash =
if stdenvNoCC.hostPlatform.isDarwin then
"sha256-DChoXNWJFlyyAqeiR06BQMLITwVQXy0wvQs58l0d1Xc="
else
"sha256-zkinMkPR1hCBbB5BIuqozQZDpjX4eiFXjM6lpwUx1fM=";
outputHashAlgo = "sha256";
outputHashMode = "recursive";
};
nativeBuildInputs = [
bun
installShellFiles
makeBinaryWrapper
models-dev
writableTmpDirAsHomeHook
];
patches = [
./root_fix.patch # https://github.com/anomalyco/opencode/pull/7691
];
configurePhase = ''
runHook preConfigure
cp -R ${finalAttrs.node_modules}/. .
runHook postConfigure
'';
env.MODELS_DEV_API_JSON = "${models-dev}/dist/_api.json";
env.OPENCODE_VERSION = finalAttrs.version;
env.OPENCODE_CHANNEL = "stable";
buildPhase = ''
runHook preBuild
cd ./packages/opencode
bun --bun ./script/build.ts --single --skip-install
bun --bun ./script/schema.ts schema.json
runHook postBuild
'';
installPhase = ''
runHook preInstall
install -Dm755 dist/opencode-*/bin/opencode $out/bin/opencode
wrapProgram $out/bin/opencode \
--prefix PATH : ${
lib.makeBinPath (
[
ripgrep
]
++ lib.optionals stdenvNoCC.hostPlatform.isDarwin [
sysctl
]
)
}
install -Dm644 schema.json $out/share/opencode/schema.json
runHook postInstall
'';
postInstall = lib.optionalString (stdenvNoCC.buildPlatform.canExecute stdenvNoCC.hostPlatform) ''
installShellCompletion --cmd opencode \
--bash <($out/bin/opencode completion) \
--zsh <(SHELL=/bin/zsh $out/bin/opencode completion)
'';
nativeInstallCheckInputs = [
versionCheckHook
writableTmpDirAsHomeHook
];
doInstallCheck = true;
versionCheckKeepEnvironment = [ "HOME" ];
versionCheckProgramArg = "--version";
passthru = {
jsonschema = "${placeholder "out"}/share/opencode/schema.json";
updateScript = nix-update-script {
extraArgs = [
"--subpackage"
"node_modules"
];
};
};
meta = {
description = "AI coding agent built for the terminal";
homepage = "https://github.com/anomalyco/opencode";
license = lib.licenses.mit;
maintainers = with lib.maintainers; [
delafthi
graham33
];
sourceProvenance = with lib.sourceTypes; [ fromSource ];
platforms = [
"aarch64-linux"
"x86_64-linux"
"aarch64-darwin"
"x86_64-darwin"
];
mainProgram = "opencode";
};
})