|
| 1 | +{ |
| 2 | + lib, |
| 3 | + stdenvNoCC, |
| 4 | + bun, |
| 5 | + fetchFromGitHub, |
| 6 | + makeBinaryWrapper, |
| 7 | + models-dev, |
| 8 | + nodejs, |
| 9 | + nix-update-script, |
| 10 | + ripgrep, |
| 11 | + sysctl, |
| 12 | + installShellFiles, |
| 13 | + versionCheckHook, |
| 14 | + writableTmpDirAsHomeHook, |
| 15 | +}: |
| 16 | +stdenvNoCC.mkDerivation (finalAttrs: { |
| 17 | + pname = "opencode"; |
| 18 | + version = "1.3.10"; |
| 19 | + |
| 20 | + src = fetchFromGitHub { |
| 21 | + owner = "anomalyco"; |
| 22 | + repo = "opencode"; |
| 23 | + tag = "v${finalAttrs.version}"; |
| 24 | + hash = "sha256-/pVesY9fyfeAheT1gMvoVCaO9GlRHQB0H4HIB56pwnE="; |
| 25 | + }; |
| 26 | + |
| 27 | + node_modules = stdenvNoCC.mkDerivation { |
| 28 | + pname = "${finalAttrs.pname}-node_modules"; |
| 29 | + inherit (finalAttrs) version src; |
| 30 | + |
| 31 | + impureEnvVars = |
| 32 | + lib.fetchers.proxyImpureEnvVars |
| 33 | + ++ [ |
| 34 | + "GIT_PROXY_COMMAND" |
| 35 | + "SOCKS_SERVER" |
| 36 | + ]; |
| 37 | + |
| 38 | + nativeBuildInputs = [ |
| 39 | + bun |
| 40 | + writableTmpDirAsHomeHook |
| 41 | + ]; |
| 42 | + |
| 43 | + dontConfigure = true; |
| 44 | + |
| 45 | + buildPhase = '' |
| 46 | + runHook preBuild |
| 47 | +
|
| 48 | + bun install \ |
| 49 | + --cpu="*" \ |
| 50 | + --frozen-lockfile \ |
| 51 | + --filter ./packages/app \ |
| 52 | + --filter ./packages/desktop \ |
| 53 | + --filter ./packages/opencode \ |
| 54 | + --ignore-scripts \ |
| 55 | + --no-progress \ |
| 56 | + --os="*" |
| 57 | +
|
| 58 | + bun --bun ./nix/scripts/canonicalize-node-modules.ts |
| 59 | + bun --bun ./nix/scripts/normalize-bun-binaries.ts |
| 60 | +
|
| 61 | + runHook postBuild |
| 62 | + ''; |
| 63 | + |
| 64 | + installPhase = '' |
| 65 | + runHook preInstall |
| 66 | +
|
| 67 | + mkdir -p $out |
| 68 | + find . -type d -name node_modules -exec cp -R --parents {} $out \; |
| 69 | +
|
| 70 | + runHook postInstall |
| 71 | + ''; |
| 72 | + |
| 73 | + # NOTE: Required else we get errors that our fixed-output derivation references store paths |
| 74 | + dontFixup = true; |
| 75 | + |
| 76 | + outputHash = "sha256-Q7eyK6GNYE5dBYooDHbIQSkfe4fs6L7tzCfbsIlYY7o="; |
| 77 | + outputHashAlgo = "sha256"; |
| 78 | + outputHashMode = "recursive"; |
| 79 | + }; |
| 80 | + |
| 81 | + nativeBuildInputs = [ |
| 82 | + bun |
| 83 | + nodejs |
| 84 | + installShellFiles |
| 85 | + makeBinaryWrapper |
| 86 | + models-dev |
| 87 | + writableTmpDirAsHomeHook |
| 88 | + ]; |
| 89 | + |
| 90 | + postPatch = '' |
| 91 | + # NOTE: Relax Bun version check to be a warning instead of an error |
| 92 | + substituteInPlace packages/script/src/index.ts \ |
| 93 | + --replace-fail 'throw new Error(`This script requires bun@''${expectedBunVersionRange}' \ |
| 94 | + 'console.warn(`Warning: This script requires bun@''${expectedBunVersionRange}' |
| 95 | + ''; |
| 96 | + |
| 97 | + configurePhase = '' |
| 98 | + runHook preConfigure |
| 99 | +
|
| 100 | + cp -R ${finalAttrs.node_modules}/. . |
| 101 | + patchShebangs node_modules |
| 102 | + patchShebangs packages/*/node_modules |
| 103 | +
|
| 104 | + runHook postConfigure |
| 105 | + ''; |
| 106 | + |
| 107 | + env.MODELS_DEV_API_JSON = "${models-dev}/dist/_api.json"; |
| 108 | + env.OPENCODE_VERSION = finalAttrs.version; |
| 109 | + env.OPENCODE_CHANNEL = "stable"; |
| 110 | + |
| 111 | + buildPhase = '' |
| 112 | + runHook preBuild |
| 113 | +
|
| 114 | + cd ./packages/opencode |
| 115 | + bun --bun ./script/build.ts --single --skip-install |
| 116 | + bun --bun ./script/schema.ts schema.json |
| 117 | +
|
| 118 | + runHook postBuild |
| 119 | + ''; |
| 120 | + |
| 121 | + installPhase = '' |
| 122 | + runHook preInstall |
| 123 | +
|
| 124 | + install -Dm755 dist/opencode-*/bin/opencode $out/bin/opencode |
| 125 | + wrapProgram $out/bin/opencode \ |
| 126 | + --prefix PATH : ${ |
| 127 | + lib.makeBinPath ( |
| 128 | + [ |
| 129 | + ripgrep |
| 130 | + ] |
| 131 | + ++ lib.optionals stdenvNoCC.hostPlatform.isDarwin [ |
| 132 | + sysctl |
| 133 | + ] |
| 134 | + ) |
| 135 | + } |
| 136 | +
|
| 137 | + install -Dm644 schema.json $out/share/opencode/schema.json |
| 138 | +
|
| 139 | + runHook postInstall |
| 140 | + ''; |
| 141 | + |
| 142 | + postInstall = lib.optionalString (stdenvNoCC.buildPlatform.canExecute stdenvNoCC.hostPlatform) '' |
| 143 | + installShellCompletion --cmd opencode \ |
| 144 | + --bash <($out/bin/opencode completion) \ |
| 145 | + --zsh <(SHELL=/bin/zsh $out/bin/opencode completion) |
| 146 | + ''; |
| 147 | + |
| 148 | + nativeInstallCheckInputs = [ |
| 149 | + versionCheckHook |
| 150 | + writableTmpDirAsHomeHook |
| 151 | + ]; |
| 152 | + doInstallCheck = true; |
| 153 | + versionCheckKeepEnvironment = ["HOME"]; |
| 154 | + versionCheckProgramArg = "--version"; |
| 155 | + |
| 156 | + passthru = { |
| 157 | + jsonschema = "${placeholder "out"}/share/opencode/schema.json"; |
| 158 | + updateScript = nix-update-script { |
| 159 | + extraArgs = [ |
| 160 | + "--subpackage" |
| 161 | + "node_modules" |
| 162 | + ]; |
| 163 | + }; |
| 164 | + }; |
| 165 | + |
| 166 | + meta = { |
| 167 | + description = "AI coding agent built for the terminal"; |
| 168 | + homepage = "https://github.com/anomalyco/opencode"; |
| 169 | + license = lib.licenses.mit; |
| 170 | + maintainers = with lib.maintainers; [ |
| 171 | + delafthi |
| 172 | + DuskyElf |
| 173 | + graham33 |
| 174 | + superherointj |
| 175 | + ]; |
| 176 | + sourceProvenance = with lib.sourceTypes; [fromSource]; |
| 177 | + platforms = [ |
| 178 | + "aarch64-linux" |
| 179 | + "x86_64-linux" |
| 180 | + "aarch64-darwin" |
| 181 | + "x86_64-darwin" |
| 182 | + ]; |
| 183 | + mainProgram = "opencode"; |
| 184 | + badPlatforms = [ |
| 185 | + # Broken as 2026-04-23, errors as: |
| 186 | + # CPU lacks AVX support, strange crashes may occur. Reinstall Bun |
| 187 | + "x86_64-darwin" |
| 188 | + ]; |
| 189 | + }; |
| 190 | +}) |
0 commit comments