flake.nix 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. {
  2. description = "OpenCode development flake";
  3. inputs = {
  4. nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
  5. };
  6. outputs =
  7. {
  8. nixpkgs,
  9. ...
  10. }:
  11. let
  12. systems = [
  13. "aarch64-linux"
  14. "x86_64-linux"
  15. "aarch64-darwin"
  16. "x86_64-darwin"
  17. ];
  18. inherit (nixpkgs) lib;
  19. forEachSystem = lib.genAttrs systems;
  20. pkgsFor = system: nixpkgs.legacyPackages.${system};
  21. packageJson = builtins.fromJSON (builtins.readFile ./packages/opencode/package.json);
  22. bunTarget = {
  23. "aarch64-linux" = "bun-linux-arm64";
  24. "x86_64-linux" = "bun-linux-x64";
  25. "aarch64-darwin" = "bun-darwin-arm64";
  26. "x86_64-darwin" = "bun-darwin-x64";
  27. };
  28. # Parse "bun-{os}-{cpu}" to {os, cpu}
  29. parseBunTarget =
  30. target:
  31. let
  32. parts = lib.splitString "-" target;
  33. in
  34. {
  35. os = builtins.elemAt parts 1;
  36. cpu = builtins.elemAt parts 2;
  37. };
  38. hashesFile = "${./nix}/hashes.json";
  39. hashesData =
  40. if builtins.pathExists hashesFile then builtins.fromJSON (builtins.readFile hashesFile) else { };
  41. # Lookup hash: supports per-system ({system: hash}) or legacy single hash
  42. nodeModulesHashFor =
  43. system:
  44. if builtins.isAttrs hashesData.nodeModules then
  45. hashesData.nodeModules.${system}
  46. else
  47. hashesData.nodeModules;
  48. modelsDev = forEachSystem (
  49. system:
  50. let
  51. pkgs = pkgsFor system;
  52. in
  53. pkgs."models-dev"
  54. );
  55. in
  56. {
  57. devShells = forEachSystem (
  58. system:
  59. let
  60. pkgs = pkgsFor system;
  61. in
  62. {
  63. default = pkgs.mkShell {
  64. packages = with pkgs; [
  65. bun
  66. nodejs_20
  67. pkg-config
  68. openssl
  69. git
  70. ];
  71. };
  72. }
  73. );
  74. packages = forEachSystem (
  75. system:
  76. let
  77. pkgs = pkgsFor system;
  78. bunPlatform = parseBunTarget bunTarget.${system};
  79. mkNodeModules = pkgs.callPackage ./nix/node-modules.nix {
  80. hash = nodeModulesHashFor system;
  81. bunCpu = bunPlatform.cpu;
  82. bunOs = bunPlatform.os;
  83. };
  84. mkOpencode = pkgs.callPackage ./nix/opencode.nix { };
  85. mkDesktop = pkgs.callPackage ./nix/desktop.nix { };
  86. opencodePkg = mkOpencode {
  87. inherit (packageJson) version;
  88. src = ./.;
  89. scripts = ./nix/scripts;
  90. target = bunTarget.${system};
  91. modelsDev = "${modelsDev.${system}}/dist/_api.json";
  92. inherit mkNodeModules;
  93. };
  94. desktopPkg = mkDesktop {
  95. inherit (packageJson) version;
  96. src = ./.;
  97. scripts = ./nix/scripts;
  98. mkNodeModules = mkNodeModules;
  99. opencode = opencodePkg;
  100. };
  101. in
  102. {
  103. default = opencodePkg;
  104. desktop = desktopPkg;
  105. }
  106. );
  107. apps = forEachSystem (
  108. system:
  109. let
  110. pkgs = pkgsFor system;
  111. in
  112. {
  113. opencode-dev = {
  114. type = "app";
  115. meta = {
  116. description = "Nix devshell shell for OpenCode";
  117. runtimeInputs = [ pkgs.bun ];
  118. };
  119. program = "${
  120. pkgs.writeShellApplication {
  121. name = "opencode-dev";
  122. text = ''
  123. exec bun run dev "$@"
  124. '';
  125. }
  126. }/bin/opencode-dev";
  127. };
  128. }
  129. );
  130. };
  131. }