opencode.nix 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. { lib, stdenv, stdenvNoCC, bun, fzf, ripgrep, makeBinaryWrapper }:
  2. args:
  3. let
  4. scripts = args.scripts;
  5. mkModules =
  6. attrs:
  7. args.mkNodeModules (
  8. attrs
  9. // {
  10. canonicalizeScript = scripts + "/canonicalize-node-modules.ts";
  11. normalizeBinsScript = scripts + "/normalize-bun-binaries.ts";
  12. }
  13. );
  14. in
  15. stdenvNoCC.mkDerivation (finalAttrs: {
  16. pname = "opencode";
  17. version = args.version;
  18. src = args.src;
  19. node_modules = mkModules {
  20. version = finalAttrs.version;
  21. src = finalAttrs.src;
  22. };
  23. nativeBuildInputs = [
  24. bun
  25. makeBinaryWrapper
  26. ];
  27. configurePhase = ''
  28. runHook preConfigure
  29. cp -R ${finalAttrs.node_modules}/. .
  30. runHook postConfigure
  31. '';
  32. env.MODELS_DEV_API_JSON = args.modelsDev;
  33. env.OPENCODE_VERSION = args.version;
  34. env.OPENCODE_CHANNEL = "stable";
  35. buildPhase = ''
  36. runHook preBuild
  37. cp ${scripts + "/bun-build.ts"} bun-build.ts
  38. substituteInPlace bun-build.ts \
  39. --replace '@VERSION@' "${finalAttrs.version}"
  40. export BUN_COMPILE_TARGET=${args.target}
  41. bun --bun bun-build.ts
  42. runHook postBuild
  43. '';
  44. dontStrip = true;
  45. installPhase = ''
  46. runHook preInstall
  47. cd packages/opencode
  48. if [ ! -f opencode ]; then
  49. echo "ERROR: opencode binary not found in $(pwd)"
  50. ls -la
  51. exit 1
  52. fi
  53. if [ ! -f opencode-worker.js ]; then
  54. echo "ERROR: opencode worker bundle not found in $(pwd)"
  55. ls -la
  56. exit 1
  57. fi
  58. install -Dm755 opencode $out/bin/opencode
  59. install -Dm644 opencode-worker.js $out/bin/opencode-worker.js
  60. if [ -f opencode-assets.manifest ]; then
  61. while IFS= read -r asset; do
  62. [ -z "$asset" ] && continue
  63. if [ ! -f "$asset" ]; then
  64. echo "ERROR: referenced asset \"$asset\" missing"
  65. exit 1
  66. fi
  67. install -Dm644 "$asset" "$out/bin/$(basename "$asset")"
  68. done < opencode-assets.manifest
  69. fi
  70. runHook postInstall
  71. '';
  72. postFixup = ''
  73. wrapProgram "$out/bin/opencode" --prefix PATH : ${lib.makeBinPath [ fzf ripgrep ]}
  74. '';
  75. meta = {
  76. description = "AI coding agent built for the terminal";
  77. longDescription = ''
  78. OpenCode is a terminal-based agent that can build anything.
  79. It combines a TypeScript/JavaScript core with a Go-based TUI
  80. to provide an interactive AI coding experience.
  81. '';
  82. homepage = "https://github.com/sst/opencode";
  83. license = lib.licenses.mit;
  84. platforms = [
  85. "aarch64-linux"
  86. "x86_64-linux"
  87. "aarch64-darwin"
  88. "x86_64-darwin"
  89. ];
  90. mainProgram = "opencode";
  91. };
  92. })