-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathflake.nix
More file actions
371 lines (318 loc) · 13.2 KB
/
flake.nix
File metadata and controls
371 lines (318 loc) · 13.2 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
{
description = "cc3-next dev env and commands";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
rust-overlay.url = "github:oxalica/rust-overlay";
crane.url = "github:ipetkov/crane";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = {
nixpkgs,
rust-overlay,
crane,
flake-utils,
...
}:
flake-utils.lib.eachDefaultSystem (system: let
overlays = [(import rust-overlay)];
pkgs = import nixpkgs {
inherit system overlays;
};
# ====================================================================== #
# RUST SETUP #
# ====================================================================== #
rustToolchain = builtins.readFile ./rust-toolchain.toml;
rustChannel = builtins.fromTOML rustToolchain;
rustVersion = rustChannel.toolchain.channel;
rust = pkgs.rust-bin.stable.${rustVersion}.default.override {
extensions = ["rust-src"];
targets = [
"x86_64-unknown-linux-musl"
"wasm32-unknown-unknown"
];
};
# Use musl for static compilation
pkgsMusl = pkgs.pkgsCross.musl64;
craneLib = (crane.mkLib pkgsMusl).overrideToolchain (p: rust);
# Source filtering - include all files needed for the build
# We use cleanCargoSource with additional filters
src = let
# Filter to include Cargo files, Rust source, and other necessary files
cargoFilter = path: _type: builtins.match ".*Cargo\\.toml$|.*Cargo\\.lock$" path != null;
rustFilter = path: _type: builtins.match ".*\\.rs$" path != null;
# Include YAML configs that might be needed
yamlFilter = path: _type: builtins.match ".*\\.yaml$|.*\\.yml$" path != null;
# Include .scale files for subxt metadata (needed by cc-client)
scaleFilter = path: _type: builtins.match ".*\\.scale$" path != null;
# Include .json files for ABI definitions (needed by eth crate's sol! macro)
jsonFilter = path: _type: builtins.match ".*\\.json$" path != null;
# Include .html files for doc enhancements (needed by the attestor crate)
htmlFilter = path: _type: builtins.match ".*\\.html$" path != null;
# Include symlinks (needed for contracts/block_prover.json -> precompiles/metadata/abi/)
symlinkFilter = _path: type: type == "symlink";
# Combine filters
combinedFilter = path: type:
(cargoFilter path type) || (rustFilter path type) || (yamlFilter path type) || (scaleFilter path type) || (jsonFilter path type) || (htmlFilter path type) || (symlinkFilter path type) || (craneLib.filterCargoSources path type);
in
pkgs.lib.cleanSourceWith {
src = ./.;
filter = combinedFilter;
};
# ====================================================================== #
# BUILD SETUP #
# ====================================================================== #
nativeBuildInputs = with pkgs; [
perl
pkg-config
protobuf
clang
];
sccacheEnv = {
RUSTC_WRAPPER = "${pkgs.sccache}/bin/sccache";
SCCACHE_DIR = "/tmp/sccache"; # Shared cache directory
SCCACHE_CACHE_SIZE = "50G";
SCCACHE_SERVER_UDS = "/tmp/sccache-server.sock";
};
buildEnv = {
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
ROCKSDB_LIB_DIR = "${pkgs.rocksdb}/lib";
OPENSSL_LIB_DIR = "${pkgs.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${pkgs.openssl.dev}/include";
};
commonArgs = {
inherit src;
strictDeps = true;
nativeBuildInputs = nativeBuildInputs ++ [pkgsMusl.stdenv.cc];
buildInputs = [];
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl";
CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static -C linker=${pkgsMusl.stdenv.cc.targetPrefix}cc";
SKIP_WASM_BUILD = "1";
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
PROTOC = "${pkgs.protobuf}/bin/protoc";
# Let openssl-sys build OpenSSL from source (vendored)
OPENSSL_STATIC = "1";
};
# ====================================================================== #
# COMPILATION #
# ====================================================================== #
# Static dependencies (cached between builds)
cargoArtifacts = craneLib.buildDepsOnly (commonArgs
// {
pname = "cargo-deps";
cargoExtraArgs = "--package attestor --package attestor_zombienet --locked";
});
# attestor
attestor = craneLib.buildPackage (commonArgs
// {
inherit cargoArtifacts;
pname = "attestor";
cargoExtraArgs = "--package attestor --locked";
doCheck = false;
meta.mainProgram = "attestor";
});
# zombienet
zombienet = craneLib.buildPackage (commonArgs
// {
inherit cargoArtifacts;
pname = "attestor-zombienet";
cargoExtraArgs = "--package attestor_zombienet --locked";
doCheck = false;
# Binary name uses underscore, not hyphen
meta.mainProgram = "attestor_zombienet";
});
# ====================================================================== #
# SCRIPTS #
# ====================================================================== #
experimental = "--extra-experimental-features nix-command --extra-experimental-features flakes";
shebang = "#!/usr/bin/env -S nix develop ${experimental} .#default -c bash";
script_help = pkgs.writeShellScriptBin "help" ''
exec ${pkgs.busybox}/bin/cat <<EOF
cc3-next dev env and commands
=============================
USAGE
nix run .#<app> [-- <args>]
APPS
Info:
help Show this help message (default)
Runtime:
attestor Run the attestor binary (via cargo, in devshell)
Passes additional arguments to the attestor.
zombienet Launch a local attestor zombienet with 3 nodes
Preconfigured with:
• eth-url: ws://localhost:8545
• cc3-url: ws://localhost:9944
• funding: //Alice
• config: ./attestor/config.yaml
node Run creditcoin3-node in dev mode (fast-runtime)
Starts a temporary local chain with debug logging.
anvil Run Anvil (local Ethereum testnet)
Configured with 6-second block time.
Development:
check Run 'cargo check --tests --release'
clippy Run 'cargo clippy --tests --release'
test Run 'cargo test --release'
doc Run 'cargo doc --reslease --lib --no-deps'
EXAMPLES
nix run # Show this help
nix run .#node # Start local creditcoin3 node
nix run .#anvil # Start local Ethereum testnet
nix run .#zombienet # Start 3-node attestor network
nix run .#attestor -- --help # Show attestor CLI help
nix run .#check # Quick compilation check
nix run .#test -- -p attestor # Run tests for attestor package
PACKAGES
nix build .#attestor # Build static attestor binary
nix build .#zombienet # Build static zombienet binary
DEVSHELL
nix develop # Enter development shell with all tools
# Includes: rust, protobuf, foundry, nodejs,
# rocksdb, openssl, sccache, and more.
EOF
'';
script_anvil = pkgs.writeShellScriptBin "anvil" ''
exec ${pkgs.foundry}/bin/anvil --block-time 1 "$@"
'';
script_attestor = pkgs.writeScriptBin "attestor" ''
${shebang}
cargo run --release --bin attestor -- "$@"
'';
script_zombienet = pkgs.writeScriptBin "zombienet" ''
${shebang}
cargo run --release --bin attestor_zombienet -- \
-n 3 \
--bin=${script_attestor}/bin/attestor \
--funding-address='//Alice' \
--config=./attestor/config.yaml \
"$@"
'';
script_node = pkgs.writeScriptBin "node" ''
${shebang}
cargo run --features=fast-runtime --release --bin creditcoin3-node -- \
--dev \
--tmp \
--log=info,pallet_attestation=debug \
"$@"
'';
script_check = pkgs.writeScriptBin "cargo_check" ''
${shebang}
cargo check --release --tests --benches --examples "$@"
'';
script_clippy = pkgs.writeScriptBin "cargo_clippy" ''
${shebang}
cargo clippy --release --tests --benches --examples "$@"
'';
script_fmt = pkgs.writeScriptBin "cargo_fmt" ''
${shebang}
cargo fmt "$@"
'';
script_taplo = pkgs.writeScriptBin "taplo" ''
${shebang}
taplo "$@"
'';
script_test = pkgs.writeScriptBin "cargo_test" ''
${shebang}
cargo test --release --benches --examples "$@"
'';
script_doc = pkgs.writeScriptBin "cargo_doc" ''
${shebang}
cargo doc --lib --no-deps "$@"
'';
in {
# ====================================================================== #
# PROGRAMS #
# ====================================================================== #
packages = {
inherit attestor zombienet;
};
apps = rec {
default = help;
help = {
type = "app";
program = "${script_help}/bin/help";
};
attestor = {
type = "app";
program = "${script_attestor}/bin/attestor";
};
zombienet = {
type = "app";
program = "${script_zombienet}/bin/zombienet";
};
node = {
type = "app";
program = "${script_node}/bin/node";
};
anvil = {
type = "app";
program = "${script_anvil}/bin/anvil";
};
check = {
type = "app";
program = "${script_check}/bin/cargo_check";
};
clippy = {
type = "app";
program = "${script_clippy}/bin/cargo_clippy";
};
fmt = {
type = "app";
program = "${script_fmt}/bin/cargo_fmt";
};
taplo = {
type = "app";
program = "${script_taplo}/bin/taplo";
};
test = {
type = "app";
program = "${script_test}/bin/cargo_test";
};
doc = {
type = "app";
program = "${script_doc}/bin/cargo_doc";
};
};
# ====================================================================== #
# DEVSHELL #
# ====================================================================== #
devShells.default = pkgs.mkShell {
nativeBuildInputs = nativeBuildInputs ++ [rust pkgs.sccache pkgs.gcc];
buildInputs = with pkgs; [
openssl
rocksdb
libpq
];
# Additional packages for development
packages = with pkgs; [
jq
foundry
nodejs
taplo
];
LIBCLANG_PATH = buildEnv.LIBCLANG_PATH;
ROCKSDB_LIB_DIR = buildEnv.ROCKSDB_LIB_DIR;
OPENSSL_LIB_DIR = buildEnv.OPENSSL_LIB_DIR;
OPENSSL_INCLUDE_DIR = buildEnv.OPENSSL_INCLUDE_DIR;
# sccache for shared compilation cache
RUSTC_WRAPPER = sccacheEnv.RUSTC_WRAPPER;
SCCACHE_DIR = sccacheEnv.SCCACHE_DIR;
SCCACHE_CACHE_SIZE = sccacheEnv.SCCACHE_CACHE_SIZE;
SCCACHE_SERVER_UDS = sccacheEnv.SCCACHE_SERVER_UDS;
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [
pkgs.gcc.cc.lib
pkgs.openssl
pkgs.libpq
];
shellHook = ''
# See https://nixos.wiki/wiki/Node.js
export NPM_GLOBAL_PREFIX="$PWD/npm-global"
export NODE_PATH="$NPM_GLOBAL_PREFIX/lib/node_modules";
export PATH="$NPM_GLOBAL_PREFIX/bin:$PATH"
npm config set prefix "$NPM_GLOBAL_PREFIX"
# Start sccache server if not running
${pkgs.sccache}/bin/sccache --start-server 2>/dev/null || true
echo "sccache stats:"
${pkgs.sccache}/bin/sccache --show-stats
'';
};
});
}