-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
305 lines (282 loc) · 10.1 KB
/
Copy pathflake.nix
File metadata and controls
305 lines (282 loc) · 10.1 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
{
description = "unit2nix — per-crate Nix build plans from Cargo's unit graph";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-parts = {
url = "github:hercules-ci/flake-parts";
inputs.nixpkgs-lib.follows = "nixpkgs";
};
};
outputs =
{
self,
nixpkgs,
...
}:
let
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
forAllSystems = nixpkgs.lib.genAttrs systems;
perSystem = forAllSystems (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
# Library: build a workspace from unit2nix JSON
buildFromUnitGraph =
{
pkgs ? nixpkgs.legacyPackages.${system},
src,
resolvedJson,
buildRustCrateForPkgs ? pkgs: pkgs.buildRustCrate,
defaultCrateOverrides ? null,
extraCrateOverrides ? { },
externalSources ? { },
localSourceFilter ? null,
skipStalenessCheck ? false,
clippyArgs ? [ ],
members ? null,
rustToolchain ? null,
}:
import ./lib/build-from-unit-graph.nix {
inherit
pkgs
src
resolvedJson
buildRustCrateForPkgs
defaultCrateOverrides
extraCrateOverrides
externalSources
localSourceFilter
skipStalenessCheck
clippyArgs
members
rustToolchain
;
};
# Auto mode: generate build plan via IFD (no manual step needed).
# Requires IFD enabled (default in Nix; disabled on Hydra).
buildFromUnitGraphAuto =
{
pkgs ? nixpkgs.legacyPackages.${system},
src,
workspaceDir ? null,
buildRustCrateForPkgs ? pkgs: pkgs.buildRustCrate,
defaultCrateOverrides ? null,
extraCrateOverrides ? { },
clippyArgs ? [ ],
members ? null,
# Optional: Rust toolchain for the IFD step (e.g. nightly from rust-overlay).
# `cargo --unit-graph` requires nightly. When set, this toolchain is
# prepended to PATH, overriding the stable cargo bundled in the unit2nix wrapper.
rustToolchain ? null,
# Pass --workspace to cargo for per-crate test support.
# When true, ALL workspace members and their dev-deps are resolved.
workspace ? false,
# Optional: build a specific package (-p flag)
package ? null,
# Optional: features to enable (comma-separated string)
features ? null,
# Optional: enable all features
allFeatures ? false,
# Optional: disable default features
noDefaultFeatures ? false,
# Optional: include dev-dependencies
includeDev ? false,
# Optional: build a specific binary target (--bin flag).
# More restrictive than package: only captures deps for one binary.
bin ? null,
# Don't pass --locked to cargo.
noLocked ? false,
# Sources for out-of-tree path dependencies.
# Maps relative paths (as in Cargo.toml) to Nix store paths.
# Example: externalSources = { "../sibling" = sibling-input; };
externalSources ? { },
localSourceFilter ? null,
}:
import ./lib/auto.nix {
inherit
pkgs
src
workspaceDir
buildRustCrateForPkgs
defaultCrateOverrides
extraCrateOverrides
clippyArgs
members
rustToolchain
workspace
package
bin
noLocked
features
allFeatures
noDefaultFeatures
includeDev
externalSources
localSourceFilter
;
unit2nix = self.packages.${system}.unit2nix;
};
# Plugin-based builder (requires plugin loaded)
buildFromUnitGraphPlugin =
{
pkgs ? nixpkgs.legacyPackages.${system},
src,
buildRustCrateForPkgs ? pkgs: pkgs.buildRustCrate,
defaultCrateOverrides ? null,
extraCrateOverrides ? { },
clippyArgs ? [ ],
members ? null,
target ? null,
includeDev ? false,
features ? null,
allFeatures ? false,
noDefaultFeatures ? false,
bin ? null,
package ? null,
}:
import ./lib/plugin.nix {
inherit
pkgs
src
buildRustCrateForPkgs
defaultCrateOverrides
extraCrateOverrides
clippyArgs
members
target
includeDev
features
allFeatures
noDefaultFeatures
bin
package
;
};
unit2nix = pkgs.callPackage ./nix/package.nix {
src = ./.;
cargoLockFile = ./Cargo.lock;
};
unit2nixPlugin = pkgs.callPackage ./nix/plugin.nix {
nixComponents = pkgs.nixVersions.nixComponents_2_33;
};
# Vendor crate sources from a single Cargo.lock.
# Returns { vendoredSources, cargoConfig, gitCheckouts }.
vendorCargoDeps =
{
pkgs ? nixpkgs.legacyPackages.${system},
cargoLock,
crateHashesJson ? null,
}:
import ./lib/vendor.nix {
inherit pkgs cargoLock crateHashesJson;
};
# Vendor crate sources from multiple Cargo.lock files.
# Merges all lock files and produces a single vendor directory.
# Returns { vendoredSources, cargoConfig, gitCheckouts }.
vendorMultipleCargoDeps =
{
pkgs ? nixpkgs.legacyPackages.${system},
cargoLocks,
crateHashesJson ? null,
}:
let
lib = pkgs.lib;
# Parse all lock files
allLocked = map (lock: lib.importTOML lock) cargoLocks;
# Merge all packages, deduplicating by (name, version, source)
allPackages = builtins.concatLists (map (l: l.package or [ ]) allLocked);
withSource = builtins.filter (p: p ? source) allPackages;
byId = builtins.listToAttrs (
map (p: {
name = "${p.name} ${p.version} (${p.source})";
value = p;
}) withSource
);
# Write a synthetic merged lock file for vendor.nix
mergedLock = pkgs.writeText "merged-Cargo.lock" (
builtins.toJSON {
package = builtins.attrValues byId;
}
);
# vendor.nix expects TOML, but we can just pass the merged packages
# directly. Create a minimal TOML lock file.
mergedLockToml = pkgs.writeText "merged-Cargo.lock" (
"# Merged lock file\nversion = 3\n\n"
+ lib.concatMapStrings (
p:
"[[package]]\n"
+ "name = ${builtins.toJSON p.name}\n"
+ "version = ${builtins.toJSON p.version}\n"
+ lib.optionalString (p ? source) "source = ${builtins.toJSON p.source}\n"
+ lib.optionalString (p ? checksum) "checksum = ${builtins.toJSON p.checksum}\n"
+ "\n"
) (builtins.attrValues byId)
);
in
import ./lib/vendor.nix {
inherit pkgs crateHashesJson;
cargoLock = mergedLockToml;
};
crateOverridesLib = import ./lib/crate-overrides.nix { inherit pkgs; };
sampleWorkspace = buildFromUnitGraph {
inherit pkgs;
src = ./sample_workspace;
resolvedJson = ./sample_workspace/build-plan.json;
};
in
{
lib = {
inherit
buildFromUnitGraph
buildFromUnitGraphAuto
buildFromUnitGraphPlugin
vendorCargoDeps
vendorMultipleCargoDeps
;
crateOverrides = crateOverridesLib.overrides;
isKnownNoOverride = crateOverridesLib.isKnownNoOverride;
};
packages = {
default = unit2nix;
inherit unit2nix unit2nixPlugin;
sample = sampleWorkspace.allWorkspaceMembers;
sample-bin = sampleWorkspace.workspaceMembers."sample-bin".build;
};
checks =
let
unit2nix = self.packages.${system}.unit2nix;
in
import ./nix/checks.nix {
inherit
pkgs
self
system
buildFromUnitGraph
buildFromUnitGraphAuto
unit2nix
sampleWorkspace
;
};
devShells.default = import ./nix/devshell.nix { inherit pkgs; };
}
);
pick = attr: nixpkgs.lib.mapAttrs (_: v: v.${attr}) perSystem;
in
{
templates.default = {
description = "Rust project with unit2nix per-crate Nix builds";
path = ./templates/default;
};
overlays.default = import ./nix/overlay.nix { inherit self; };
flakeModules.default = import ./flake-modules/default.nix { unit2nixFlake = self; };
lib = pick "lib";
packages = pick "packages";
checks = pick "checks";
devShells = pick "devShells";
};
}