opencode.nix 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. configurePhase = ''
  29. runHook preConfigure
  30. cp -R ${finalAttrs.node_modules}/. .
  31. patchShebangs node_modules
  32. patchShebangs packages/*/node_modules
  33. runHook postConfigure
  34. '';
  35. env.MODELS_DEV_API_JSON = "${models-dev}/dist/_api.json";
  36. env.OPENCODE_DISABLE_MODELS_FETCH = true;
  37. env.OPENCODE_VERSION = finalAttrs.version;
  38. env.OPENCODE_CHANNEL = "local";
  39. buildPhase = ''
  40. runHook preBuild
  41. cd ./packages/opencode
  42. bun --bun ./script/build.ts --single --skip-install
  43. bun --bun ./script/schema.ts schema.json
  44. runHook postBuild
  45. '';
  46. installPhase = ''
  47. runHook preInstall
  48. install -Dm755 dist/opencode-*/bin/opencode $out/bin/opencode
  49. install -Dm644 schema.json $out/share/opencode/schema.json
  50. wrapProgram $out/bin/opencode \
  51. --prefix PATH : ${
  52. lib.makeBinPath (
  53. [
  54. ripgrep
  55. ]
  56. # bun runs sysctl to detect if dunning on rosetta2
  57. ++ lib.optional stdenvNoCC.hostPlatform.isDarwin sysctl
  58. )
  59. }
  60. runHook postInstall
  61. '';
  62. postInstall = lib.optionalString (stdenvNoCC.buildPlatform.canExecute stdenvNoCC.hostPlatform) ''
  63. # trick yargs into also generating zsh completions
  64. installShellCompletion --cmd opencode \
  65. --bash <($out/bin/opencode completion) \
  66. --zsh <(SHELL=/bin/zsh $out/bin/opencode completion)
  67. '';
  68. nativeInstallCheckInputs = [
  69. versionCheckHook
  70. writableTmpDirAsHomeHook
  71. ];
  72. doInstallCheck = true;
  73. versionCheckKeepEnvironment = [ "HOME" "OPENCODE_DISABLE_MODELS_FETCH" ];
  74. versionCheckProgramArg = "--version";
  75. passthru = {
  76. jsonschema = "${placeholder "out"}/share/opencode/schema.json";
  77. };
  78. meta = {
  79. description = "The open source coding agent";
  80. homepage = "https://opencode.ai/";
  81. license = lib.licenses.mit;
  82. mainProgram = "opencode";
  83. inherit (node_modules.meta) platforms;
  84. };
  85. })