flake.nix 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.91.1".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. mprocs
  101. cargo-outdated
  102. cargo-mutants
  103. # Needed for github ci
  104. libz
  105. ]
  106. ++ libsDarwin;
  107. # Common arguments can be set here to avoid repeating them later
  108. nativeBuildInputs = [
  109. #Add additional build inputs here
  110. ]
  111. ++ lib.optionals isDarwin [
  112. # Additional darwin specific native inputs can be set here
  113. ];
  114. in
  115. {
  116. checks = {
  117. # Pre-commit checks
  118. pre-commit-check =
  119. let
  120. # this is a hack based on https://github.com/cachix/pre-commit-hooks.nix/issues/126
  121. # we want to use our own rust stuff from oxalica's overlay
  122. _rust = pkgs.rust-bin.stable.latest.default;
  123. rust = pkgs.buildEnv {
  124. name = _rust.name;
  125. inherit (_rust) meta;
  126. buildInputs = [ pkgs.makeWrapper ];
  127. paths = [ _rust ];
  128. pathsToLink = [
  129. "/"
  130. "/bin"
  131. ];
  132. postBuild = ''
  133. for i in $out/bin/*; do
  134. wrapProgram "$i" --prefix PATH : "$out/bin"
  135. done
  136. '';
  137. };
  138. in
  139. pre-commit-hooks.lib.${system}.run {
  140. src = ./.;
  141. hooks = {
  142. rustfmt = {
  143. enable = true;
  144. entry = lib.mkForce "${rust}/bin/cargo-fmt fmt --all -- --config format_code_in_doc_comments=true --check --color always";
  145. };
  146. nixpkgs-fmt.enable = true;
  147. typos.enable = true;
  148. commitizen.enable = true; # conventional commits
  149. };
  150. };
  151. };
  152. devShells =
  153. let
  154. # pre-commit-checks
  155. _shellHook = (self.checks.${system}.pre-commit-check.shellHook or "");
  156. # devShells
  157. msrv = pkgs.mkShell (
  158. {
  159. shellHook = "
  160. cargo update
  161. cargo update home --precise 0.5.11
  162. ${_shellHook}
  163. ";
  164. buildInputs = buildInputs ++ [ msrv_toolchain ];
  165. inherit nativeBuildInputs;
  166. }
  167. // envVars
  168. );
  169. stable = pkgs.mkShell (
  170. {
  171. shellHook = ''
  172. ${_shellHook}
  173. # Needed for github ci
  174. export LD_LIBRARY_PATH=${
  175. pkgs.lib.makeLibraryPath [
  176. pkgs.zlib
  177. ]
  178. }:$LD_LIBRARY_PATH
  179. '';
  180. buildInputs = buildInputs ++ [ stable_toolchain ];
  181. inherit nativeBuildInputs;
  182. }
  183. // envVars
  184. );
  185. nightly = pkgs.mkShell (
  186. {
  187. shellHook = ''
  188. ${_shellHook}
  189. # Needed for github ci
  190. export LD_LIBRARY_PATH=${
  191. pkgs.lib.makeLibraryPath [
  192. pkgs.zlib
  193. ]
  194. }:$LD_LIBRARY_PATH
  195. '';
  196. buildInputs = buildInputs ++ [ nightly_toolchain ];
  197. inherit nativeBuildInputs;
  198. }
  199. // envVars
  200. );
  201. # Shell with Docker for integration tests
  202. integration = pkgs.mkShell (
  203. {
  204. shellHook = ''
  205. ${_shellHook}
  206. # Ensure Docker is available
  207. if ! command -v docker &> /dev/null; then
  208. echo "Docker is not installed or not in PATH"
  209. echo "Please install Docker to run integration tests"
  210. exit 1
  211. fi
  212. echo "Docker is available at $(which docker)"
  213. echo "Docker version: $(docker --version)"
  214. '';
  215. buildInputs = buildInputs ++ [
  216. stable_toolchain
  217. pkgs.docker-client
  218. ];
  219. inherit nativeBuildInputs;
  220. }
  221. // envVars
  222. );
  223. in
  224. {
  225. inherit
  226. msrv
  227. stable
  228. nightly
  229. integration
  230. ;
  231. default = stable;
  232. };
  233. }
  234. );
  235. }