opencode.nix 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. {
  2. lib,
  3. stdenvNoCC,
  4. callPackage,
  5. bun,
  6. nodejs,
  7. sysctl,
  8. makeBinaryWrapper,
  9. models-dev,
  10. ripgrep,
  11. installShellFiles,
  12. versionCheckHook,
  13. writableTmpDirAsHomeHook,
  14. node_modules ? callPackage ./node-modules.nix { },
  15. }:
  16. stdenvNoCC.mkDerivation (finalAttrs: {
  17. pname = "opencode";
  18. inherit (node_modules) version src;
  19. inherit node_modules;
  20. nativeBuildInputs = [
  21. bun
  22. nodejs # for patchShebangs node_modules
  23. installShellFiles
  24. makeBinaryWrapper
  25. models-dev
  26. writableTmpDirAsHomeHook
  27. ];
  28. postPatch = ''
  29. # NOTE: Relax Bun version check to be a warning instead of an error
  30. substituteInPlace packages/script/src/index.ts \
  31. --replace-fail 'throw new Error(`This script requires bun@''${expectedBunVersionRange}' \
  32. 'console.warn(`Warning: This script requires bun@''${expectedBunVersionRange}'
  33. '';
  34. configurePhase = ''
  35. runHook preConfigure
  36. cp -R ${finalAttrs.node_modules}/. .
  37. patchShebangs node_modules
  38. patchShebangs packages/*/node_modules
  39. runHook postConfigure
  40. '';
  41. env.MODELS_DEV_API_JSON = "${models-dev}/dist/_api.json";
  42. env.OPENCODE_DISABLE_MODELS_FETCH = true;
  43. env.OPENCODE_VERSION = finalAttrs.version;
  44. env.OPENCODE_CHANNEL = "prod";
  45. buildPhase = ''
  46. runHook preBuild
  47. cd ./packages/opencode
  48. bun --bun ./script/build.ts --single --skip-install
  49. bun --bun ./script/schema.ts schema.json
  50. runHook postBuild
  51. '';
  52. installPhase = ''
  53. runHook preInstall
  54. install -Dm755 dist/opencode-*/bin/opencode $out/bin/opencode
  55. install -Dm644 schema.json $out/share/opencode/schema.json
  56. wrapProgram $out/bin/opencode \
  57. --prefix PATH : ${
  58. lib.makeBinPath (
  59. [
  60. ripgrep
  61. ]
  62. # bun runs sysctl to detect if running on rosetta2
  63. ++ lib.optional stdenvNoCC.hostPlatform.isDarwin sysctl
  64. )
  65. }
  66. runHook postInstall
  67. '';
  68. postInstall = lib.optionalString (stdenvNoCC.buildPlatform.canExecute stdenvNoCC.hostPlatform) ''
  69. # trick yargs into also generating zsh completions
  70. installShellCompletion --cmd opencode \
  71. --bash <($out/bin/opencode completion) \
  72. --zsh <(SHELL=/bin/zsh $out/bin/opencode completion)
  73. '';
  74. nativeInstallCheckInputs = [
  75. versionCheckHook
  76. writableTmpDirAsHomeHook
  77. ];
  78. doInstallCheck = true;
  79. versionCheckKeepEnvironment = [ "HOME" "OPENCODE_DISABLE_MODELS_FETCH" ];
  80. versionCheckProgramArg = "--version";
  81. passthru = {
  82. jsonschema = "${placeholder "out"}/share/opencode/schema.json";
  83. env = finalAttrs.env;
  84. };
  85. meta = {
  86. description = "The open source coding agent";
  87. homepage = "https://opencode.ai";
  88. license = lib.licenses.mit;
  89. mainProgram = "opencode";
  90. inherit (node_modules.meta) platforms;
  91. };
  92. })