-
Notifications
You must be signed in to change notification settings - Fork 6
/
flake.nix
346 lines (306 loc) · 13.3 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
{
description = ''
A nix flake for development, and deployment of a LASR node.
'';
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";
advisory-db = {
url = "github:rustsec/advisory-db";
flake = false;
};
versatus-nix = {
url = "github:versatus/versatus.nix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.fenix.follows = "fenix";
inputs.flake-utils.follows = "flake-utils";
};
};
outputs = { self, nixpkgs, crane, fenix, flake-utils, advisory-db, versatus-nix, ... }:
flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) lib;
versaLib = versatus-nix.lib.${system};
rustToolchain = versaLib.toolchains.mkRustToolchainFromTOML
./rust-toolchain.toml
"sha256-SXRtAuO4IqNOQq+nLbrsDFbVk+3aVA8NNpSZsKlVH/8=";
# Overrides the default crane rust-toolchain with fenix.
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain.fenix-pkgs;
workspace = rec {
# Inherit the workspace version from the node crate since the workspace is not a package.
inherit (craneLib.crateNameFromCargoToml { cargoToml = (root + "/crates/node/Cargo.toml"); }) version;
name = "lasr";
root = ./.;
src = craneLib.cleanCargoSource root;
};
# Common arguments can be set here to avoid repeating them later
commonArgs = {
inherit (workspace) version src;
pname = workspace.name;
strictDeps = true;
# Inputs that must be available at the time of the build
nativeBuildInputs = [ pkgs.pkg-config ];
buildInputs = [
pkgs.openssl.dev
rustToolchain.darwin-pkgs
];
};
# Build *just* the cargo dependencies, so we can reuse
# all of that work (e.g. via cachix) when running in CI
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
individualCrateArgs = commonArgs // {
inherit cargoArtifacts;
doCheck = false; # Use cargo-nextest below.
};
fileSetForCrate = crate: lib.fileset.toSource {
root = workspace.root;
fileset = lib.fileset.unions [
./Cargo.toml
./Cargo.lock
./crates
(workspace.root + crate)
];
};
# Build the top-level crates of the workspace as individual derivations.
# This allows consumers to only depend on (and build) only what they need.
# Though it is possible to build the entire workspace as a single derivation,
# in this case the workspace itself is not a package.
mkCrateDrv = crate:
let
manifest = craneLib.crateNameFromCargoToml {
cargoToml = (workspace.root + "${crate}/Cargo.toml");
};
in
craneLib.buildPackage (individualCrateArgs // {
inherit (manifest) version pname;
cargoExtraArgs = "--locked --bin ${manifest.pname}";
src = fileSetForCrate crate;
});
lasr_cli = mkCrateDrv "/crates/cli";
lasr_node = mkCrateDrv "/crates/node";
in
{
checks = {
# Build the crate as part of `nix flake check` for convenience
inherit lasr_cli lasr_node;
# Run clippy (and deny all warnings) on the workspace source,
# again, reusing the dependency artifacts from above.
#
# Note that this is done as a separate derivation so that
# we can block the CI if there are issues here, but not
# prevent downstream consumers from building our crate by itself.
workspace-clippy = craneLib.cargoClippy (commonArgs // {
inherit cargoArtifacts;
pname = workspace.name;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
});
workspace-doc = craneLib.cargoDoc (commonArgs // {
inherit cargoArtifacts;
pname = workspace.name;
});
# Check formatting
workspace-fmt = craneLib.cargoFmt {
inherit (workspace) version src;
pname = workspace.name;
};
# Audit dependencies
workspace-audit = craneLib.cargoAudit {
inherit (workspace) version src;
inherit advisory-db;
pname = workspace.name;
};
# Audit licenses
workspace-deny = craneLib.cargoDeny {
inherit (workspace) version src;
pname = workspace.name;
};
# Run tests with cargo-nextest
# Consider setting `doCheck = false` on other crate derivations
# if you do not want the tests to run twice
workspace-nextest = craneLib.cargoNextest (commonArgs // {
inherit cargoArtifacts;
pname = workspace.name;
partitions = 1;
partitionType = "count";
});
};
packages =
let
hostPkgs = pkgs;
guest_system = versaLib.virtualisation.mkGuestSystem pkgs;
# Build packages for the linux variant of the host architecture, but preserve the host's
# version of nixpkgs to build the virtual machine with. This way, building and running a
# linux virtual environment works for all supported system architectures.
lasrGuestVM = nixpkgs.lib.nixosSystem {
system = null;
modules = [
./nixos/modules/deployments/common
./nixos/modules/deployments/debug/debug-options.nix
versatus-nix.nixosModules.deployments.debugVm
({
# Use the version of nixpkgs from the host to build the virtual image.
virtualisation.host.pkgs = hostPkgs;
nixpkgs.hostPlatform = guest_system;
})
({
nixpkgs.overlays = [
self.overlays.rust-overlay
self.overlays.lasr-overlay
];
})
];
};
# This would under normal circumstances be made available through `self.nixosConfigurations`
# however, we want the darwin systems to build linux images since the `lasr_node` binary
# has linux-only dependencies. Using the `nix-darwin` linux builder, MacOS users can still
# build this image for deployment, and can use the `lasr-vm` for debugging.
mkDigitalOceanImage = extraModules:
nixpkgs.lib.nixosSystem {
system = guest_system;
modules = [
./nixos/modules/deployments/common
versatus-nix.nixosModules.deployments.digitalOcean.digitalOceanImage
({
nixpkgs.overlays = [
self.overlays.rust-overlay
self.overlays.lasr-overlay
];
})
] ++ extraModules;
};
debugDigitalOceanImage = mkDigitalOceanImage [
./nixos/modules/deployments/debug/debug-options.nix
];
in
{
inherit lasr_cli lasr_node;
lasr_debug_image =
debugDigitalOceanImage.config.system.build.digitalOceanImage;
# Spin up a virtual machine with the lasr_debug_image options
# Useful for quickly debugging or testing changes locally
lasr_vm = lasrGuestVM.config.system.build.vm;
# TODO: Fix musl static linking
# lasr_cli_cross = # this works on Linux only at the moment
# let
# archPrefix = builtins.elemAt (pkgs.lib.strings.split "-" system) 0;
# target = "${archPrefix}-unknown-linux-musl";
# staticCraneLib =
# let rustMuslToolchain = with fenix.packages.${system}; combine [
# minimal.cargo
# minimal.rustc
# targets.${target}.latest.rust-std
# ];
# in
# (crane.mkLib pkgs).overrideToolchain rustMuslToolchain;
# buildLasrCliStatic = { stdenv, pkg-config, openssl, libiconv, darwin }:
# staticCraneLib.buildPackage {
# pname = "lasr_cli";
# version = "1";
# src = lasrSrc;
# strictDeps = true;
# nativeBuildInputs = [ pkg-config ];
# buildInputs = [
# (openssl.override { static = true; })
# rustToolchain.darwin-pkgs
# ];
# doCheck = false;
# cargoExtraArgs = "--locked --bin lasr_cli";
# CARGO_BUILD_TARGET = target;
# CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static";
# };
# in
# pkgs.pkgsMusl.callPackage buildLasrCliStatic {}; # TODO: needs fix, pkgsMusl not available on darwin systems
# TODO: Getting CC linker error
# lasr_cli_windows =
# let
# crossPkgs = import nixpkgs {
# crossSystem = pkgs.lib.systems.examples.mingwW64;
# localSystem = system;
# };
# craneLib =
# let
# rustToolchain = with fenix.packages.${system}; combine [
# minimal.cargo
# minimal.rustc
# targets.x86_64-pc-windows-gnu.latest.rust-std
# ];
# in
# (crane.mkLib crossPkgs).overrideToolchain rustToolchain;
# inherit (crossPkgs.stdenv.targetPlatform.rust)
# cargoEnvVarTarget cargoShortTarget;
# buildLasrCli = { stdenv, pkg-config, openssl, libiconv, windows }:
# craneLib.buildPackage {
# pname = "lasr_node";
# version = "1";
# src = lasrSrc;
# strictDeps = true;
# nativeBuildInputs = [ pkg-config ];
# buildInputs = [
# (openssl.override { static = true; })
# windows.pthreads
# ];
# doCheck = false;
# cargoExtraArgs = "--locked --bin lasr_cli";
# CARGO_BUILD_TARGET = cargoShortTarget;
# CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static";
# "CARGO_TARGET_${cargoEnvVarTarget}_LINKER" = "${stdenv.cc.targetPrefix}cc";
# HOST_CC = "${stdenv.cc.nativePrefix}cc";
# };
# in
# crossPkgs.callPackage buildLasrCli {};
} // lib.optionalAttrs (!pkgs.stdenv.isDarwin) {
workspace-llvm-coverage = craneLib.cargoLlvmCov (commonArgs // {
inherit cargoArtifacts;
});
};
apps = {
lasr_cli = flake-utils.lib.mkApp {
drv = lasr_cli;
};
lasr_node = flake-utils.lib.mkApp {
drv = lasr_node;
};
};
devShells.default = craneLib.devShell {
# Inherit inputs from checks.
checks = self.checks.${system};
# Extra inputs can be added here; cargo and rustc are provided by default.
#
# In addition, these packages and the `rustToolchain` are inherited from checks above:
# cargo-audit
# cargo-deny
# cargo-nextest
packages = with pkgs; [
# ripgrep
taplo
nil # nix lsp
nixpkgs-fmt # nix formatter
self.packages.${system}.lasr_cli
];
};
formatter = pkgs.nixpkgs-fmt;
}) // {
overlays = {
# Build lasr_cli & lasr_node so we can add their derivations to a configuration
lasr-overlay = import ./nixos/overlay.nix;
# This contains a lot of policy decisions which rust toolchain is used
rust-overlay = final: prev: {
craneLib = (self.inputs.crane.mkLib prev).overrideToolchain final.rustToolchain.fenix-pkgs;
rustToolchain = self.inputs.versatus-nix.lib.${prev.system}.toolchains.mkRustToolchainFromTOML
./rust-toolchain.toml
"sha256-SXRtAuO4IqNOQq+nLbrsDFbVk+3aVA8NNpSZsKlVH/8=";
};
};
};
}