flake.nix 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. 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. ${_shellHook}
  160. ";
  161. buildInputs = buildInputs ++ [ msrv_toolchain ];
  162. inherit nativeBuildInputs;
  163. }
  164. // envVars
  165. );
  166. stable = pkgs.mkShell (
  167. {
  168. shellHook = ''${_shellHook}'';
  169. buildInputs = buildInputs ++ [ stable_toolchain ];
  170. inherit nativeBuildInputs;
  171. }
  172. // envVars
  173. );
  174. nightly = pkgs.mkShell (
  175. {
  176. shellHook = ''
  177. ${_shellHook}
  178. # Needed for github ci
  179. export LD_LIBRARY_PATH=${
  180. pkgs.lib.makeLibraryPath [
  181. pkgs.zlib
  182. ]
  183. }:$LD_LIBRARY_PATH
  184. '';
  185. buildInputs = buildInputs ++ [ nightly_toolchain ];
  186. inherit nativeBuildInputs;
  187. }
  188. // envVars
  189. );
  190. # Shell with Docker for integration tests
  191. integration = pkgs.mkShell (
  192. {
  193. shellHook = ''
  194. ${_shellHook}
  195. # Ensure Docker is available
  196. if ! command -v docker &> /dev/null; then
  197. echo "Docker is not installed or not in PATH"
  198. echo "Please install Docker to run integration tests"
  199. exit 1
  200. fi
  201. echo "Docker is available at $(which docker)"
  202. echo "Docker version: $(docker --version)"
  203. '';
  204. buildInputs = buildInputs ++ [
  205. stable_toolchain
  206. pkgs.docker-client
  207. ];
  208. inherit nativeBuildInputs;
  209. }
  210. // envVars
  211. );
  212. in
  213. {
  214. inherit
  215. msrv
  216. stable
  217. nightly
  218. integration
  219. ;
  220. default = stable;
  221. };
  222. }
  223. );
  224. }