flake.nix 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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-redb)
  120. (craneLib.fileset.commonCargoSources ./crates/cdk-sqlite)
  121. ./crates/cdk-sqlite/src/mint/migrations
  122. ./crates/cdk-sqlite/src/wallet/migrations
  123. (craneLib.fileset.commonCargoSources crate)
  124. ];
  125. };
  126. cdk-mintd = craneLib.buildPackage (individualCrateArgs // {
  127. pname = "cdk-mintd";
  128. name = "cdk-mintd-${individualCrateArgs.version}";
  129. cargoExtraArgs = "-p cdk-mintd";
  130. src = fileSetForCrate ./crates/cdk-mintd;
  131. });
  132. nativeBuildInputs = with pkgs; [
  133. #Add additional build inputs here
  134. ] ++ lib.optionals isDarwin [
  135. # Additional darwin specific native inputs can be set here
  136. ];
  137. in
  138. {
  139. checks = {
  140. # Pre-commit checks
  141. pre-commit-check =
  142. let
  143. # this is a hack based on https://github.com/cachix/pre-commit-hooks.nix/issues/126
  144. # we want to use our own rust stuff from oxalica's overlay
  145. _rust = pkgs.rust-bin.stable.latest.default;
  146. rust = pkgs.buildEnv {
  147. name = _rust.name;
  148. inherit (_rust) meta;
  149. buildInputs = [ pkgs.makeWrapper ];
  150. paths = [ _rust ];
  151. pathsToLink = [ "/" "/bin" ];
  152. postBuild = ''
  153. for i in $out/bin/*; do
  154. wrapProgram "$i" --prefix PATH : "$out/bin"
  155. done
  156. '';
  157. };
  158. in
  159. pre-commit-hooks.lib.${system}.run {
  160. src = ./.;
  161. hooks = {
  162. rustfmt = {
  163. enable = true;
  164. entry = lib.mkForce "${rust}/bin/cargo-fmt fmt --all -- --config format_code_in_doc_comments=true --check --color always";
  165. };
  166. nixpkgs-fmt.enable = true;
  167. typos.enable = true;
  168. commitizen.enable = true; # conventional commits
  169. };
  170. };
  171. };
  172. packages = {
  173. inherit cdk-mintd;
  174. default = cdk-mintd;
  175. } // lib.optionalAttrs (!pkgs.stdenv.isDarwin) {
  176. my-workspace-llvm-coverage = craneLibLLvmTools.cargoLlvmCov (commonArgs // {
  177. inherit cargoArtifacts;
  178. });
  179. };
  180. apps = {
  181. cdk-mintd = flake-utils.lib.mkApp {
  182. drv = cdk-mintd;
  183. };
  184. };
  185. devShells =
  186. let
  187. # pre-commit-checks
  188. _shellHook = (self.checks.${system}.pre-commit-check.shellHook or "");
  189. # devShells
  190. msrv = pkgs.mkShell ({
  191. shellHook = "
  192. ${_shellHook}
  193. cargo update
  194. cargo update -p async-compression --precise 0.4.3
  195. cargo update -p zstd-sys --precise 2.0.8+zstd.1.5.5
  196. cargo update -p flate2 --precise 1.0.35
  197. cargo update -p home --precise 0.5.5
  198. cargo update -p zerofrom --precise 0.1.5
  199. cargo update -p half --precise 2.4.1
  200. cargo update -p url --precise 2.5.2
  201. # For wasm32-unknown-unknown target
  202. cargo update -p triomphe --precise 0.1.11
  203. ";
  204. buildInputs = buildInputs ++ WASMInputs ++ [ msrv_toolchain ];
  205. inherit nativeBuildInputs;
  206. } // envVars);
  207. stable = pkgs.mkShell ({
  208. shellHook = ''${_shellHook}'';
  209. buildInputs = buildInputs ++ WASMInputs ++ [ stable_toolchain ];
  210. inherit nativeBuildInputs;
  211. } // envVars);
  212. nightly = pkgs.mkShell ({
  213. shellHook = ''
  214. ${_shellHook}
  215. # Needed for github ci
  216. export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath [
  217. pkgs.zlib
  218. ]}:$LD_LIBRARY_PATH
  219. export RUST_SRC_PATH=${nightly_toolchain}/lib/rustlib/src/rust/library
  220. '';
  221. buildInputs = buildInputs ++ [ nightly_toolchain ];
  222. inherit nativeBuildInputs;
  223. } // envVars);
  224. in
  225. {
  226. inherit msrv stable nightly;
  227. default = stable;
  228. };
  229. }
  230. );
  231. }