flake.nix 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.86.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. };
  62. # Nightly used for formatting
  63. nightly_toolchain = pkgs.rust-bin.selectLatestNightlyWith (
  64. toolchain:
  65. toolchain.default.override {
  66. extensions = [
  67. "rustfmt"
  68. "clippy"
  69. "rust-analyzer"
  70. "rust-src"
  71. ];
  72. targets = [ "wasm32-unknown-unknown" ]; # wasm
  73. }
  74. );
  75. # Common inputs
  76. envVars = {
  77. # rust analyzer needs NIX_PATH for some reason.
  78. NIX_PATH = "nixpkgs=${inputs.nixpkgs}";
  79. };
  80. buildInputs =
  81. with pkgs;
  82. [
  83. # Add additional build inputs here
  84. git
  85. pkg-config
  86. curl
  87. just
  88. protobuf
  89. nixpkgs-fmt
  90. typos
  91. lnd
  92. clightning
  93. bitcoind
  94. sqlx-cli
  95. cargo-outdated
  96. mprocs
  97. # Needed for github ci
  98. libz
  99. ]
  100. ++ libsDarwin;
  101. # Common arguments can be set here to avoid repeating them later
  102. nativeBuildInputs = [
  103. pkgs.rust-analyzer
  104. #Add additional build inputs here
  105. ]
  106. ++ lib.optionals isDarwin [
  107. # Additional darwin specific native inputs can be set here
  108. ];
  109. in
  110. {
  111. checks = {
  112. # Pre-commit checks
  113. pre-commit-check =
  114. let
  115. # this is a hack based on https://github.com/cachix/pre-commit-hooks.nix/issues/126
  116. # we want to use our own rust stuff from oxalica's overlay
  117. _rust = pkgs.rust-bin.stable.latest.default;
  118. rust = pkgs.buildEnv {
  119. name = _rust.name;
  120. inherit (_rust) meta;
  121. buildInputs = [ pkgs.makeWrapper ];
  122. paths = [ _rust ];
  123. pathsToLink = [
  124. "/"
  125. "/bin"
  126. ];
  127. postBuild = ''
  128. for i in $out/bin/*; do
  129. wrapProgram "$i" --prefix PATH : "$out/bin"
  130. done
  131. '';
  132. };
  133. in
  134. pre-commit-hooks.lib.${system}.run {
  135. src = ./.;
  136. hooks = {
  137. rustfmt = {
  138. enable = true;
  139. entry = lib.mkForce "${rust}/bin/cargo-fmt fmt --all -- --config format_code_in_doc_comments=true --check --color always";
  140. };
  141. nixpkgs-fmt.enable = true;
  142. typos.enable = true;
  143. commitizen.enable = true; # conventional commits
  144. };
  145. };
  146. };
  147. devShells =
  148. let
  149. # pre-commit-checks
  150. _shellHook = (self.checks.${system}.pre-commit-check.shellHook or "");
  151. # devShells
  152. msrv = pkgs.mkShell (
  153. {
  154. shellHook = "
  155. ${_shellHook}
  156. ";
  157. buildInputs = buildInputs ++ [ msrv_toolchain ];
  158. inherit nativeBuildInputs;
  159. }
  160. // envVars
  161. );
  162. stable = pkgs.mkShell (
  163. {
  164. shellHook = ''${_shellHook}'';
  165. buildInputs = buildInputs ++ [ stable_toolchain ];
  166. inherit nativeBuildInputs;
  167. }
  168. // envVars
  169. );
  170. nightly = pkgs.mkShell (
  171. {
  172. shellHook = ''
  173. ${_shellHook}
  174. # Needed for github ci
  175. export LD_LIBRARY_PATH=${
  176. pkgs.lib.makeLibraryPath [
  177. pkgs.zlib
  178. ]
  179. }:$LD_LIBRARY_PATH
  180. '';
  181. buildInputs = buildInputs ++ [ nightly_toolchain ];
  182. inherit nativeBuildInputs;
  183. }
  184. // envVars
  185. );
  186. # Shell with Docker for integration tests
  187. integration = pkgs.mkShell (
  188. {
  189. shellHook = ''
  190. ${_shellHook}
  191. # Ensure Docker is available
  192. if ! command -v docker &> /dev/null; then
  193. echo "Docker is not installed or not in PATH"
  194. echo "Please install Docker to run integration tests"
  195. exit 1
  196. fi
  197. echo "Docker is available at $(which docker)"
  198. echo "Docker version: $(docker --version)"
  199. '';
  200. buildInputs = buildInputs ++ [
  201. stable_toolchain
  202. pkgs.docker-client
  203. ];
  204. inherit nativeBuildInputs;
  205. }
  206. // envVars
  207. );
  208. in
  209. {
  210. inherit
  211. msrv
  212. stable
  213. nightly
  214. integration
  215. ;
  216. default = stable;
  217. };
  218. }
  219. );
  220. }