flake.nix 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.85.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. ";
  124. buildInputs = buildInputs ++ [ msrv_toolchain ];
  125. inherit nativeBuildInputs;
  126. } // envVars);
  127. stable = pkgs.mkShell ({
  128. shellHook = ''${_shellHook}'';
  129. buildInputs = buildInputs ++ [ stable_toolchain ];
  130. inherit nativeBuildInputs;
  131. } // envVars);
  132. nightly = pkgs.mkShell ({
  133. shellHook = ''
  134. ${_shellHook}
  135. # Needed for github ci
  136. export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath [
  137. pkgs.zlib
  138. ]}:$LD_LIBRARY_PATH
  139. export RUST_SRC_PATH=${nightly_toolchain}/lib/rustlib/src/rust/library
  140. '';
  141. buildInputs = buildInputs ++ [ nightly_toolchain ];
  142. inherit nativeBuildInputs;
  143. } // envVars);
  144. # Shell with Docker for integration tests
  145. integration = pkgs.mkShell ({
  146. shellHook = ''
  147. ${_shellHook}
  148. # Ensure Docker is available
  149. if ! command -v docker &> /dev/null; then
  150. echo "Docker is not installed or not in PATH"
  151. echo "Please install Docker to run integration tests"
  152. exit 1
  153. fi
  154. echo "Docker is available at $(which docker)"
  155. echo "Docker version: $(docker --version)"
  156. '';
  157. buildInputs = buildInputs ++ [
  158. stable_toolchain
  159. pkgs.docker-client
  160. ];
  161. inherit nativeBuildInputs;
  162. } // envVars);
  163. in
  164. {
  165. inherit msrv stable nightly integration;
  166. default = stable;
  167. };
  168. }
  169. );
  170. }