-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathflake.nix
More file actions
1533 lines (1389 loc) · 55.3 KB
/
Copy pathflake.nix
File metadata and controls
1533 lines (1389 loc) · 55.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
description = "CDK Flake";
nixConfig = {
extra-substituters = [
"https://cashudevkit.cachix.org"
];
extra-trusted-public-keys = [
"cashudevkit.cachix.org-1:zFKdvMiTllKWxIFNTjXgisZsOFufmaZXjWJNcmc8r+4="
];
};
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-26.05";
nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
};
};
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.rust-analyzer-src.follows = "";
};
flake-utils.url = "github:numtide/flake-utils";
dart-overlay = {
url = "github:roman-vanesyan/dart-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
crane = {
url = "github:ipetkov/crane";
};
};
outputs =
{ self
, nixpkgs
, nixpkgs-unstable
, rust-overlay
, flake-utils
, dart-overlay
, crane
, ...
}@inputs:
flake-utils.lib.eachDefaultSystem (
system:
let
# Architecture-specific configuration for static musl builds
muslTarget =
{
"x86_64-linux" = "x86_64-unknown-linux-musl";
"aarch64-linux" = "aarch64-unknown-linux-musl";
}.${system} or null;
archSuffix =
{
"x86_64-linux" = "x86_64";
"aarch64-linux" = "aarch64";
"x86_64-darwin" = "x86_64-apple-darwin";
"aarch64-darwin" = "aarch64-apple-darwin";
}.${system} or null;
# Rust host target triple (used by binding derivations)
hostTarget =
{
"x86_64-linux" = "x86_64-unknown-linux-gnu";
"aarch64-linux" = "aarch64-unknown-linux-gnu";
"x86_64-darwin" = "x86_64-apple-darwin";
"aarch64-darwin" = "aarch64-apple-darwin";
}.${system};
cargoTargetEnvName =
{
"x86_64-linux" = "CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER";
"aarch64-linux" = "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER";
}.${system} or null;
overlays = [ (import rust-overlay) ];
# Derive version from Cargo.toml so there is a single source of truth
version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).workspace.package.version;
lib = pkgs.lib;
stdenv = pkgs.stdenv;
isDarwin = stdenv.isDarwin;
libsDarwin = lib.optionals isDarwin [
# Additional drwin specific inputs can be set here
# Note: Security and SystemConfiguration frameworks are provided by the default SDK
];
# Dependencies
pkgs = import nixpkgs {
inherit system overlays;
};
pkgsUnstable = import nixpkgs-unstable {
inherit system;
};
# Dart SDK from dart-overlay
dartpkgs = dart-overlay.packages.${system};
# Static/musl packages for fully static binary builds (Linux only)
pkgsMusl =
if muslTarget != null then
import nixpkgs
{
localSystem = system;
crossSystem = {
config = muslTarget;
isStatic = true;
};
}
else
null;
# Toolchains
# latest stable
stable_toolchain = pkgs.rust-bin.stable."1.96.0".default.override {
targets = [
"wasm32-unknown-unknown"
"aarch64-apple-ios"
"x86_64-apple-ios"
"aarch64-apple-ios-sim"
"aarch64-apple-darwin"
"x86_64-apple-darwin"
];
extensions = [
"rustfmt"
"clippy"
"rust-analyzer"
"llvm-tools-preview"
];
};
# MSRV stable
msrv_toolchain = pkgs.rust-bin.stable."1.85.0".default.override {
targets = [ "wasm32-unknown-unknown" ]; # wasm
extensions = [
"rustfmt"
"clippy"
"rust-analyzer"
"llvm-tools-preview"
];
};
# Nightly used for formatting
nightly_toolchain = pkgs.rust-bin.selectLatestNightlyWith (
toolchain:
toolchain.default.override {
extensions = [
"rustfmt"
"clippy"
"rust-analyzer"
"rust-src"
"llvm-tools-preview"
];
targets = [ "wasm32-unknown-unknown" ]; # wasm
}
);
# Stable toolchain with musl target for static builds (Linux only)
static_toolchain =
if muslTarget != null then
pkgs.rust-bin.stable."1.96.0".default.override
{
targets = [ muslTarget ];
}
else
null;
# Shim for native_toolchain_rust (Dart package) which requires rustup.
# Nix provides Rust directly, so this fakes the rustup commands.
rustupShim = pkgs.writeShellScriptBin "rustup" ''
case "''${1:-}" in
show)
version=$(rustc --version | sed 's/rustc //')
echo "''${version} (nix)"
;;
run)
shift # drop "run"
shift # drop channel
exec "$@"
;;
target)
# no-op: Nix provides targets via stable_toolchain
;;
*)
echo "rustup shim: unsupported command '$*'" >&2
exit 1
;;
esac
'';
# ========================================
# Crane setup for cached builds
# ========================================
craneLib = (crane.mkLib pkgs).overrideToolchain stable_toolchain;
craneLibMsrv = (crane.mkLib pkgs).overrideToolchain msrv_toolchain;
craneLibStatic =
if static_toolchain != null then (crane.mkLib pkgs).overrideToolchain static_toolchain else null;
craneLibNightly = (crane.mkLib pkgs).overrideToolchain nightly_toolchain;
# Source for crane builds - uses lib.fileset for efficient filtering
# This is much faster than nix-gitignore when large directories (like target/) exist
# because it uses a whitelist approach rather than scanning everything first
src = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.intersection (lib.fileset.fromSource (lib.sources.cleanSource ./.)) (
lib.fileset.unions [
./Cargo.toml
./Cargo.lock
./Cargo.lock.msrv
./.typos.toml
./rustfmt.toml
./README.md
./.cargo
./crates
./fuzz
./bindings
]
);
};
# Source for MSRV builds - uses Cargo.lock.msrv with MSRV-compatible deps
# Use lib.fileset approach (same as src) but substitute Cargo.lock with Cargo.lock.msrv
# We include both lock files and use cargoLock override to point to MSRV version
srcMsrv = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.intersection (lib.fileset.fromSource (lib.sources.cleanSource ./.)) (
lib.fileset.unions [
./Cargo.toml
./Cargo.lock.msrv
./.typos.toml
./rustfmt.toml
./README.md
./.cargo
./crates
./fuzz
./bindings
]
);
};
# Vendor cargo dependencies
cargoVendorDir = craneLib.vendorMultipleCargoDeps {
inherit (craneLib.findCargoFiles src) cargoConfigs;
cargoLockList = [
./Cargo.lock
];
};
# Vendor cargo dependencies for MSRV builds (Cargo.lock.msrv has different versions)
cargoVendorDirMsrv = craneLib.vendorMultipleCargoDeps {
inherit (craneLib.findCargoFiles srcMsrv) cargoConfigs;
cargoLockList = [
./Cargo.lock.msrv
];
};
# Common args for all Crane builds
commonCraneArgs = {
inherit src version cargoVendorDir;
pname = "cdk";
nativeBuildInputs = with pkgs; [
pkg-config
protobuf
];
buildInputs =
with pkgs;
[
openssl
sqlite
zlib
]
++ libsDarwin;
# Environment variables
PROTOC = "${pkgs.protobuf}/bin/protoc";
PROTOC_INCLUDE = "${pkgs.protobuf}/include";
};
# Common args for MSRV builds - uses srcMsrv with pinned deps
# Override cargoLock and cargoVendorDir to use Cargo.lock.msrv instead of Cargo.lock
commonCraneArgsMsrv = commonCraneArgs // {
src = srcMsrv;
cargoLock = ./Cargo.lock.msrv;
cargoVendorDir = cargoVendorDirMsrv;
};
# Musl-targeting C compiler for crates that compile bundled C code
# (libsqlite3-sys, secp256k1-sys, aws-lc-sys, etc.)
muslCC = if muslTarget != null then pkgs.pkgsStatic.stdenv.cc else null;
# Common args for static musl builds (Linux only)
# Produces fully statically-linked binaries that run on any Linux system
commonCraneArgsStatic =
if muslTarget != null then
(
{
inherit src version cargoVendorDir;
pname = "cdk-static";
# Cross-compile to musl for fully static linking
CARGO_BUILD_TARGET = muslTarget;
CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static";
# Host-side build tools (run on build machine)
nativeBuildInputs = with pkgs; [
pkg-config
protobuf
muslCC
];
# Target-side libraries (musl static libs linked into the binary)
buildInputs = with pkgsMusl; [
openssl.dev
zlib
];
# Tell the cc crate and cargo to use the musl-targeting C compiler/linker
TARGET_CC = "${muslCC}/bin/${muslCC.targetPrefix}cc";
# Force static OpenSSL linking (needed by postgres/native-tls)
OPENSSL_STATIC = "1";
OPENSSL_DIR = "${pkgsMusl.openssl.dev}";
OPENSSL_LIB_DIR = "${pkgsMusl.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${pkgsMusl.openssl.dev}/include";
# Protobuf (build-time code generation, runs on host)
PROTOC = "${pkgs.protobuf}/bin/protoc";
PROTOC_INCLUDE = "${pkgs.protobuf}/include";
# Tell pkg-config to find musl static libraries
PKG_CONFIG_ALL_STATIC = "1";
# Use the release-static profile for reproducible, optimized builds
CARGO_PROFILE = "release-static";
}
// {
# Dynamic attribute name for the cargo linker env var (arch-specific)
${cargoTargetEnvName} = "${muslCC}/bin/${muslCC.targetPrefix}cc";
}
)
else
null;
# Build ALL dependencies once - this is what gets cached by Cachix
workspaceDeps = craneLib.buildDepsOnly (
commonCraneArgs
// {
pname = "cdk-deps";
# Build deps for workspace
cargoExtraArgs = "--workspace";
}
);
# MSRV dependencies (separate cache due to different toolchain)
# Exclude cdk-redb (and its dependents) since redb requires a higher MSRV
workspaceDepsMsrv = craneLibMsrv.buildDepsOnly (
commonCraneArgsMsrv
// {
pname = "cdk-deps-msrv";
cargoExtraArgs = "--workspace --exclude cdk-redb --exclude cdk-integration-tests --exclude cdk-ffi-dart --exclude cdk-ffi-swift --exclude cdk-ffi-kotlin --exclude cdk-bindings-releaser";
}
);
# Static musl dependencies (separate cache for static builds, Linux only)
workspaceDepsStatic =
if muslTarget != null then
craneLibStatic.buildDepsOnly
(
commonCraneArgsStatic
// {
pname = "cdk-deps-static";
# Cache only the static release binaries' dependency set. FFI
# binding crates build cdylibs, which musl targets do not support.
cargoExtraArgs = "-p cdk-mintd -p cdk-cli --features cdk-mintd/ldk-node,cdk-mintd/postgres,cdk-mintd/prometheus,cdk-mintd/redis";
}
)
else
null;
# Nightly dependencies (separate cache due to different toolchain)
workspaceDepsNightly = craneLibNightly.buildDepsOnly (
commonCraneArgs
// {
pname = "cdk-deps-nightly";
cargoExtraArgs = "--workspace";
}
);
# Helper function to create combined clippy + test checks
# Runs both in a single derivation to share build artifacts.
# Now accepts a list of cargoArgs to run multiple checks sequentially.
mkClippyAndTest =
name: cargoArgsList:
craneLib.mkCargoDerivation (
commonCraneArgs
// {
pname = "cdk-check-${name}";
cargoArtifacts = workspaceDeps;
buildPhaseCargoCommand = builtins.concatStringsSep "\n" (
builtins.map
(cargoArgs: ''
echo "Running check with args: ${cargoArgs}"
cargo clippy ${cargoArgs} -- -D warnings
cargo test ${if builtins.match ".*cdk-cli.*" cargoArgs != null then "--bins" else "--lib"} ${cargoArgs}
'')
cargoArgsList
);
doCheck = false;
installPhaseCommand = "mkdir -p $out";
}
);
# Helper function to create example checks (compile only, no network access in sandbox)
mkExample =
name:
craneLib.mkCargoDerivation (
commonCraneArgs
// {
pname = "cdk-example-${name}";
cargoArtifacts = workspaceDeps;
buildPhaseCargoCommand = "cargo build --example ${name}";
# Examples are compiled but not run (no network in Nix sandbox)
doCheck = false;
installPhaseCommand = "mkdir -p $out";
}
);
# Helper function to create example packages (outputs binary for running outside sandbox)
mkExamplePackage =
name:
craneLib.mkCargoDerivation (
commonCraneArgs
// {
pname = "cdk-example-${name}";
cargoArtifacts = workspaceDeps;
buildPhaseCargoCommand = "cargo build --release --example ${name}";
doCheck = false;
installPhaseCommand = ''
mkdir -p $out/bin
cp target/release/examples/${name} $out/bin/
'';
}
);
# Helper function to create MSRV build checks
mkMsrvBuild =
name: cargoArgs:
craneLibMsrv.cargoBuild (
commonCraneArgsMsrv
// {
pname = "cdk-msrv-${name}";
cargoArtifacts = workspaceDepsMsrv;
cargoExtraArgs = cargoArgs;
}
);
# Helper function to create WASM build checks
# WASM builds don't need native libs like openssl
mkWasmBuild =
name: cargoArgs:
craneLib.cargoBuild ({
inherit src version cargoVendorDir;
pname = "cdk-wasm-${name}";
cargoArtifacts = workspaceDeps;
cargoExtraArgs = "${cargoArgs} --target wasm32-unknown-unknown";
# WASM doesn't need native build inputs
nativeBuildInputs = with pkgs; [ pkg-config ];
buildInputs = [ ];
# Disable tests for WASM (can't run in sandbox)
doCheck = false;
});
# Doc tests check
docTests = craneLib.cargoTest (
commonCraneArgs
// {
pname = "cdk-doc-tests";
cargoArtifacts = workspaceDeps;
cargoTestExtraArgs = "--doc";
}
);
# Strict docs check - build docs with warnings as errors
# Uses mkCargoDerivation for custom RUSTDOCFLAGS
strictDocs = craneLib.mkCargoDerivation (
commonCraneArgs
// {
pname = "cdk-strict-docs";
cargoArtifacts = workspaceDeps;
buildPhaseCargoCommand = ''
export RUSTDOCFLAGS="-D warnings"
cargo doc --no-deps \
-p cashu \
-p cdk-common \
-p cdk-sql-common \
-p cdk \
-p cdk-redb \
-p cdk-sqlite \
-p cdk-axum \
-p cdk-cln \
-p cdk-lnd \
-p cdk-lnbits \
-p cdk-fake-wallet \
-p cdk-mint-rpc \
-p cdk-payment-processor \
-p cdk-signatory \
-p cdk-cli \
-p cdk-mintd
'';
doCheck = false;
installPhaseCommand = "mkdir -p $out";
}
);
# FFI Python tests
ffiTests = craneLib.mkCargoDerivation (
commonCraneArgs
// {
pname = "cdk-ffi-tests";
cargoArtifacts = workspaceDeps;
nativeBuildInputs = commonCraneArgs.nativeBuildInputs ++ [
pkgs.python311
];
buildPhaseCargoCommand = ''
# Build the FFI library
cargo build --release --package cdk-ffi
# Generate Python bindings
cargo run -p cdk-ffi --bin uniffi-bindgen generate \
--library target/release/libcdk_ffi.so \
--language python \
--out-dir target/bindings/python
# Copy library to bindings directory
cp target/release/libcdk_ffi.so target/bindings/python/
# Run Python tests
python3 crates/cdk-ffi/tests/test_transactions.py
python3 crates/cdk-ffi/tests/test_kvstore.py
'';
doCheck = false;
installPhaseCommand = "mkdir -p $out";
}
);
# ========================================
# Language binding derivations (cached by Cachix)
# ========================================
# Dart FFI bindings: builds cdk-ffi-dart cdylib + generates Dart source
dartBindings = craneLib.mkCargoDerivation (
commonCraneArgs
// {
pname = "cdk-dart-bindings";
cargoArtifacts = workspaceDeps;
buildPhaseCargoCommand = ''
# Build the Dart FFI cdylib
cargo build --release -p cdk-ffi-dart
LIB_EXT="${if isDarwin then "dylib" else "so"}"
# Find the cdk-ffi-dart shared library in deps (contains UniFFI metadata)
CDK_FFI_LIB="target/release/deps/libcdk_ffi_dart.$LIB_EXT"
if [ ! -f "$CDK_FFI_LIB" ]; then
echo "ERROR: Could not find $CDK_FFI_LIB"
ls -la target/release/deps/libcdk_ffi* || true
exit 1
fi
echo "Using library: $CDK_FFI_LIB"
# Generate Dart bindings via the custom uniffi-bindgen binary
# Must run from bindings/dart/rust/ so it finds uniffi.toml
(cd bindings/dart/rust && \
cargo run --release -p cdk-ffi-dart --bin uniffi-bindgen -- \
"../../../$CDK_FFI_LIB" --out-dir ../../../target/dart-bindings)
'';
doCheck = false;
installPhaseCommand = ''
LIB_EXT="${if isDarwin then "dylib" else "so"}"
mkdir -p $out/lib/src/generated
cp target/dart-bindings/*.dart $out/lib/src/generated/
cp target/release/libcdk_ffi_dart.$LIB_EXT $out/lib/src/generated/
'';
}
);
# Kotlin JVM bindings: builds cdk-ffi-kotlin cdylib + generates Kotlin sources
kotlinBindings = craneLib.mkCargoDerivation (
commonCraneArgs
// {
pname = "cdk-kotlin-bindings";
cargoArtifacts = workspaceDeps;
buildPhaseCargoCommand = ''
LIB_EXT="${if isDarwin then "dylib" else "so"}"
# Build for host target
cargo build --release -p cdk-ffi-kotlin --target "${hostTarget}"
# Generate Kotlin bindings
cargo run --release -p cdk-ffi-kotlin --bin uniffi-bindgen -- generate \
--library "target/${hostTarget}/release/libcdk_ffi.$LIB_EXT" \
--language kotlin \
--out-dir target/kotlin-bindings \
--no-format
'';
doCheck = false;
installPhaseCommand = ''
LIB_EXT="${if isDarwin then "dylib" else "so"}"
mkdir -p $out/cdk-jvm/src/main/kotlin
mkdir -p $out/cdk-jvm/src/main/resources
# Copy generated Kotlin sources
cp -r target/kotlin-bindings/org $out/cdk-jvm/src/main/kotlin/
# Copy native library
cp "target/${hostTarget}/release/libcdk_ffi.$LIB_EXT" \
"$out/cdk-jvm/src/main/resources/libcdk_ffi.$LIB_EXT"
# Strip debug symbols
strip -x "$out/cdk-jvm/src/main/resources/libcdk_ffi.$LIB_EXT" 2>/dev/null || true
'';
}
);
uniffiBindgenGoSrc = pkgs.fetchFromGitHub {
owner = "NordSecurity";
repo = "uniffi-bindgen-go";
rev = "v0.6.0+v0.30.0";
hash = "sha256-HxcVUJb8DwbOcAP4LeBQPeKYrqR5jvvYJfcY7/gygkk=";
};
uniffiBindgenGoPackageSrc = pkgs.runCommand "uniffi-bindgen-go-src" { } ''
mkdir -p $out
cp -r ${uniffiBindgenGoSrc}/bindgen/* $out/
chmod -R u+w $out
cp ${./nix/uniffi-bindgen-go.Cargo.lock} $out/Cargo.lock
substituteInPlace $out/Cargo.toml \
--replace-fail 'uniffi_bindgen.workspace = true' 'uniffi_bindgen = "0.30.0"' \
--replace-fail 'uniffi_meta.workspace = true' 'uniffi_meta = "0.30.0"' \
--replace-fail 'uniffi_udl.workspace = true' 'uniffi_udl = "0.30.0"'
'';
uniffiBindgenGo = pkgs.rustPlatform.buildRustPackage {
pname = "uniffi-bindgen-go";
version = "0.6.0+v0.30.0";
src = uniffiBindgenGoPackageSrc;
cargoLock.lockFile = ./nix/uniffi-bindgen-go.Cargo.lock;
doCheck = false;
};
# Go FFI bindings: builds cdk-ffi-go cdylib + generates Go sources
goBindings = craneLib.mkCargoDerivation (
commonCraneArgs
// {
pname = "cdk-go-bindings";
cargoArtifacts = workspaceDeps;
nativeBuildInputs = commonCraneArgs.nativeBuildInputs ++ [
pkgs.go
uniffiBindgenGo
];
buildPhaseCargoCommand = ''
cargo build --release -p cdk-ffi-go
LIB_EXT="${if isDarwin then "dylib" else "so"}"
CDK_FFI_LIB="target/release/libcdk_ffi_go.$LIB_EXT"
export GOCACHE="$TMPDIR/go-cache"
mkdir -p "$GOCACHE"
if [ ! -f "$CDK_FFI_LIB" ]; then
echo "ERROR: Could not find $CDK_FFI_LIB"
ls -la target/release/libcdk_ffi_go.* || true
exit 1
fi
uniffi-bindgen-go \
"$CDK_FFI_LIB" \
--library \
--config bindings/go/uniffi.toml \
--out-dir target/go-bindings
'';
doCheck = false;
installPhaseCommand = ''
LIB_EXT="${if isDarwin then "dylib" else "so"}"
mkdir -p $out/bindings $out/lib
cp -r target/go-bindings/* $out/bindings/
cp "target/release/libcdk_ffi_go.$LIB_EXT" $out/lib/
'';
}
);
# ========================================
# Example definitions - single source of truth
# ========================================
exampleChecks = [
"mint-token"
"melt-token"
"p2pk"
"proof-selection"
"wallet"
];
# ========================================
# Clippy + test check definitions - single source of truth
# These run both clippy and unit tests in a single derivation
# ========================================
clippyAndTestChecks = {
"core-lib" = [
"-p cashu"
"-p cashu --no-default-features"
"-p cashu --no-default-features --features wallet"
"-p cashu --no-default-features --features mint"
"-p cdk-common"
"-p cdk-common --no-default-features"
"-p cdk-common --no-default-features --features wallet"
"-p cdk-common --no-default-features --features mint"
"-p cdk"
"-p cdk --no-default-features"
"-p cdk --no-default-features --features wallet"
"-p cdk --no-default-features --features mint"
];
"storage-and-cli" = [
"-p cdk-sql-common"
"-p cdk-sql-common --no-default-features --features wallet"
"-p cdk-sql-common --no-default-features --features mint"
"-p cdk-redb"
"-p cdk-sqlite"
"-p cdk-sqlite --features sqlcipher"
"-p cdk-cli"
"-p cdk-cli --features sqlcipher"
"-p cdk-cli --features redb"
];
"lightning-and-api" = [
"-p cdk-axum"
"-p cdk-axum --no-default-features"
"-p cdk-axum --no-default-features --features redis"
"-p cdk-cln"
"-p cdk-lnd"
"-p cdk-lnbits"
"-p cdk-fake-wallet"
"-p cdk-payment-processor"
"-p cdk-ldk-node"
"-p cdk-npubcash"
];
"mintd-main" = [
"-p cdk-mintd"
"-p cdk-mintd --features redis"
"-p cdk-mintd --features sqlcipher"
"-p cdk-signatory"
"-p cdk-mint-rpc"
"-p cdk-prometheus"
"-p cdk-ffi"
];
"mintd-backends-sqlite" = [
"-p cdk-mintd --no-default-features --features lnd,sqlite"
"-p cdk-mintd --no-default-features --features lnbits,sqlite"
"-p cdk-mintd --no-default-features --features fakewallet,sqlite"
"-p cdk-mintd --no-default-features --features grpc-processor,sqlite"
"-p cdk-mintd --no-default-features --features management-rpc,lnd,sqlite"
"-p cdk-mintd --no-default-features --features cln,sqlite"
];
"mintd-backends-postgres" = [
"-p cdk-mintd --no-default-features --features cln,postgres"
"-p cdk-mintd --no-default-features --features lnd,postgres"
"-p cdk-mintd --no-default-features --features lnbits,postgres"
"-p cdk-mintd --no-default-features --features fakewallet,postgres"
"-p cdk-mintd --no-default-features --features grpc-processor,postgres"
"-p cdk-mintd --no-default-features --features management-rpc,cln,postgres"
];
};
# ========================================
# MSRV build check definitions
# ========================================
msrvChecks = {
# Core library with all features
"cdk-all-features" = "-p cdk --features \"mint,wallet\"";
# Mintd with all backends, databases, and features
"cdk-mintd-all" =
"-p cdk-mintd --no-default-features --features \"cln,lnd,lnbits,fakewallet,ldk-node,grpc-processor,sqlite,postgres,redis,management-rpc\"";
# CLI - default features (excludes redb which breaks MSRV)
"cdk-cli" = "-p cdk-cli";
# Minimal builds to ensure no-default-features works
"cdk-wallet-only" = "-p cdk --no-default-features --features wallet";
};
# ========================================
# WASM build check definitions
# ========================================
wasmChecks = {
"cdk" = "-p cdk";
"cdk-no-default" = "-p cdk --no-default-features";
"cdk-wallet" = "-p cdk --no-default-features --features wallet";
};
# Common inputs
envVars = {
# rust analyzer needs NIX_PATH for some reason.
NIX_PATH = "nixpkgs=${inputs.nixpkgs}";
};
baseBuildInputs =
with pkgs;
[
# Add additional build inputs here
git
pkg-config
curl
just
protobuf
nixpkgs-fmt
typos
cargo-outdated
cargo-mutants
cargo-fuzz
cargo-nextest
# Database
postgresql_16
startPostgres
stopPostgres
pgStatus
pgConnect
# Needed for github ci
libz
]
++ libsDarwin;
regtestBuildInputs =
(with pkgsUnstable; [
lnd
# Apple Clang treats certain warnings as errors via -Werror, breaking
# the clightning build on macOS. These are fixed upstream in commit
# c22538ec (milestone v26.04) but not yet released. The override is
# Darwin-only so Linux builds still use the binary cache unmodified.
# TODO: Remove this override once clightning >= 26.04 lands in nixpkgs.
(
if pkgs.stdenv.hostPlatform.isDarwin then
(clightning.overrideAttrs (old: {
env = (old.env or { }) // {
NIX_CFLAGS_COMPILE =
(old.env.NIX_CFLAGS_COMPILE or "")
+ " -Wno-error=uninitialized-const-pointer"
+ " -Wno-error=gnu-folding-constant"
+ " -Wno-error=default-const-init-var-unsafe"
+ " -Wno-error=sometimes-uninitialized";
};
}))
else
clightning
)
bitcoind
])
++ (with pkgs; [
mprocs
]);
commonShellHook = ''
# Needed for github ci
export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath [ pkgs.zlib ]}:$LD_LIBRARY_PATH
'';
pgShellHook = ''
# PostgreSQL environment variables
export CDK_MINTD_DATABASE_URL="postgresql://${postgresConf.pgUser}:${postgresConf.pgPassword}@localhost:${postgresConf.pgPort}/${postgresConf.pgDatabase}"
# Informational banner goes to stderr so it never corrupts the stdout of
# commands run via `nix develop --command ... > file` (e.g. CI schema dumps).
echo "" >&2
echo "PostgreSQL commands available:" >&2
echo " start-postgres - Initialize and start PostgreSQL" >&2
echo " stop-postgres - Stop PostgreSQL (run before exiting)" >&2
echo " pg-status - Check PostgreSQL status" >&2
echo " pg-connect - Connect to PostgreSQL with psql" >&2
echo "" >&2
'';
# PostgreSQL configuration
postgresConf = {
pgUser = "cdk_user";
pgPassword = "cdk_password";
pgDatabase = "cdk_mint";
pgPort = "5432";
};
# Script to start PostgreSQL
startPostgres = pkgs.writeShellScriptBin "start-postgres" ''
set -e
PGDATA="$PWD/.pg_data"
PGPORT="${postgresConf.pgPort}"
PGUSER="${postgresConf.pgUser}"
PGPASSWORD="${postgresConf.pgPassword}"
PGDATABASE="${postgresConf.pgDatabase}"
# Stop any existing instance first
if [ -d "$PGDATA" ] && ${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" status > /dev/null 2>&1; then
echo "Stopping existing PostgreSQL instance..."
${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" stop > /dev/null 2>&1
fi
if [ ! -d "$PGDATA" ]; then
echo "Initializing PostgreSQL database..."
${pkgs.postgresql_16}/bin/initdb -D "$PGDATA" --auth=trust --no-locale --encoding=UTF8
# Configure PostgreSQL
echo "listen_addresses = 'localhost'" >> "$PGDATA/postgresql.conf"
echo "port = $PGPORT" >> "$PGDATA/postgresql.conf"
echo "unix_socket_directories = '$PGDATA'" >> "$PGDATA/postgresql.conf"
# Start temporarily to create user and database
${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" -l "$PGDATA/logfile" start
sleep 2
# Create user and database
${pkgs.postgresql_16}/bin/createuser -h localhost -p $PGPORT -s "$PGUSER" || true
${pkgs.postgresql_16}/bin/psql -h localhost -p $PGPORT -c "ALTER USER $PGUSER WITH PASSWORD '$PGPASSWORD';" postgres
${pkgs.postgresql_16}/bin/createdb -h localhost -p $PGPORT -O "$PGUSER" "$PGDATABASE" || true
${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" stop
echo "PostgreSQL initialized."
fi
echo "Starting PostgreSQL on port $PGPORT..."
${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" -l "$PGDATA/logfile" start
echo "PostgreSQL started. Connection URL: postgresql://$PGUSER:$PGPASSWORD@localhost:$PGPORT/$PGDATABASE"
'';
# Script to stop PostgreSQL
stopPostgres = pkgs.writeShellScriptBin "stop-postgres" ''
PGDATA="$PWD/.pg_data"
if [ -d "$PGDATA" ]; then
echo "Stopping PostgreSQL..."
${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" stop || echo "PostgreSQL was not running."
else
echo "No PostgreSQL data directory found."
fi
'';
# Script to check PostgreSQL status
pgStatus = pkgs.writeShellScriptBin "pg-status" ''
PGDATA="$PWD/.pg_data"
if [ -d "$PGDATA" ]; then
${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" status
else
echo "No PostgreSQL data directory found. Run 'start-postgres' first."
fi
'';
# Script to connect to PostgreSQL
pgConnect = pkgs.writeShellScriptBin "pg-connect" ''
${pkgs.postgresql_16}/bin/psql "postgresql://${postgresConf.pgUser}:${postgresConf.pgPassword}@localhost:${postgresConf.pgPort}/${postgresConf.pgDatabase}"
'';
# Helper to build a statically-linked binary package (Linux only)
# bin: the cargo binary name (e.g. "cdk-mintd")
# name: the output binary name prefix (e.g. "cdk-mintd-ldk")
# cargoExtraArgs: additional cargo args (e.g. "--bin cdk-mintd --features ldk-node")
staticVersion = if commonCraneArgsStatic != null then commonCraneArgsStatic.version else version;
mkStaticPackage =
if muslTarget != null then
(
{ bin
, name
, cargoExtraArgs
,
}:
craneLibStatic.buildPackage (
commonCraneArgsStatic
// {
pname = name;
cargoArtifacts = workspaceDepsStatic;
inherit cargoExtraArgs;
nativeBuildInputs = commonCraneArgsStatic.nativeBuildInputs ++ [