-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathflake.nix
More file actions
319 lines (274 loc) · 11.4 KB
/
Copy pathflake.nix
File metadata and controls
319 lines (274 loc) · 11.4 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
{
inputs = {
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
treefmt-nix.url = "github:numtide/treefmt-nix";
treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";
crane.url = "github:ipetkov/crane";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = inputs:
inputs.flake-parts.lib.mkFlake {inherit inputs;} {
systems = ["x86_64-linux" "aarch64-linux" "aarch64-darwin"];
imports = [inputs.treefmt-nix.flakeModule];
flake.hydraJobs = builtins.removeAttrs inputs.self.packages ["aarch64-linux"];
perSystem = {
pkgs,
config,
system,
self',
...
}: let
inherit (inputs.nixpkgs) lib;
pkgsRustOverlay = import inputs.nixpkgs {
inherit system;
overlays = [ (import inputs.rust-overlay) ];
};
pkgsMusl =
if system == "x86_64-linux"
then pkgsRustOverlay.pkgsCross.musl64
else null;
pkgsWindows =
if system == "x86_64-linux"
then pkgsRustOverlay.pkgsCross.mingwW64
else null;
craneLib = inputs.crane.mkLib pkgsRustOverlay;
workspaceVersion = (craneLib.crateNameFromCargoToml { cargoToml = ./mithril-common/Cargo.toml; }).version;
craneLibMusl =
if pkgsMusl != null then
(inputs.crane.mkLib pkgsMusl).overrideToolchain (p:
p.rust-bin.stable.latest.default.override {
targets = [ "x86_64-unknown-linux-musl" ];
}
)
else
null;
craneLibWindows =
if pkgsWindows != null then
(inputs.crane.mkLib pkgsWindows).overrideToolchain (p:
p.rust-bin.stable.latest.default.override {
targets = [ "x86_64-pc-windows-gnu" ];
}
)
else
null;
sourceFilter = orig_path: type: let
path = builtins.toString orig_path;
base = builtins.baseNameOf path;
parentDir = builtins.baseNameOf (builtins.dirOf path);
matchesSuffix = lib.any (suffix: lib.hasSuffix suffix base) [".rs" ".toml" ".md"];
isCargoFile = base == "Cargo.lock";
isCargoConfig = parentDir == ".cargo" && base == "config";
isOpenApiYaml = base == "openapi.yaml";
isFakeAggregatorDefaultData = lib.hasInfix "/mithril-aggregator-fake/default_data/" path;
isTestDataAsset = lib.hasInfix "/test_data/" path;
isGoldenVector = lib.hasInfix "/golden_vectors/" path;
in
type == "directory" || matchesSuffix || isCargoFile || isCargoConfig || isOpenApiYaml
|| isFakeAggregatorDefaultData || isTestDataAsset || isGoldenVector;
clean = root:
lib.cleanSourceWith {
src = lib.cleanSource (craneLib.path root);
filter = sourceFilter;
};
cleanMusl = root:
lib.cleanSourceWith {
src = lib.cleanSource (craneLibMusl.path root);
filter = sourceFilter;
};
cleanWindows = root:
lib.cleanSourceWith {
src = lib.cleanSource (craneLibWindows.path root);
filter = sourceFilter;
};
buildInputs =
[
pkgsRustOverlay.pkg-config
pkgsRustOverlay.gnum4
pkgsRustOverlay.cacert
]
++ lib.optionals (pkgsRustOverlay.stdenv.isDarwin) [
pkgsRustOverlay.libiconv
];
nativeBuildInputsMusl =
if pkgsMusl != null then [
pkgsMusl.stdenv.cc
pkgsMusl.pkg-config
pkgsMusl.gnum4
]
else [];
nativeBuildInputsWindows =
if pkgsWindows != null then [
pkgsWindows.stdenv.cc
]
else [];
buildInputsWindows =
if pkgsWindows != null then [
pkgsWindows.windows.pthreads
]
else [];
commonsArgs = {
pname = "mithril";
version = workspaceVersion;
src = clean ./.;
inherit buildInputs;
CARGO_TERM_VERBOSE = "true";
SSL_CERT_FILE = "${pkgsRustOverlay.cacert}/etc/ssl/certs/ca-bundle.crt";
};
commonsArgsMusl =
if pkgsMusl != null then {
pname = "mithril";
version = workspaceVersion;
src = cleanMusl ./.;
nativeBuildInputs = nativeBuildInputsMusl;
CARGO_TERM_VERBOSE = "true";
SSL_CERT_FILE = "${pkgsRustOverlay.cacert}/etc/ssl/certs/ca-bundle.crt";
}
else {};
commonsArgsWindows =
if pkgsWindows != null then {
pname = "mithril";
version = workspaceVersion;
src = cleanWindows ./.;
nativeBuildInputs = nativeBuildInputsWindows;
buildInputs = buildInputsWindows;
CARGO_TERM_VERBOSE = "true";
SSL_CERT_FILE = "${pkgsRustOverlay.cacert}/etc/ssl/certs/ca-bundle.crt";
}
else {};
preInstallScript = let
localLibs = "dummy|mithril|stm|multi_sig|size_benches|digester";
in ''
shopt -s extglob
TARGET_DIR="''${CARGO_TARGET_DIR:-target}"
if [ -n "$CARGO_BUILD_TARGET" ]; then
PROFILE_DIR="$TARGET_DIR/$CARGO_BUILD_TARGET/release"
else
PROFILE_DIR="$TARGET_DIR/release"
fi
rm "$PROFILE_DIR/deps/"*@(${localLibs})*
rm -r "$PROFILE_DIR/"*@(${localLibs})*
rm -r "$PROFILE_DIR/build/"*@(${localLibs})*
rm -r "$PROFILE_DIR/.fingerprint/"*@(${localLibs})*
'';
buildDeps = craneLibFor: commonArgs: cargoToml: cargoArtifacts:
(craneLibFor.buildDepsOnly (commonArgs
// lib.optionalAttrs (cargoToml != null) rec {
inherit (craneLibFor.crateNameFromCargoToml {inherit cargoToml;}) pname version;
cargoExtraArgs = "-p ${pname}";
}))
.overrideAttrs (_: {
inherit cargoArtifacts;
preInstall = preInstallScript;
});
buildPackage = cargoToml: baseCargoArtifacts: args:
craneLib.buildPackage (commonsArgs
// lib.optionalAttrs (cargoToml != null) rec {
inherit (craneLib.crateNameFromCargoToml {inherit cargoToml;}) pname version;
cargoExtraArgs = "-p ${pname}";
cargoTestExtraArgs = "-- --skip slow::";
}
// {
cargoArtifacts = buildDeps craneLib commonsArgs cargoToml baseCargoArtifacts;
}
// {
cargoTestCommand = "RUST_BACKTRACE=1 cargo test --profile release";
}
// args);
buildPackageMusl = cargoToml: baseCargoArtifacts: args:
craneLibMusl.buildPackage (commonsArgsMusl
// lib.optionalAttrs (cargoToml != null) rec {
inherit (craneLibMusl.crateNameFromCargoToml {inherit cargoToml;}) pname version;
cargoExtraArgs = "-p ${pname}";
cargoTestExtraArgs = "-- --skip slow::";
}
// {
cargoArtifacts = buildDeps craneLibMusl commonsArgsMusl cargoToml baseCargoArtifacts;
RUSTFLAGS = "-C target-feature=+crt-static";
}
// args);
buildPackageWindows = cargoToml: baseCargoArtifacts: args:
craneLibWindows.buildPackage (commonsArgsWindows
// lib.optionalAttrs (cargoToml != null) rec {
inherit (craneLibWindows.crateNameFromCargoToml {inherit cargoToml;}) pname version;
cargoExtraArgs = "-p ${pname}";
cargoTestExtraArgs = "-- --skip slow::";
}
// {
cargoArtifacts = buildDeps craneLibWindows commonsArgsWindows cargoToml baseCargoArtifacts;
RUSTFLAGS = "-C target-feature=+crt-static";
doCheck = false;
}
// args);
# Standard (glibc) builds
mithril-stm = buildPackage ./mithril-stm/Cargo.toml null {};
mithril-common = buildPackage ./mithril-common/Cargo.toml mithril-stm.cargoArtifacts { cargoExtraArgs = "-p mithril-common"; };
mithril = buildPackage null mithril-common.cargoArtifacts {
doCheck = false;
};
# Static (musl) base dependencies
mithril-stm-musl = if pkgsMusl != null then buildPackageMusl ./mithril-stm/Cargo.toml null {} else null;
mithril-common-musl = if pkgsMusl != null then buildPackageMusl ./mithril-common/Cargo.toml mithril-stm-musl.cargoArtifacts { cargoExtraArgs = "-p mithril-common"; } else null;
# Windows (mingw64) base dependencies
mithril-stm-windows = if pkgsWindows != null then buildPackageWindows ./mithril-stm/Cargo.toml null {} else null;
mithril-common-windows = if pkgsWindows != null then buildPackageWindows ./mithril-common/Cargo.toml mithril-stm-windows.cargoArtifacts { cargoExtraArgs = "-p mithril-common"; } else null;
in {
packages = {
default = mithril;
inherit mithril mithril-stm mithril-common;
mithril-client = buildPackage ./mithril-client/Cargo.toml mithril.cargoArtifacts { cargoExtraArgs = "-p mithril-client --features rustls,full"; };
mithril-end-to-end = buildPackage ./mithril-test-lab/mithril-end-to-end/Cargo.toml null {};
} // lib.optionalAttrs (pkgsMusl != null) {
mithril-aggregator =
buildPackageMusl ./mithril-aggregator/Cargo.toml mithril-common-musl.cargoArtifacts {};
mithril-signer =
buildPackageMusl ./mithril-signer/Cargo.toml mithril-common-musl.cargoArtifacts {};
mithril-client-cli =
buildPackageMusl ./mithril-client-cli/Cargo.toml mithril-common-musl.cargoArtifacts {
pname = "mithril-client";
};
} // lib.optionalAttrs (pkgsWindows != null) {
mithril-client-cli-windows =
buildPackageWindows ./mithril-client-cli/Cargo.toml mithril-common-windows.cargoArtifacts {
pname = "mithril-client";
};
} // lib.optionalAttrs (pkgsMusl == null) {
mithril-client-cli =
buildPackage ./mithril-client-cli/Cargo.toml mithril.cargoArtifacts {
pname = "mithril-client";
};
};
devShells.default = pkgsRustOverlay.mkShell {
inputsFrom = [self'.packages.mithril-client-cli];
# _FORTIFY_SOURCE=2 (injected by Nix fortify hardening) requires at least -O1;
# the ci-tests cargo profile uses -O0, which causes jemalloc's autoconf configure
# step to fail. Disable fortify in the dev shell to avoid this interaction.
hardeningDisable = ["fortify"];
nativeBuildInputs = [
pkgsRustOverlay.cargo
pkgsRustOverlay.rustc
pkgsRustOverlay.libiconv
config.treefmt.package
pkgsRustOverlay.gnumake
pkgsRustOverlay.cargo-edit
];
shellHook = ''
export RUST_BACKTRACE=1
ln -sf ${config.treefmt.build.configFile} treefmt.toml
'';
};
formatter = pkgsRustOverlay.writeShellApplication {
name = "treefmt";
text = "exec ${config.treefmt.package}/bin/treefmt";
};
treefmt = {
programs.alejandra.enable = true;
programs.rustfmt.enable = true;
projectRootFile = "flake.nix";
};
};
};
}