-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathflake.nix
More file actions
309 lines (254 loc) · 9.89 KB
/
Copy pathflake.nix
File metadata and controls
309 lines (254 loc) · 9.89 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
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-25.11";
flake-parts.url = "github:hercules-ci/flake-parts";
crane.url = "github:ipetkov/crane";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
rust-advisory-db = {
url = "github:rustsec/advisory-db";
flake = false;
};
};
description = "tailscale-rs: tailscale client in rust";
outputs = inputs: let
systems = [
"x86_64-linux"
"aarch64-linux"
"armv7l-linux"
"aarch64-darwin"
];
lib = inputs.nixpkgs.lib;
# Use the Rust toolchain specified in rust-toolchain.toml.
repoRustToolchain = pkgs: pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
# Specifically used to provide `cargo +nightly fmt`
nightlyFmtToolchain = pkgs: pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.minimal.override {
extensions = ["rustfmt"];
});
importNixpkgs = { system, cross ? null }: import inputs.nixpkgs ({
system = system;
crossSystem = cross;
overlays = [
(import inputs.rust-overlay)
(final: prev: {
repo_toolchain = repoRustToolchain prev;
nightly_fmt_toolchain = nightlyFmtToolchain prev;
})
(final: prev: {
craneLib = (inputs.crane.mkLib prev).overrideToolchain (p: p.repo_toolchain);
craneLibNightlyFmt = (inputs.crane.mkLib prev).overrideToolchain (p: p.nightly_fmt_toolchain);
})
];
});
# If you include other assets in the build (e.g. via include_bytes!,
# include_str!, or a dependency used in a build.rs), you must update this
# -- these files are all that the `cargo build` for the workspace will be
# able to see.
#
# NB: this is obviously not very selective, meaning crates may rebuild
# spuriously, but better for now to avoid premature optimization.
rustsrc = let
filter = lib.fileset.unions [
./deny.toml
./.rustfmt.toml
./ts_ffi/cbindgen.toml
./ts_netstack_smoltcp/examples/axum_tun
./examples/axum
(lib.fileset.fileFilter (file: file.hasExt "rs") ./.)
(lib.fileset.fileFilter (file: file.hasExt "json") ./.)
(lib.fileset.fileFilter (file: file.name == "README.md") ./.)
(lib.fileset.fileFilter (file: file.name == "prefixes.txt.gz") ./ts_bart)
(lib.fileset.fileFilter (file: file.name == "Cargo.toml") ./.)
(lib.fileset.fileFilter (file: file.name == "Cargo.lock") ./.)
(lib.fileset.fileFilter (file: file.name == "config.toml") ./.) # capture .cargo/config.toml
];
in lib.fileset.toSource {
root = ./.;
fileset = filter;
};
# Output packages for a given `pkgs` expression.
# As above, pulled out into a function so we can call it in the
# packages.cross leaves.
makePackages = pkgs: let
deps = pkgs.callPackage ./nix/deps.nix {};
rootToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
workspaceMembers = lib.sort lib.lessThan (lib.unique rootToml.workspace.members) ++ [ "./" ];
crates = lib.map (memberPath: let
cargoToml = ./. + "/${memberPath}/Cargo.toml";
meta = pkgs.craneLib.crateNameFromCargoToml { cargoToml = cargoToml; };
builtPkg = pkgs.callPackage ./nix/crate.nix {
deps = deps;
cargoToml = cargoToml;
rustsrc = rustsrc;
};
in {
name = meta.pname;
value = builtPkg;
})
workspaceMembers;
docs = pkgs.craneLib.cargoDoc (deps.buildDeps // {
pname = "tailscale-rs-wksp";
version = "dev";
cargoArtifacts = deps;
src = rustsrc;
cargoExtraArgs = "--locked --workspace --all-features";
});
crates' = builtins.listToAttrs crates;
crates'' = crates' // {
ts_ffi = let
with_header = crates'.ts_ffi.overrideAttrs (prevAttrs: {
doCheck = false;
postInstall = ''
mkdir -p $out/include
cp -v ts_ffi/tailscale.h $out/include
'';
});
in with_header.overrideAttrs (prevAttrs: {
passthru = (prevAttrs.passthru or {} // {
examples = pkgs.callPackage ./ts_ffi/examples {
libtailscalers = with_header;
};
});
});
};
workspace = pkgs.craneLib.buildPackage (deps.passthru.buildDeps // {
pname = "tailscale-rs-wksp";
version = "dev";
src = rustsrc;
cargoArtifacts = deps;
cargoExtraArgs = "--workspace --all-targets --all-features";
env.PYO3_NO_PYTHON = 1;
env.PYO3_BUILD_EXTENSION_MODULE = 1;
# don't run benches as part of check
checkPhaseCargoCommand = "cargo test --profile release --all-features --workspace --examples --bins --lib --tests";
passthru = {
deps = deps;
};
});
in crates'' // ({
deps = deps;
docs = docs;
doc = docs;
workspace = workspace;
default = workspace;
libtailscalers = crates''.ts_ffi;
});
in inputs.flake-parts.lib.mkFlake { inputs = inputs; } {
systems = systems;
perSystem = { pkgs, system, lib, ... }: let
packages = makePackages pkgs;
# All the systems that are not us. Doesn't account for systems that might be architecture-
# compatible (e.g. i686 -> x64).
foreignSystems = builtins.filter (sys: sys != system) systems;
in {
_module.args = {
# override nixpkgs with above closure -- sets overlays needed for rust
pkgs = importNixpkgs { system = system; };
};
# Reexport nixpkgs (mostly for debugging)
legacyPackages = pkgs;
apps = let
tailscale = {
type = "app";
program = "${packages.tailscale}/lib/tailscale";
};
in {
tailscale = tailscale;
default = tailscale;
};
packages = packages // {
# flake-parts doesn't want attribute sets in packages -- this is an invalid dummy derivation
# we use to hack its check so that we can `nix build .#cross.$CROSS_SYSTEM.$PACKAGE`.
cross = (builtins.derivation {
system = system;
name = "placeholder";
builder = "empty";
}) // lib.genAttrs foreignSystems (cross: let
crossPkgs = importNixpkgs {
cross = cross;
system = system;
};
in makePackages crossPkgs);
};
# Run these (as well as checking the flake for valid structure) with `nix flake check`.
checks = let
common = {
pname = "tailscale-rs-wksp";
version = "dev";
src = rustsrc;
env.PYO3_NO_PYTHON = 1;
env.PYO3_BUILD_EXTENSION_MODULE = 1;
};
in {
# Lint lib targets
clippy_libs = pkgs.craneLib.cargoClippy (common // packages.deps.buildDeps // {
cargoArtifacts = packages.deps;
cargoExtraArgs = "--locked --workspace";
cargoClippyExtraArgs = "--lib --all-features --no-deps -- -D warnings";
});
# Lint all other targets: these are separated to skip enforcing missing_docs for
# targets that can't be part of public API.
clippy_other_targets = pkgs.craneLib.cargoClippy (common // packages.deps.buildDeps // {
cargoArtifacts = packages.deps;
cargoExtraArgs = "--locked --workspace";
cargoClippyExtraArgs = "--bins --tests --examples --benches --all-features --no-deps -- -D warnings -A missing_docs";
});
# See deny.toml -- various checks on dependencies
deny = pkgs.craneLib.cargoDeny common;
# Check code style
fmt = pkgs.craneLibNightlyFmt.cargoFmt common;
# Consults rustsec advisory db for reported vulnerabilities in dependencies
#
# We run this in addition to `cargo deny` because the deny check can't download the advisory
# db inside Nix -- for whatever reason, `cargo audit` has that hooked up in crane, while
# `deny` doesn't. Notably, the behavior of `cargo audit` appears to deviate from that of
# `deny`, so we ignore some advisories here which are known to be fine when checked via
# `deny`.
audit = pkgs.craneLib.cargoAudit (common // {
advisory-db = inputs.rust-advisory-db;
cargoAuditExtraArgs = let
ignored = [
# default ignore in crane
"yanked"
# old version of `rsa` used through russh, feature is turned off (deny doesn't mind)
"RUSTSEC-2023-0071"
];
in "--ignore " + (builtins.concatStringsSep " --ignore " ignored);
});
# This does the same as `cargo test`, it's just a pretty harness
nextest = pkgs.craneLib.cargoNextest (common // packages.deps.buildDeps // {
cargoArtifacts = packages.deps;
partitions = 1;
partitionType = "count";
cargoExtraArgs = "--locked --workspace --all-features";
cargoNextestPartitionsExtraArgs = "--no-tests=pass --show-progress=counter --no-fail-fast";
});
# Nextest can't run doctests, so run them separately
docTest = pkgs.craneLib.cargoDocTest (common // packages.deps.buildDeps // {
cargoArtifacts = packages.deps;
cargoExtraArgs = "--locked --workspace --all-features";
});
docs = packages.docs;
};
# Builds a dev shell which you can enter with `nix develop .#`, or automatically
# using direnv with use_flake
devShells.default = pkgs.craneLib.devShell {
inputsFrom = [ packages.tailscale ];
packages = with pkgs; [
cargo-audit
cargo-deny
cargo-nextest
cargo-machete
repo_toolchain
cargo-flamegraph
heaptrack
# for ffi
gnumake
stdenv.cc
];
};
};
};
}