forked from fleek-network/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
260 lines (235 loc) · 8.65 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
{
description = "Build a cargo project";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
crane = {
url = "github:ipetkov/crane";
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";
};
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; });
inherit (pkgs) lib;
craneLib = (crane.mkLib pkgs).overrideToolchain (
fenix.packages.${system}.fromToolchainFile {
file = ./rust-toolchain;
sha256 = "4HfRQx49hyuJ8IjBWSty3OXCLOmeaZF5qZAXW6QiQNI=";
}
);
src = craneLib.path ./.;
librusty_v8 = (
let
v8_version = "0.89.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 = "XxX3x3LBiJK768gvzIsV7aKm6Yn5dLS3LINdDOUjDGU=";
aarch64-darwin = "5cdd8914bf11b3d8724eab95c7a6eb8d6d791f9e26855207ab391d132f6c9aa3";
}
."${system}";
postFetch = ''
mv $out src.gz
${pkgs.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" "$@"
'')
installShellFiles
];
buildInputs =
with pkgs;
[
cacert # needed for nextests
libclang
fontconfig
freetype
protobufc
openssl_3
(rocksdb.override { enableShared = true; })
(snappy.override { static = true; })
zstd
zlib
bzip2
lz4
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_3}/lib";
OPENSSL_INCLUDE_DIR = "${lib.getDev pkgs.openssl_3.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";
# Enable mold linker
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER = "${pkgs.clang}/bin/clang";
CARGO_TARGET_AARCH64_APPLE_DARWIN_LINKER = "${pkgs.clang}/bin/clang";
# NOTE: mold is not fully supported on macOS yet, so we just use the default linker there.
# The error you would get looks like: mold: fatal: unknown command line option: -dynamic; -dynamic is a macOS linker's option. mold does not support macOS.
RUSTFLAGS =
"--cfg tokio_unstable"
+ lib.optionalString (!pkgs.stdenv.isDarwin) " -Clink-arg=-fuse-ld=${pkgs.mold-wrapped}/bin/mold";
};
# 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 tests with cargo-nextest
nextest = craneLib.cargoNextest (
commonArgs
// {
inherit cargoArtifacts;
partitions = 1;
partitionType = "count";
cargoNextestExtraArgs = "--workspace --exclude lightning-e2e";
}
);
};
# Expose the node and services as packages
packages = 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 = craneLib.buildPackage (
commonArgs
// {
inherit cargoArtifacts;
pname = "lightning-node";
doCheck = false;
cargoExtraArgs = lib.concatStringsSep " " [
"--locked"
"--bin lightning-node"
];
postInstall = lib.optionalString (pkgs.stdenv.buildPlatform.canExecute pkgs.stdenv.hostPlatform) ''
installShellCompletion --cmd lightning-node \
--bash <($out/bin/lightning-node completions bash) \
--fish <($out/bin/lightning-node completions fish) \
--zsh <($out/bin/lightning-node completions zsh)
'';
}
);
# Service binaries
lightning-services = craneLib.buildPackage (
commonArgs
// {
inherit cargoArtifacts;
pname = "lightning-services";
doCheck = false;
cargoExtraArgs = lib.concatStringsSep " " [
"--locked"
"--bin fn-service-0"
"--bin fn-service-1"
"--bin fn-service-2"
];
}
);
};
# 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};
packages = [ pkgs.rust-analyzer ];
}
);
# Allow using `nix fmt` on the project
formatter = pkgs.nixfmt-rfc-style;
}
);
}