-
Notifications
You must be signed in to change notification settings - Fork 38
/
flake.nix
394 lines (363 loc) · 14 KB
/
flake.nix
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
{
description = "Lightning - Fleek Network Node";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
crane.url = "github:ipetkov/crane";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = "github:numtide/flake-utils";
};
nixConfig = {
extra-substituters = [ "https://cache.garnix.io" ];
extra-trusted-public-keys = [ "cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=" ];
};
outputs =
{
self,
nixpkgs,
crane,
fenix,
flake-utils,
...
}:
flake-utils.lib.eachSystem
[
"x86_64-linux"
"aarch64-darwin"
]
(
system:
let
pkgs = (
import nixpkgs {
inherit system;
overlays = [
(
final: prev:
let
# Build a released package from `github.com/fortanix/rust-sgx`
mkRustSgxPackage = (
{
pname,
version,
hash,
cargoHash,
}:
prev.rustPlatform.buildRustPackage rec {
inherit pname version cargoHash;
nativeBuildInputs = with prev; [
pkg-config
protobuf
];
buildInputs = with prev; [ openssl ];
src = prev.fetchCrate { inherit pname version hash; };
}
);
in
{
# Upstream PR: https://github.com/NixOS/nixpkgs/pull/338280
fortanix-sgx-tools = mkRustSgxPackage {
pname = "fortanix-sgx-tools";
version = "0.5.1";
hash = "sha256-F0lZG1neAPVvyOxUtDPv0t7o+ZC+aQRtpFeq55QwcmE=";
cargoHash = "sha256-jYfsmPwhvt+ccUr4Vwq5q1YzNlxA+Vnpxd4KpWZrYo8=";
};
# Upstream PR: https://github.com/NixOS/nixpkgs/pull/338282
sgxs-tools = mkRustSgxPackage {
pname = "sgxs-tools";
version = "0.8.6";
hash = "sha256-24lUhi4IPv+asM51/BfufkOUYVellXoXsbWXWN/zoBw=";
cargoHash = "sha256-vtuOCLo7qBOfqMynykqf9folmlETx3or35+CuTurh3s=";
};
# Upstream PR: https://github.com/NixOS/nixpkgs/pull/338278
sgx-dcap-default-qpl = prev.stdenv.mkDerivation rec {
pname = "sgx-dcap-default-qpl";
version = "1.21";
src = prev.fetchFromGitHub {
owner = "intel";
repo = "SGXDataCenterAttestationPrimitives";
rev = "dcap_${version}_reproducible";
hash = "sha256-2ZMu9F46yR4KmTV8Os3fcjgF1uoXxBT50aLx72Ri/WY=";
fetchSubmodules = true;
};
nativeBuildInputs = [ prev.pkg-config ];
buildInputs = with prev; [
curl
openssl
boost
sgx-sdk
];
preBuild = ''
source ${prev.sgx-sdk}/sgxsdk/environment
'';
makeFlags = [
"-C QuoteGeneration"
"qpl_wrapper"
];
installPhase = ''
mkdir -p $out/lib
mv QuoteGeneration/build/linux/* $out/lib
ln -s $out/lib/libdcap_quoteprov.so $out/lib/libdcap_quoteprov.so.1
ln -s $out/lib/libsgx_default_qcnl_wrapper.so $out/lib/libsgx_default_qcnl_wrapper.so.1
'';
};
}
)
];
}
);
inherit (pkgs) lib;
craneLib = (crane.mkLib pkgs).overrideToolchain (
fenix.packages.${system}.fromToolchainFile {
dir = ./.;
sha256 = "X4me+hn5B6fbQGQ7DThreB/DqxexAhEQT8VNvW6Pwq4=";
}
);
src = craneLib.path ./.;
librusty_v8 = (
let
v8_version = "0.106.0";
arch = pkgs.rust.toRustTarget pkgs.stdenv.hostPlatform;
in
pkgs.fetchurl {
name = "librusty_v8-${v8_version}";
url = "https://github.com/denoland/rusty_v8/releases/download/v${v8_version}/librusty_v8_release_${arch}.a.gz";
sha256 =
{
x86_64-linux = "sha256-f7V60F69XsRhgwg34K9TFWruelVuux7MKpLG5vlV7Oc=";
aarch64-darwin = "sha256-A3l/ZsHdcGf/qTmiA/NGvoSKo0c97dIDBt4dSChFXtc=";
}
."${system}";
postFetch = ''
mv $out src.gz
gzip -d src.gz
mv src $out
'';
meta.version = v8_version;
}
);
gitRev = if (self ? rev) then self.rev else self.dirtyRev;
# Common arguments can be set here to avoid repeating them later
commonArgs = {
inherit src;
strictDeps = true;
pname = "lightning";
version = "0.1.0";
nativeBuildInputs = with pkgs; [
pkg-config
gcc
perl
cmake
clang
protobuf
mold-wrapped
(pkgs.writeShellScriptBin "git" ''
# hack to fix `git rev-parse HEAD` when building in sandbox
[[ $NIX_ENFORCE_PURITY -eq 1 ]] && echo ${gitRev} && exit
"${git}/bin/git" "$@"
'')
];
buildInputs =
with pkgs;
[
mold-wrapped
libclang
fontconfig
freetype
protobufc
openssl
curl
zstd
zlib
bzip2
lz4
(rocksdb.override { enableShared = true; })
(snappy.override { static = true; })
# For running nextest
cacert
# For ai service
onnxruntime
# Ebpf deps needed at runtime for debug builds via `admin ebpf build`
rust-bindgen
bpf-linker
]
++ lib.optionals pkgs.stdenv.isDarwin [
# MacOS specific packages
pkgs.libiconv
pkgs.darwin.apple_sdk.frameworks.QuartzCore
];
} // commonVars;
commonVars = {
# Shared and static libraries
PKG_CONFIG_PATH = "${lib.getDev pkgs.fontconfig}/lib/pkgconfig";
RUST_FONTCONFIG_DLOPEN = "on";
LIBCLANG_PATH = "${lib.getLib pkgs.libclang}/lib";
OPENSSL_NO_VENDOR = 1;
OPENSSL_LIB_DIR = "${lib.getLib pkgs.openssl}/lib";
OPENSSL_INCLUDE_DIR = "${lib.getDev pkgs.openssl.dev}/include";
RUSTY_V8_ARCHIVE = "${librusty_v8}";
ROCKSDB_LIB_DIR = "${pkgs.rocksdb}/lib";
Z_LIB_DIR = "${lib.getLib pkgs.zlib}/lib";
ZSTD_LIB_DIR = "${lib.getLib pkgs.zstd}/lib";
BZIP2_LIB_DIR = "${lib.getLib pkgs.bzip2}/lib";
SNAPPY_LIB_DIR = "${lib.getLib pkgs.snappy}/lib";
ORT_LIB_LOCATION = "${lib.getLib pkgs.onnxruntime}/lib";
RUSTFLAGS = "--cfg tokio_unstable";
# Enable mold linker (and clang)
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS = " -Clink-arg=-fuse-ld=${pkgs.mold-wrapped}/bin/mold";
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER = "${pkgs.clang}/bin/clang";
CARGO_TARGET_AARCH64_APPLE_DARWIN_LINKER = "${pkgs.clang}/bin/clang";
};
# Build *just* the cargo dependencies, so we can reuse all of that
# work (e.g. via cachix or github artifacts) when running in CI
cargoArtifacts = craneLib.buildDepsOnly (commonArgs);
in
{
# Allow using `nix flake check` to run tests and lints
checks = {
# Check formatting
fmt = craneLib.cargoFmt {
inherit (commonArgs) pname src;
cargoExtraArgs = "--all";
};
# Check doc tests
doc = craneLib.cargoDoc (commonArgs // { inherit cargoArtifacts; });
# Check clippy lints
clippy = craneLib.cargoClippy (
commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets --all-features -- -Dclippy::all -Dwarnings";
CARGO_PROFILE = "dev";
}
);
# Run hakari checks
hakari = craneLib.mkCargoDerivation {
inherit (commonArgs) pname src;
cargoArtifacts = null;
doInstallCargoArtifacts = false;
buildPhaseCargoCommand = ''
cargo hakari generate --diff || (echo "The workspace-hack is out of date. Run 'cargo hakari generate' and commit the changes." && exit 1)
cargo hakari manage-deps --dry-run || (echo "A crate is missing the workspace-hack dependency. Run 'cargo hakari manage-deps' and commit the changes." && exit 1)
cargo hakari verify
'';
nativeBuildInputs = [ pkgs.cargo-hakari ];
};
# Run tests with cargo-nextest
nextest = craneLib.cargoNextest (
commonArgs
// {
inherit cargoArtifacts;
partitions = 1;
partitionType = "count";
cargoNextestExtraArgs = "--workspace";
RUST_LOG = "debug";
}
);
};
# Expose the node and services as packages
packages =
let
# Helper to build a derivation for a single binary in the project
mkLightningBin =
name:
craneLib.buildPackage (
commonArgs
// {
inherit cargoArtifacts;
pname = name;
doCheck = false;
cargoExtraArgs = "--locked --bin ${name}";
}
);
in
rec {
default = lightning-node;
# Unified package with the node and all services
lightning-node = pkgs.symlinkJoin {
name = "lightning-node";
paths = [
lightning-node-standalone
lightning-services
];
};
# Core node binary
lightning-node-standalone = mkLightningBin "lightning-node";
# All service binaries
lightning-services = pkgs.symlinkJoin {
name = "lightning-services";
paths =
[
fn-service-0
fn-service-1
fn-service-2
]
++ lib.optionals (!pkgs.stdenv.isDarwin) [
# sgx service, not available on mac
fn-service-3
];
};
fn-service-0 = mkLightningBin "fn-service-0";
fn-service-1 = mkLightningBin "fn-service-1";
fn-service-2 = mkLightningBin "fn-service-2";
fn-service-3 = craneLib.buildPackage (
commonArgs
// {
pname = "fn-service-3";
doCheck = false;
nativeBuildInputs = (
with pkgs;
[
fortanix-sgx-tools
sgxs-tools
]
++ commonArgs.nativeBuildInputs
);
buildInputs = ([ pkgs.sgx-dcap-default-qpl ] ++ commonArgs.buildInputs);
# hack to use full source, but set cargo lock and deps to excluded workspace
cargoToml = "${src}/services/sgx/Cargo.toml";
cargoLock = "${src}/services/sgx/Cargo.lock";
postUnpack = ''
cd $sourceRoot/services/sgx
sourceRoot="."
'';
# Vendor enclave bin
FN_ENCLAVE_SGXS = pkgs.fetchurl {
name = "enclave.sgxs";
url = "https://bafybeifepixyjdgq5cfyvb4tlxfezkgwmyy6gvnua45pceh6fuxkbvwtly.ipfs.flk-ipfs.xyz";
hash = "sha256-LgJbqIKyhSdu8+W62ynETrynhIGAsUOg7KCd/xuC/N4=";
};
}
);
};
# Allow using `nix run` on the project
apps.default = flake-utils.lib.mkApp { drv = self.packages.${system}.default; };
# Allow using `nix develop` on the project
devShells.default = craneLib.devShell (
commonVars
// {
# Inherit inputs from checks
checks = self.checks.${system};
name = "lightning-dev";
packages =
with pkgs;
[
rust-analyzer
wabt # wasm tools, ie wasm2wat
]
++ lib.optionals (!pkgs.stdenv.isDarwin) [
# for debugging sgx service, not available on mac
fortanix-sgx-tools
sgxs-tools
sgx-dcap-default-qpl
];
FN_ENCLAVE_SGXS = "";
}
);
# Allow using `nix fmt` on the project
formatter = pkgs.nixfmt-rfc-style;
}
);
}