flake.nix 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. {
  2. description = "CDK Flake";
  3. inputs = {
  4. nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.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.82.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.63.0".default.override {
  47. targets = [ "wasm32-unknown-unknown" ]; # wasm
  48. };
  49. # DB MSRV stable
  50. db_msrv_toolchain = pkgs.rust-bin.stable."1.66.0".default.override {
  51. targets = [ "wasm32-unknown-unknown" ]; # wasm
  52. };
  53. # Nightly for creating lock files
  54. nightly_toolchain = pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override {
  55. extensions = [ "rustfmt" "clippy" "rust-analyzer" ];
  56. });
  57. # Common inputs
  58. envVars = { };
  59. buildInputs = with pkgs; [
  60. # Add additional build inputs here
  61. git
  62. pkg-config
  63. curl
  64. just
  65. protobuf
  66. nixpkgs-fmt
  67. typos
  68. lnd
  69. clightning
  70. bitcoind
  71. sqlx-cli
  72. # Needed for github ci
  73. libz
  74. ] ++ libsDarwin;
  75. # WASM deps
  76. WASMInputs = with pkgs; [
  77. ];
  78. craneLib = crane.mkLib pkgs;
  79. src = craneLib.cleanCargoSource ./.;
  80. # Common arguments can be set here to avoid repeating them later
  81. commonArgs = {
  82. inherit src;
  83. strictDeps = true;
  84. buildInputs = [
  85. # Add additional build inputs here
  86. pkgs.protobuf
  87. pkgs.pkg-config
  88. ] ++ lib.optionals pkgs.stdenv.isDarwin [
  89. # Additional darwin specific inputs can be set here
  90. pkgs.libiconv
  91. ];
  92. # Additional environment variables can be set directly
  93. # MY_CUSTOM_VAR = "some value";
  94. PROTOC = "${pkgs.protobuf}/bin/protoc";
  95. PROTOC_INCLUDE = "${pkgs.protobuf}/include";
  96. };
  97. craneLibLLvmTools = craneLib.overrideToolchain
  98. (fenix.packages.${system}.complete.withComponents [
  99. "cargo"
  100. "llvm-tools"
  101. "rustc"
  102. ]);
  103. cargoArtifacts = craneLib.buildDepsOnly commonArgs;
  104. individualCrateArgs = commonArgs // {
  105. inherit cargoArtifacts;
  106. inherit (craneLib.crateNameFromCargoToml { inherit src; }) version;
  107. # NB: we disable tests since we'll run them all via cargo-nextest
  108. doCheck = false;
  109. };
  110. fileSetForCrate = crate: lib.fileset.toSource {
  111. root = ./.;
  112. fileset = lib.fileset.unions [
  113. ./Cargo.toml
  114. ./Cargo.lock
  115. (craneLib.fileset.commonCargoSources ./crates/cdk)
  116. (craneLib.fileset.commonCargoSources ./crates/cdk-axum)
  117. (craneLib.fileset.commonCargoSources ./crates/cdk-cln)
  118. (craneLib.fileset.commonCargoSources ./crates/cdk-lnd)
  119. (craneLib.fileset.commonCargoSources ./crates/cdk-fake-wallet)
  120. (craneLib.fileset.commonCargoSources ./crates/cdk-lnbits)
  121. (craneLib.fileset.commonCargoSources ./crates/cdk-strike)
  122. (craneLib.fileset.commonCargoSources ./crates/cdk-phoenixd)
  123. (craneLib.fileset.commonCargoSources ./crates/cdk-redb)
  124. (craneLib.fileset.commonCargoSources ./crates/cdk-sqlite)
  125. ./crates/cdk-sqlite/src/mint/migrations
  126. ./crates/cdk-sqlite/src/wallet/migrations
  127. (craneLib.fileset.commonCargoSources crate)
  128. ];
  129. };
  130. cdk-mintd = craneLib.buildPackage (individualCrateArgs // {
  131. pname = "cdk-mintd";
  132. name = "cdk-mintd-${individualCrateArgs.version}";
  133. cargoExtraArgs = "-p cdk-mintd";
  134. src = fileSetForCrate ./crates/cdk-mintd;
  135. });
  136. nativeBuildInputs = with pkgs; [
  137. #Add additional build inputs here
  138. ] ++ lib.optionals isDarwin [
  139. # Additional darwin specific native inputs can be set here
  140. ];
  141. in
  142. {
  143. checks = {
  144. # Pre-commit checks
  145. pre-commit-check =
  146. let
  147. # this is a hack based on https://github.com/cachix/pre-commit-hooks.nix/issues/126
  148. # we want to use our own rust stuff from oxalica's overlay
  149. _rust = pkgs.rust-bin.stable.latest.default;
  150. rust = pkgs.buildEnv {
  151. name = _rust.name;
  152. inherit (_rust) meta;
  153. buildInputs = [ pkgs.makeWrapper ];
  154. paths = [ _rust ];
  155. pathsToLink = [ "/" "/bin" ];
  156. postBuild = ''
  157. for i in $out/bin/*; do
  158. wrapProgram "$i" --prefix PATH : "$out/bin"
  159. done
  160. '';
  161. };
  162. in
  163. pre-commit-hooks.lib.${system}.run {
  164. src = ./.;
  165. hooks = {
  166. rustfmt = {
  167. enable = true;
  168. entry = lib.mkForce "${rust}/bin/cargo-fmt fmt --all -- --config format_code_in_doc_comments=true --check --color always";
  169. };
  170. nixpkgs-fmt.enable = true;
  171. typos.enable = true;
  172. commitizen.enable = true; # conventional commits
  173. };
  174. };
  175. };
  176. packages = {
  177. inherit cdk-mintd;
  178. default = cdk-mintd;
  179. } // lib.optionalAttrs (!pkgs.stdenv.isDarwin) {
  180. my-workspace-llvm-coverage = craneLibLLvmTools.cargoLlvmCov (commonArgs // {
  181. inherit cargoArtifacts;
  182. });
  183. };
  184. apps = {
  185. cdk-mintd = flake-utils.lib.mkApp {
  186. drv = cdk-mintd;
  187. };
  188. };
  189. devShells =
  190. let
  191. # pre-commit-checks
  192. _shellHook = (self.checks.${system}.pre-commit-check.shellHook or "");
  193. # devShells
  194. msrv = pkgs.mkShell ({
  195. shellHook = "
  196. ${_shellHook}
  197. cargo update -p half --precise 2.2.1
  198. cargo update -p tokio --precise 1.38.1
  199. cargo update -p tokio-util --precise 0.7.11
  200. cargo update -p tokio-stream --precise 0.1.15
  201. cargo update -p reqwest --precise 0.12.4
  202. cargo update -p serde_with --precise 3.1.0
  203. cargo update -p regex --precise 1.9.6
  204. cargo update -p backtrace --precise 0.3.58
  205. cargo update -p async-compression --precise 0.4.3
  206. cargo update -p zstd-sys --precise 2.0.8+zstd.1.5.5
  207. # For wasm32-unknown-unknown target
  208. cargo update -p bumpalo --precise 3.12.0
  209. cargo update -p moka --precise 0.11.1
  210. cargo update -p triomphe --precise 0.1.11
  211. cargo update -p url --precise 2.5.2
  212. ";
  213. buildInputs = buildInputs ++ WASMInputs ++ [ msrv_toolchain ];
  214. inherit nativeBuildInputs;
  215. } // envVars);
  216. stable = pkgs.mkShell ({
  217. shellHook = ''${_shellHook}'';
  218. buildInputs = buildInputs ++ WASMInputs ++ [ stable_toolchain ];
  219. inherit nativeBuildInputs;
  220. } // envVars);
  221. db_shell = pkgs.mkShell ({
  222. shellHook = ''
  223. ${_shellHook}
  224. cargo update -p half --precise 2.2.1
  225. cargo update -p home --precise 0.5.5
  226. cargo update -p tokio --precise 1.38.1
  227. cargo update -p tokio-stream --precise 0.1.15
  228. cargo update -p tokio-util --precise 0.7.11
  229. cargo update -p serde_with --precise 3.1.0
  230. cargo update -p reqwest --precise 0.12.4
  231. cargo update -p url --precise 2.5.2
  232. cargo update -p allocator-api2 --precise 0.2.18
  233. cargo update -p async-compression --precise 0.4.3
  234. cargo update -p zstd-sys --precise 2.0.8+zstd.1.5.5
  235. '';
  236. buildInputs = buildInputs ++ WASMInputs ++ [ db_msrv_toolchain ];
  237. inherit nativeBuildInputs;
  238. } // envVars);
  239. nightly = pkgs.mkShell ({
  240. shellHook = ''
  241. ${_shellHook}
  242. # Needed for github ci
  243. export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath [
  244. pkgs.zlib
  245. ]}:$LD_LIBRARY_PATH
  246. '';
  247. buildInputs = buildInputs ++ [ nightly_toolchain ];
  248. inherit nativeBuildInputs;
  249. } // envVars);
  250. in
  251. {
  252. inherit msrv stable nightly db_shell;
  253. default = stable;
  254. };
  255. }
  256. );
  257. }