summaryrefslogtreecommitdiffhomepage
path: root/nix/opencode.nix
blob: bec2997608e1601e4b8d9951daa365abb9ca2265 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
{ lib, stdenv, stdenvNoCC, bun, fzf, ripgrep, makeBinaryWrapper }:
args:
let
  scripts = args.scripts;
  mkModules =
    attrs:
    args.mkNodeModules (
      attrs
      // {
        canonicalizeScript = scripts + "/canonicalize-node-modules.ts";
        normalizeBinsScript = scripts + "/normalize-bun-binaries.ts";
      }
    );
in
stdenvNoCC.mkDerivation (finalAttrs: {
  pname = "opencode";
  version = args.version;

  src = args.src;

  node_modules = mkModules {
    version = finalAttrs.version;
    src = finalAttrs.src;
  };

  nativeBuildInputs = [
    bun
    makeBinaryWrapper
  ];

  configurePhase = ''
    runHook preConfigure
    cp -R ${finalAttrs.node_modules}/. .
    runHook postConfigure
  '';

  env.MODELS_DEV_API_JSON = args.modelsDev;
  env.OPENCODE_VERSION = args.version;
  env.OPENCODE_CHANNEL = "stable";

  buildPhase = ''
    runHook preBuild

    cp ${scripts + "/bun-build.ts"} bun-build.ts

    substituteInPlace bun-build.ts \
      --replace '@VERSION@' "${finalAttrs.version}"

    export BUN_COMPILE_TARGET=${args.target}
    bun --bun bun-build.ts

    runHook postBuild
  '';

  dontStrip = true;

  installPhase = ''
    runHook preInstall

    cd packages/opencode
    if [ ! -f opencode ]; then
      echo "ERROR: opencode binary not found in $(pwd)"
      ls -la
      exit 1
    fi
    if [ ! -f opencode-worker.js ]; then
      echo "ERROR: opencode worker bundle not found in $(pwd)"
      ls -la
      exit 1
    fi

    install -Dm755 opencode $out/bin/opencode
    install -Dm644 opencode-worker.js $out/bin/opencode-worker.js
    if [ -f opencode-assets.manifest ]; then
      while IFS= read -r asset; do
        [ -z "$asset" ] && continue
        if [ ! -f "$asset" ]; then
          echo "ERROR: referenced asset \"$asset\" missing"
          exit 1
        fi
        install -Dm644 "$asset" "$out/bin/$(basename "$asset")"
      done < opencode-assets.manifest
    fi
    runHook postInstall
  '';

  postFixup = ''
    wrapProgram "$out/bin/opencode" --prefix PATH : ${lib.makeBinPath [ fzf ripgrep ]}
  '';

  meta = {
    description = "AI coding agent built for the terminal";
    longDescription = ''
      OpenCode is a terminal-based agent that can build anything.
      It combines a TypeScript/JavaScript core with a Go-based TUI
      to provide an interactive AI coding experience.
    '';
    homepage = "https://github.com/sst/opencode";
    license = lib.licenses.mit;
    platforms = [
      "aarch64-linux"
      "x86_64-linux"
      "aarch64-darwin"
      "x86_64-darwin"
    ];
    mainProgram = "opencode";
  };
})