flake.nix 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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, crane, fenix, ... }:
  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. # WASM deps
  75. WASMInputs = with pkgs; [
  76. ];
  77. craneLib = crane.mkLib pkgs;
  78. src = craneLib.cleanCargoSource ./.;
  79. # Common arguments can be set here to avoid repeating them later
  80. commonArgs = {
  81. inherit src;
  82. strictDeps = true;
  83. buildInputs = [
  84. # Add additional build inputs here
  85. pkgs.protobuf
  86. pkgs.pkg-config
  87. ] ++ lib.optionals pkgs.stdenv.isDarwin [
  88. # Additional darwin specific inputs can be set here
  89. pkgs.libiconv
  90. ];
  91. # Additional environment variables can be set directly
  92. # MY_CUSTOM_VAR = "some value";
  93. PROTOC = "${pkgs.protobuf}/bin/protoc";
  94. PROTOC_INCLUDE = "${pkgs.protobuf}/include";
  95. };
  96. craneLibLLvmTools = craneLib.overrideToolchain
  97. (fenix.packages.${system}.complete.withComponents [
  98. "cargo"
  99. "llvm-tools"
  100. "rustc"
  101. ]);
  102. cargoArtifacts = craneLib.buildDepsOnly commonArgs;
  103. individualCrateArgs = commonArgs // {
  104. inherit cargoArtifacts;
  105. inherit (craneLib.crateNameFromCargoToml { inherit src; }) version;
  106. # NB: we disable tests since we'll run them all via cargo-nextest
  107. doCheck = false;
  108. };
  109. fileSetForCrate = crate: lib.fileset.toSource {
  110. root = ./.;
  111. fileset = lib.fileset.unions [
  112. ./Cargo.toml
  113. ./Cargo.lock
  114. (craneLib.fileset.commonCargoSources ./crates/cdk)
  115. (craneLib.fileset.commonCargoSources ./crates/cdk-axum)
  116. (craneLib.fileset.commonCargoSources ./crates/cdk-cln)
  117. (craneLib.fileset.commonCargoSources ./crates/cdk-lnd)
  118. (craneLib.fileset.commonCargoSources ./crates/cdk-fake-wallet)
  119. (craneLib.fileset.commonCargoSources ./crates/cdk-lnbits)
  120. (craneLib.fileset.commonCargoSources ./crates/cdk-redb)
  121. (craneLib.fileset.commonCargoSources ./crates/cdk-sqlite)
  122. ./crates/cdk-sqlite/src/mint/migrations
  123. ./crates/cdk-sqlite/src/wallet/migrations
  124. (craneLib.fileset.commonCargoSources crate)
  125. ];
  126. };
  127. cdk-mintd = craneLib.buildPackage (individualCrateArgs // {
  128. pname = "cdk-mintd";
  129. name = "cdk-mintd-${individualCrateArgs.version}";
  130. cargoExtraArgs = "-p cdk-mintd";
  131. src = fileSetForCrate ./crates/cdk-mintd;
  132. });
  133. nativeBuildInputs = with pkgs; [
  134. #Add additional build inputs here
  135. ] ++ lib.optionals isDarwin [
  136. # Additional darwin specific native inputs can be set here
  137. ];
  138. in
  139. {
  140. checks = {
  141. # Pre-commit checks
  142. pre-commit-check =
  143. let
  144. # this is a hack based on https://github.com/cachix/pre-commit-hooks.nix/issues/126
  145. # we want to use our own rust stuff from oxalica's overlay
  146. _rust = pkgs.rust-bin.stable.latest.default;
  147. rust = pkgs.buildEnv {
  148. name = _rust.name;
  149. inherit (_rust) meta;
  150. buildInputs = [ pkgs.makeWrapper ];
  151. paths = [ _rust ];
  152. pathsToLink = [ "/" "/bin" ];
  153. postBuild = ''
  154. for i in $out/bin/*; do
  155. wrapProgram "$i" --prefix PATH : "$out/bin"
  156. done
  157. '';
  158. };
  159. in
  160. pre-commit-hooks.lib.${system}.run {
  161. src = ./.;
  162. hooks = {
  163. rustfmt = {
  164. enable = true;
  165. entry = lib.mkForce "${rust}/bin/cargo-fmt fmt --all -- --config format_code_in_doc_comments=true --check --color always";
  166. };
  167. nixpkgs-fmt.enable = true;
  168. typos.enable = true;
  169. commitizen.enable = true; # conventional commits
  170. };
  171. };
  172. };
  173. packages = {
  174. inherit cdk-mintd;
  175. default = cdk-mintd;
  176. } // lib.optionalAttrs (!pkgs.stdenv.isDarwin) {
  177. my-workspace-llvm-coverage = craneLibLLvmTools.cargoLlvmCov (commonArgs // {
  178. inherit cargoArtifacts;
  179. });
  180. };
  181. apps = {
  182. cdk-mintd = flake-utils.lib.mkApp {
  183. drv = cdk-mintd;
  184. };
  185. };
  186. devShells =
  187. let
  188. # pre-commit-checks
  189. _shellHook = (self.checks.${system}.pre-commit-check.shellHook or "");
  190. # devShells
  191. msrv = pkgs.mkShell ({
  192. shellHook = "
  193. ${_shellHook}
  194. cargo update
  195. # cargo update -p async-compression --precise 0.4.3
  196. cargo update -p home --precise 0.5.9
  197. cargo update -p zerofrom --precise 0.1.5
  198. cargo update -p half --precise 2.4.1
  199. cargo update -p base64ct --precise 1.7.3
  200. cargo update -p url --precise 2.5.2
  201. cargo update -p pest_derive --precise 2.8.0
  202. cargo update -p pest_generator --precise 2.8.0
  203. cargo update -p pest_meta --precise 2.8.0
  204. cargo update -p pest --precise 2.8.0
  205. ";
  206. buildInputs = buildInputs ++ WASMInputs ++ [ msrv_toolchain ];
  207. inherit nativeBuildInputs;
  208. } // envVars);
  209. stable = pkgs.mkShell ({
  210. shellHook = ''${_shellHook}'';
  211. buildInputs = buildInputs ++ WASMInputs ++ [ stable_toolchain ];
  212. inherit nativeBuildInputs;
  213. } // envVars);
  214. nightly = pkgs.mkShell ({
  215. shellHook = ''
  216. ${_shellHook}
  217. # Needed for github ci
  218. export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath [
  219. pkgs.zlib
  220. ]}:$LD_LIBRARY_PATH
  221. export RUST_SRC_PATH=${nightly_toolchain}/lib/rustlib/src/rust/library
  222. '';
  223. buildInputs = buildInputs ++ [ nightly_toolchain ];
  224. inherit nativeBuildInputs;
  225. } // envVars);
  226. # Shell with Docker for integration tests
  227. integration = pkgs.mkShell ({
  228. shellHook = ''
  229. ${_shellHook}
  230. # Ensure Docker is available
  231. if ! command -v docker &> /dev/null; then
  232. echo "Docker is not installed or not in PATH"
  233. echo "Please install Docker to run integration tests"
  234. exit 1
  235. fi
  236. echo "Docker is available at $(which docker)"
  237. echo "Docker version: $(docker --version)"
  238. '';
  239. buildInputs = buildInputs ++ [
  240. stable_toolchain
  241. pkgs.docker-client
  242. ];
  243. inherit nativeBuildInputs;
  244. } // envVars);
  245. in
  246. {
  247. inherit msrv stable nightly integration;
  248. default = stable;
  249. };
  250. }
  251. );
  252. }