flake.nix 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. inputs.nixpkgs.follows = "nixpkgs";
  20. };
  21. pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
  22. };
  23. outputs = { self, nixpkgs, rust-overlay, flake-utils, pre-commit-hooks, ... }:
  24. flake-utils.lib.eachDefaultSystem (system:
  25. let
  26. overlays = [ (import rust-overlay) ];
  27. lib = pkgs.lib;
  28. stdenv = pkgs.stdenv;
  29. isDarwin = stdenv.isDarwin;
  30. libsDarwin = with pkgs; lib.optionals isDarwin [
  31. # Additional darwin specific inputs can be set here
  32. darwin.apple_sdk.frameworks.Security
  33. darwin.apple_sdk.frameworks.SystemConfiguration
  34. ];
  35. # Dependencies
  36. pkgs = import nixpkgs {
  37. inherit system overlays;
  38. };
  39. # Toolchains
  40. # latest stable
  41. stable_toolchain = pkgs.rust-bin.stable."1.86.0".default.override {
  42. targets = [ "wasm32-unknown-unknown" ]; # wasm
  43. extensions = [ "rustfmt" "clippy" "rust-analyzer" ];
  44. };
  45. # MSRV stable
  46. msrv_toolchain = pkgs.rust-bin.stable."1.75.0".default.override {
  47. targets = [ "wasm32-unknown-unknown" ]; # wasm
  48. };
  49. # Nightly used for formatting
  50. nightly_toolchain = pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override {
  51. extensions = [ "rustfmt" "clippy" "rust-analyzer" "rust-src" ];
  52. targets = [ "wasm32-unknown-unknown" ]; # wasm
  53. });
  54. # Common inputs
  55. envVars = { };
  56. buildInputs = with pkgs; [
  57. # Add additional build inputs here
  58. git
  59. pkg-config
  60. curl
  61. just
  62. protobuf
  63. nixpkgs-fmt
  64. typos
  65. lnd
  66. clightning
  67. bitcoind
  68. sqlx-cli
  69. cargo-outdated
  70. mprocs
  71. # Needed for github ci
  72. libz
  73. ] ++ libsDarwin;
  74. # Common arguments can be set here to avoid repeating them later
  75. nativeBuildInputs = [
  76. #Add additional build inputs here
  77. ] ++ lib.optionals isDarwin [
  78. # Additional darwin specific native inputs can be set here
  79. ];
  80. in
  81. {
  82. checks = {
  83. # Pre-commit checks
  84. pre-commit-check =
  85. let
  86. # this is a hack based on https://github.com/cachix/pre-commit-hooks.nix/issues/126
  87. # we want to use our own rust stuff from oxalica's overlay
  88. _rust = pkgs.rust-bin.stable.latest.default;
  89. rust = pkgs.buildEnv {
  90. name = _rust.name;
  91. inherit (_rust) meta;
  92. buildInputs = [ pkgs.makeWrapper ];
  93. paths = [ _rust ];
  94. pathsToLink = [ "/" "/bin" ];
  95. postBuild = ''
  96. for i in $out/bin/*; do
  97. wrapProgram "$i" --prefix PATH : "$out/bin"
  98. done
  99. '';
  100. };
  101. in
  102. pre-commit-hooks.lib.${system}.run {
  103. src = ./.;
  104. hooks = {
  105. rustfmt = {
  106. enable = true;
  107. entry = lib.mkForce "${rust}/bin/cargo-fmt fmt --all -- --config format_code_in_doc_comments=true --check --color always";
  108. };
  109. nixpkgs-fmt.enable = true;
  110. typos.enable = true;
  111. commitizen.enable = true; # conventional commits
  112. };
  113. };
  114. };
  115. devShells =
  116. let
  117. # pre-commit-checks
  118. _shellHook = (self.checks.${system}.pre-commit-check.shellHook or "");
  119. # devShells
  120. msrv = pkgs.mkShell ({
  121. shellHook = "
  122. ${_shellHook}
  123. cargo update
  124. # cargo update -p async-compression --precise 0.4.3
  125. cargo update -p home --precise 0.5.9
  126. cargo update -p zerofrom --precise 0.1.5
  127. cargo update -p half --precise 2.4.1
  128. cargo update -p base64ct --precise 1.7.3
  129. cargo update -p url --precise 2.5.2
  130. cargo update -p pest_derive --precise 2.8.0
  131. cargo update -p pest_generator --precise 2.8.0
  132. cargo update -p pest_meta --precise 2.8.0
  133. cargo update -p pest --precise 2.8.0
  134. ";
  135. buildInputs = buildInputs ++ [ msrv_toolchain ];
  136. inherit nativeBuildInputs;
  137. } // envVars);
  138. stable = pkgs.mkShell ({
  139. shellHook = ''${_shellHook}'';
  140. buildInputs = buildInputs ++ [ stable_toolchain ];
  141. inherit nativeBuildInputs;
  142. } // envVars);
  143. nightly = pkgs.mkShell ({
  144. shellHook = ''
  145. ${_shellHook}
  146. # Needed for github ci
  147. export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath [
  148. pkgs.zlib
  149. ]}:$LD_LIBRARY_PATH
  150. export RUST_SRC_PATH=${nightly_toolchain}/lib/rustlib/src/rust/library
  151. '';
  152. buildInputs = buildInputs ++ [ nightly_toolchain ];
  153. inherit nativeBuildInputs;
  154. } // envVars);
  155. # Shell with Docker for integration tests
  156. integration = pkgs.mkShell ({
  157. shellHook = ''
  158. ${_shellHook}
  159. # Ensure Docker is available
  160. if ! command -v docker &> /dev/null; then
  161. echo "Docker is not installed or not in PATH"
  162. echo "Please install Docker to run integration tests"
  163. exit 1
  164. fi
  165. echo "Docker is available at $(which docker)"
  166. echo "Docker version: $(docker --version)"
  167. '';
  168. buildInputs = buildInputs ++ [
  169. stable_toolchain
  170. pkgs.docker-client
  171. ];
  172. inherit nativeBuildInputs;
  173. } // envVars);
  174. in
  175. {
  176. inherit msrv stable nightly integration;
  177. default = stable;
  178. };
  179. }
  180. );
  181. }