opencode.nix 2.4 KB

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