flake.nix 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. {
  2. description = "CDK Flake";
  3. inputs = {
  4. nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
  5. rust-overlay = {
  6. url = "github:oxalica/rust-overlay";
  7. inputs = {
  8. nixpkgs.follows = "nixpkgs";
  9. };
  10. };
  11. fenix = {
  12. url = "github:nix-community/fenix";
  13. inputs.nixpkgs.follows = "nixpkgs";
  14. inputs.rust-analyzer-src.follows = "";
  15. };
  16. flake-utils.url = "github:numtide/flake-utils";
  17. crane = {
  18. url = "github:ipetkov/crane";
  19. };
  20. pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
  21. };
  22. outputs =
  23. { self
  24. , nixpkgs
  25. , rust-overlay
  26. , flake-utils
  27. , pre-commit-hooks
  28. , ...
  29. }@inputs:
  30. flake-utils.lib.eachDefaultSystem (
  31. system:
  32. let
  33. overlays = [ (import rust-overlay) ];
  34. lib = pkgs.lib;
  35. stdenv = pkgs.stdenv;
  36. isDarwin = stdenv.isDarwin;
  37. libsDarwin =
  38. with pkgs;
  39. lib.optionals isDarwin [
  40. # Additional darwin specific inputs can be set here
  41. darwin.apple_sdk.frameworks.Security
  42. darwin.apple_sdk.frameworks.SystemConfiguration
  43. ];
  44. # Dependencies
  45. pkgs = import nixpkgs {
  46. inherit system overlays;
  47. };
  48. # Toolchains
  49. # latest stable
  50. stable_toolchain = pkgs.rust-bin.stable."1.90.0".default.override {
  51. targets = [ "wasm32-unknown-unknown" ]; # wasm
  52. extensions = [
  53. "rustfmt"
  54. "clippy"
  55. "rust-analyzer"
  56. ];
  57. };
  58. # MSRV stable
  59. msrv_toolchain = pkgs.rust-bin.stable."1.85.0".default.override {
  60. targets = [ "wasm32-unknown-unknown" ]; # wasm
  61. extensions = [
  62. "rustfmt"
  63. "clippy"
  64. "rust-analyzer"
  65. ];
  66. };
  67. # Nightly used for formatting
  68. nightly_toolchain = pkgs.rust-bin.selectLatestNightlyWith (
  69. toolchain:
  70. toolchain.default.override {
  71. extensions = [
  72. "rustfmt"
  73. "clippy"
  74. "rust-analyzer"
  75. "rust-src"
  76. ];
  77. targets = [ "wasm32-unknown-unknown" ]; # wasm
  78. }
  79. );
  80. # Common inputs
  81. envVars = {
  82. # rust analyzer needs NIX_PATH for some reason.
  83. NIX_PATH = "nixpkgs=${inputs.nixpkgs}";
  84. };
  85. buildInputs =
  86. with pkgs;
  87. [
  88. # Add additional build inputs here
  89. git
  90. pkg-config
  91. curl
  92. just
  93. protobuf
  94. nixpkgs-fmt
  95. typos
  96. lnd
  97. clightning
  98. bitcoind
  99. sqlx-cli
  100. cargo-outdated
  101. mprocs
  102. # Needed for github ci
  103. libz
  104. ]
  105. ++ libsDarwin;
  106. # Common arguments can be set here to avoid repeating them later
  107. nativeBuildInputs = [
  108. #Add additional build inputs here
  109. ]
  110. ++ lib.optionals isDarwin [
  111. # Additional darwin specific native inputs can be set here
  112. ];
  113. in
  114. {
  115. checks = {
  116. # Pre-commit checks
  117. pre-commit-check =
  118. let
  119. # this is a hack based on https://github.com/cachix/pre-commit-hooks.nix/issues/126
  120. # we want to use our own rust stuff from oxalica's overlay
  121. _rust = pkgs.rust-bin.stable.latest.default;
  122. rust = pkgs.buildEnv {
  123. name = _rust.name;
  124. inherit (_rust) meta;
  125. buildInputs = [ pkgs.makeWrapper ];
  126. paths = [ _rust ];
  127. pathsToLink = [
  128. "/"
  129. "/bin"
  130. ];
  131. postBuild = ''
  132. for i in $out/bin/*; do
  133. wrapProgram "$i" --prefix PATH : "$out/bin"
  134. done
  135. '';
  136. };
  137. in
  138. pre-commit-hooks.lib.${system}.run {
  139. src = ./.;
  140. hooks = {
  141. rustfmt = {
  142. enable = true;
  143. entry = lib.mkForce "${rust}/bin/cargo-fmt fmt --all -- --config format_code_in_doc_comments=true --check --color always";
  144. };
  145. nixpkgs-fmt.enable = true;
  146. typos.enable = true;
  147. commitizen.enable = true; # conventional commits
  148. };
  149. };
  150. };
  151. devShells =
  152. let
  153. # pre-commit-checks
  154. _shellHook = (self.checks.${system}.pre-commit-check.shellHook or "");
  155. # devShells
  156. msrv = pkgs.mkShell (
  157. {
  158. shellHook = "
  159. cargo update
  160. cargo update home --precise 0.5.11
  161. ${_shellHook}
  162. ";
  163. buildInputs = buildInputs ++ [ msrv_toolchain ];
  164. inherit nativeBuildInputs;
  165. }
  166. // envVars
  167. );
  168. stable = pkgs.mkShell (
  169. {
  170. shellHook = ''${_shellHook}'';
  171. buildInputs = buildInputs ++ [ stable_toolchain ];
  172. inherit nativeBuildInputs;
  173. }
  174. // envVars
  175. );
  176. nightly = pkgs.mkShell (
  177. {
  178. shellHook = ''
  179. ${_shellHook}
  180. # Needed for github ci
  181. export LD_LIBRARY_PATH=${
  182. pkgs.lib.makeLibraryPath [
  183. pkgs.zlib
  184. ]
  185. }:$LD_LIBRARY_PATH
  186. '';
  187. buildInputs = buildInputs ++ [ nightly_toolchain ];
  188. inherit nativeBuildInputs;
  189. }
  190. // envVars
  191. );
  192. # Shell with Docker for integration tests
  193. integration = pkgs.mkShell (
  194. {
  195. shellHook = ''
  196. ${_shellHook}
  197. # Ensure Docker is available
  198. if ! command -v docker &> /dev/null; then
  199. echo "Docker is not installed or not in PATH"
  200. echo "Please install Docker to run integration tests"
  201. exit 1
  202. fi
  203. echo "Docker is available at $(which docker)"
  204. echo "Docker version: $(docker --version)"
  205. '';
  206. buildInputs = buildInputs ++ [
  207. stable_toolchain
  208. pkgs.docker-client
  209. ];
  210. inherit nativeBuildInputs;
  211. }
  212. // envVars
  213. );
  214. in
  215. {
  216. inherit
  217. msrv
  218. stable
  219. nightly
  220. integration
  221. ;
  222. default = stable;
  223. };
  224. }
  225. );
  226. }