flake.nix 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. {
  2. description = "CDK Flake";
  3. inputs = {
  4. nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
  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.83.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. # Needed for github ci
  71. libz
  72. ] ++ libsDarwin;
  73. # WASM deps
  74. WASMInputs = with pkgs; [
  75. ];
  76. craneLib = crane.mkLib pkgs;
  77. src = craneLib.cleanCargoSource ./.;
  78. # Common arguments can be set here to avoid repeating them later
  79. commonArgs = {
  80. inherit src;
  81. strictDeps = true;
  82. buildInputs = [
  83. # Add additional build inputs here
  84. pkgs.protobuf
  85. pkgs.pkg-config
  86. ] ++ lib.optionals pkgs.stdenv.isDarwin [
  87. # Additional darwin specific inputs can be set here
  88. pkgs.libiconv
  89. ];
  90. # Additional environment variables can be set directly
  91. # MY_CUSTOM_VAR = "some value";
  92. PROTOC = "${pkgs.protobuf}/bin/protoc";
  93. PROTOC_INCLUDE = "${pkgs.protobuf}/include";
  94. };
  95. craneLibLLvmTools = craneLib.overrideToolchain
  96. (fenix.packages.${system}.complete.withComponents [
  97. "cargo"
  98. "llvm-tools"
  99. "rustc"
  100. ]);
  101. cargoArtifacts = craneLib.buildDepsOnly commonArgs;
  102. individualCrateArgs = commonArgs // {
  103. inherit cargoArtifacts;
  104. inherit (craneLib.crateNameFromCargoToml { inherit src; }) version;
  105. # NB: we disable tests since we'll run them all via cargo-nextest
  106. doCheck = false;
  107. };
  108. fileSetForCrate = crate: lib.fileset.toSource {
  109. root = ./.;
  110. fileset = lib.fileset.unions [
  111. ./Cargo.toml
  112. ./Cargo.lock
  113. (craneLib.fileset.commonCargoSources ./crates/cdk)
  114. (craneLib.fileset.commonCargoSources ./crates/cdk-axum)
  115. (craneLib.fileset.commonCargoSources ./crates/cdk-cln)
  116. (craneLib.fileset.commonCargoSources ./crates/cdk-lnd)
  117. (craneLib.fileset.commonCargoSources ./crates/cdk-fake-wallet)
  118. (craneLib.fileset.commonCargoSources ./crates/cdk-lnbits)
  119. (craneLib.fileset.commonCargoSources ./crates/cdk-strike)
  120. (craneLib.fileset.commonCargoSources ./crates/cdk-phoenixd)
  121. (craneLib.fileset.commonCargoSources ./crates/cdk-redb)
  122. (craneLib.fileset.commonCargoSources ./crates/cdk-sqlite)
  123. ./crates/cdk-sqlite/src/mint/migrations
  124. ./crates/cdk-sqlite/src/wallet/migrations
  125. (craneLib.fileset.commonCargoSources crate)
  126. ];
  127. };
  128. cdk-mintd = craneLib.buildPackage (individualCrateArgs // {
  129. pname = "cdk-mintd";
  130. name = "cdk-mintd-${individualCrateArgs.version}";
  131. cargoExtraArgs = "-p cdk-mintd";
  132. src = fileSetForCrate ./crates/cdk-mintd;
  133. });
  134. nativeBuildInputs = with pkgs; [
  135. #Add additional build inputs here
  136. ] ++ lib.optionals isDarwin [
  137. # Additional darwin specific native inputs can be set here
  138. ];
  139. in
  140. {
  141. checks = {
  142. # Pre-commit checks
  143. pre-commit-check =
  144. let
  145. # this is a hack based on https://github.com/cachix/pre-commit-hooks.nix/issues/126
  146. # we want to use our own rust stuff from oxalica's overlay
  147. _rust = pkgs.rust-bin.stable.latest.default;
  148. rust = pkgs.buildEnv {
  149. name = _rust.name;
  150. inherit (_rust) meta;
  151. buildInputs = [ pkgs.makeWrapper ];
  152. paths = [ _rust ];
  153. pathsToLink = [ "/" "/bin" ];
  154. postBuild = ''
  155. for i in $out/bin/*; do
  156. wrapProgram "$i" --prefix PATH : "$out/bin"
  157. done
  158. '';
  159. };
  160. in
  161. pre-commit-hooks.lib.${system}.run {
  162. src = ./.;
  163. hooks = {
  164. rustfmt = {
  165. enable = true;
  166. entry = lib.mkForce "${rust}/bin/cargo-fmt fmt --all -- --config format_code_in_doc_comments=true --check --color always";
  167. };
  168. nixpkgs-fmt.enable = true;
  169. typos.enable = true;
  170. commitizen.enable = true; # conventional commits
  171. };
  172. };
  173. };
  174. packages = {
  175. inherit cdk-mintd;
  176. default = cdk-mintd;
  177. } // lib.optionalAttrs (!pkgs.stdenv.isDarwin) {
  178. my-workspace-llvm-coverage = craneLibLLvmTools.cargoLlvmCov (commonArgs // {
  179. inherit cargoArtifacts;
  180. });
  181. };
  182. apps = {
  183. cdk-mintd = flake-utils.lib.mkApp {
  184. drv = cdk-mintd;
  185. };
  186. };
  187. devShells =
  188. let
  189. # pre-commit-checks
  190. _shellHook = (self.checks.${system}.pre-commit-check.shellHook or "");
  191. # devShells
  192. msrv = pkgs.mkShell ({
  193. shellHook = "
  194. ${_shellHook}
  195. cargo update
  196. cargo update -p async-compression --precise 0.4.3
  197. cargo update -p zstd-sys --precise 2.0.8+zstd.1.5.5
  198. cargo update -p flate2 --precise 1.0.35
  199. cargo update -p home --precise 0.5.5
  200. cargo update -p zerofrom --precise 0.1.5
  201. cargo update -p half --precise 2.4.1
  202. cargo update -p url --precise 2.5.2
  203. # For wasm32-unknown-unknown target
  204. cargo update -p triomphe --precise 0.1.11
  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. in
  227. {
  228. inherit msrv stable nightly;
  229. default = stable;
  230. };
  231. }
  232. );
  233. }