flake.nix 3.0 KB

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