-
Notifications
You must be signed in to change notification settings - Fork 89
/
tools.nix
438 lines (392 loc) · 15.1 KB
/
tools.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#
# Some tools that might be useful in builds.
#
# Part of the "public" API of crate2nix in the sense that we will try to
# avoid breaking the API and/or mention breakages in the CHANGELOG.
#
{ pkgs ? import ./nix/nixpkgs.nix { config = { }; }
, lib ? pkgs.lib
, stdenv ? pkgs.stdenv
, strictDeprecation ? true
}:
let
cargoNix = pkgs.callPackage ./crate2nix/Cargo.nix { inherit strictDeprecation; };
crate2nix = cargoNix.rootCrate.build;
in
rec {
/* Returns a derivation containing the whole top-level function generated
by crate2nix (`Cargo.nix`) which is typically called with `pkgs.callPackage`.
name: will be part of the derivation name
src: the source that is needed to build the crate, usually the
crate/workspace root directory
cargoToml: Path to the Cargo.toml file relative to src, "Cargo.toml" by
default.
*/
generatedCargoNix =
{ name
, src
, cargoToml ? "Cargo.toml"
, additionalCargoNixArgs ? [ ]
, additionalCrateHashes ? internal.parseOptHashesFile
(src + "/crate-hashes.json")
}:
let
crateDir = dirOf (src + "/${cargoToml}");
vendor = internal.vendorSupport rec {
inherit crateDir;
lockFiles = internal.gatherLockFiles crateDir;
hashes = internal.gatherHashes (lockFiles) // additionalCrateHashes;
};
in
stdenv.mkDerivation {
name = "${name}-crate2nix";
buildInputs = [ pkgs.cargo pkgs.jq crate2nix ];
preferLocalBuild = true;
inherit src;
phases = [ "unpackPhase" "buildPhase" ];
buildPhase = ''
set -e
mkdir -p "$out/cargo"
export CARGO_HOME="$out/cargo"
export HOME="$out"
cp ${vendor.cargoConfig} $out/cargo/config
crate_hashes="$out/crate-hashes.json"
echo -n '${builtins.toJSON vendor.extendedHashes}' | jq > "$crate_hashes"
# Remove last trailing newline, which crate2nix doesn't (yet) include
truncate -s -1 "$crate_hashes"
crate2nix_options=""
if [ -r ./${cargoToml} ]; then
crate2nix_options+=" -f ./${cargoToml}"
fi
if test -r "./crate2nix.json" ; then
cp "./crate2nix.json" "$out/crate2nix.json"
crate2nix_options+=" -c $out/crate2nix.json"
fi
if test -r "${src}/crate2nix-sources" ; then
ln -s "${src}/crate2nix-sources" "$out/crate2nix-sources"
fi
set -x
crate2nix generate \
$crate2nix_options \
-o "Cargo-generated.nix" \
-h "$crate_hashes" \
${lib.escapeShellArgs additionalCargoNixArgs} || {
{ set +x; } 2>/dev/null
echo "crate2nix failed." >&2
echo "== cargo/config (BEGIN)" >&2
sed 's/^/ /' $out/cargo/config >&2
echo ""
echo "== cargo/config (END)" >&2
echo ""
echo "== crate-hashes.json (BEGIN)" >&2
if [ -r $crate_hashes ]; then
sed 's/^/ /' $crate_hashes >&2
echo ""
else
echo "$crate_hashes missing"
fi
echo "== crate-hashes.json (END)" >&2
echo ""
echo "== ls -la (BEGIN)" >&2
ls -la
echo "== ls -la (END)" >&2
exit 3
}
{ set +x; } 2>/dev/null
if test -r "./crate-hashes.json" ; then
set -x
diff -u "./crate-hashes.json" $crate_hashes
{ set +x; } 2>/dev/null
fi
cp -r . $out/crate
echo "import ./crate/Cargo-generated.nix" > $out/default.nix
'';
};
# Applies the default arguments from pkgs to the generated `Cargo.nix` file.
#
# name: will be part of the derivation name
# src: the source that is needed to build the crate, usually the crate/workspace root directory
# cargoToml: Path to the Cargo.toml file relative to src, "Cargo.toml" by default.
appliedCargoNix = { cargoToml ? "Cargo.toml", ... } @ args:
pkgs.callPackage (generatedCargoNix args) { };
generate =
cargoNix.internal.deprecationWarning
"crate2nix/tools.nix: generate deprecated since 0.7. Use generatedCargoNix instead."
generatedCargoNix;
generated =
cargoNix.internal.deprecationWarning
"crate2nix/tools.nix: generated deprecated since 0.7. Use appliedCargoNix in instead."
appliedCargoNix;
internal = rec {
# Unpack sources and add a .cargo-checksum.json file to make cargo happy.
unpacked = { sha256, src }:
assert builtins.isString sha256;
assert builtins.isAttrs src;
pkgs.runCommand (lib.removeSuffix ".tar.gz" src.name) { }
''
mkdir -p $out
tar -xzf ${src} --strip-components=1 -C $out
echo '{"package":"${sha256}","files":{}}' > $out/.cargo-checksum.json
'';
sourceType = { source ? null, ... } @ package:
assert source == null || builtins.isString source;
if source == null then
null
else if source == "registry+https://github.com/rust-lang/crates.io-index" then
"crates-io"
else if lib.hasPrefix "git+" source then
"git"
else
builtins.throw "unknown source type: ${source}";
# Extracts URL and rev from a git source URL.
#
# Crude, should be more robust :(
parseGitSource = source:
assert builtins.isString source;
let
withoutGitPlus = lib.removePrefix "git+" source;
splitHash = lib.splitString "#" withoutGitPlus;
preFragment = builtins.elemAt splitHash 0;
fragment =
if builtins.length splitHash >= 2
then builtins.elemAt splitHash 1
else null;
splitQuestion = lib.splitString "?" preFragment;
preQueryParams = builtins.elemAt splitQuestion 0;
queryParamsList = lib.optionals
(builtins.length splitQuestion >= 2)
(lib.splitString "&" (builtins.elemAt splitQuestion 1));
kv = s:
let
l = lib.splitString "=" s;
key = builtins.elemAt l 0;
in
{
# Cargo supports using the now-obsoleted "ref" key in place of
# "branch"; see cargo-vendor source
name =
if key == "ref"
then "branch"
else key;
value = builtins.elemAt l 1;
};
queryParams = builtins.listToAttrs (map kv queryParamsList);
in
assert builtins.length splitHash <= 2;
assert builtins.length splitQuestion <= 2;
queryParams // {
url = preQueryParams;
urlFragment = fragment;
};
gatherLockFiles = crateDir:
let
fromCrateDir =
if builtins.pathExists (crateDir + "/Cargo.lock")
then [ (crateDir + "/Cargo.lock") ]
else [ ];
fromSources =
if builtins.pathExists (crateDir + "/crate2nix-sources")
then
let
subdirsTypes = builtins.readDir (crateDir + "/crate2nix-sources");
subdirs = builtins.attrNames subdirsTypes;
toLockFile = subdir: (crateDir + "/crate2nix-sources/${subdir}/Cargo.lock");
in
builtins.map toLockFile subdirs
else [ ];
in
fromCrateDir ++ fromSources;
parseOptHashesFile = hashesFile: lib.optionalAttrs
(builtins.pathExists hashesFile)
(builtins.fromJSON (builtins.readFile hashesFile));
gatherHashes = lockFiles:
let
hashesFiles = builtins.map
(cargoLock: "${dirOf cargoLock}/crate-hashes.json")
lockFiles;
parsedFiles = builtins.map parseOptHashesFile hashesFiles;
in
lib.foldl (a: b: a // b) { } parsedFiles;
vendorSupport =
{ crateDir ? ./.
, lockFiles ? [ ]
, hashes ? { }
}:
rec {
toPackageId = { name, version, source, ... }:
"${name} ${version} (${source})";
locked =
let
parseFile = cargoLock: lib.importTOML cargoLock;
allParsedFiles = builtins.map parseFile lockFiles;
merge = merged: lock:
{
package = merged.package ++ lock.package or [ ];
metadata = merged.metadata // lock.metadata or { };
};
in
lib.foldl merge { package = [ ]; metadata = { }; } allParsedFiles;
mkGitHash = { source, ... }@attrs:
let
parsed = parseGitSource source;
src = builtins.fetchGit ({
submodules = true;
inherit (parsed) url;
rev =
if isNull parsed.urlFragment
then parsed.rev
else parsed.urlFragment;
} // (if (parsed ? branch || parsed ? tag)
then { ref = parsed.branch or "refs/tags/${parsed.tag}"; }
else { allRefs = true; })
);
hash = pkgs.runCommand "hash-of-${attrs.name}" { nativeBuildInputs = [ pkgs.nix ]; } ''
echo -n "$(nix-hash --type sha256 --base32 ${src})" > $out
'';
in
rec {
name = toPackageId attrs;
# Fetching git submodules with builtins.fetchGit is only supported in nix > 2.3
value = hashes.${name} or
(if lib.versionAtLeast builtins.nixVersion "2.4"
then builtins.readFile hash
else builtins.throw "Checksum for ${name} not found in `hashes`");
};
extendedHashes = hashes
// builtins.listToAttrs (map mkGitHash (packagesByType.git or [ ]));
packages =
let
packagesWithDuplicates = assert builtins.isList locked.package; locked.package;
packagesWithoutLocal = builtins.filter (p: p ? source) packagesWithDuplicates;
packageById = package: { name = toPackageId package; value = package; };
packagesById = builtins.listToAttrs (builtins.map packageById packagesWithoutLocal);
in
builtins.attrValues packagesById;
packagesWithType = builtins.filter (pkg: (sourceType pkg) != null) packages;
packagesByType = lib.groupBy sourceType packagesWithType;
# Returns a derivation with all the transitive dependencies in
# sub directories suitable for cargo vendoring.
vendoredSources =
let
crateSources =
builtins.map
(
package:
let
fetcher = fetchers.${sourceType package};
source = fetcher package;
in
{
# We are using the store path (without the store directory)
# as the name of a symlink, and don't care about store
# store path we got that string one. It will in fact be
# tract in the value's string context anyways.
#
# This is needed for Nixpkgs 22.11 and beyond where the
# names are deduplicated with an attrset, and attrset keys
# are required to not have a string context.
name = builtins.baseNameOf (builtins.unsafeDiscardStringContext source);
path = source;
}
)
packagesWithType;
in
pkgs.linkFarm "deps" crateSources;
cargoConfig =
let
gitSourceConfig =
{ source, ... }@attrs:
assert builtins.isString source;
let
parsed = parseGitSource source;
in
''
[source."${lib.removePrefix "git+" source}"]
git = "${parsed.url}"
${lib.optionalString (parsed ? rev) ''rev = "${parsed.rev}"''}
${lib.optionalString (parsed ? tag) ''tag = "${parsed.tag}"''}
${lib.optionalString (parsed ? branch) ''branch = "${parsed.branch}"''}
replace-with = "vendored-sources"
'';
gitSources = packagesByType."git" or [ ];
uniqueBy = f:
lib.foldl' (acc: e: if lib.elem (f e) (map f acc) then acc else acc ++ [ e ]) [ ];
gitSourcesUnique = uniqueBy (c: c.source) gitSources;
gitSourceConfigs = builtins.map gitSourceConfig gitSourcesUnique;
gitSourceConfigsString = lib.concatStrings gitSourceConfigs;
in
pkgs.writeText
"vendor-config"
''
[source.crates-io]
replace-with = "vendored-sources"
${gitSourceConfigsString}
[source.vendored-sources]
directory = "${vendoredSources}"
'';
# Fetchers by source type that can fetch the package source.
fetchers = {
"crates-io" = { name, version, source, ... } @ package:
assert (sourceType package) == "crates-io";
let
packageId = toPackageId package;
sha256 =
package.checksum
or locked.metadata."checksum ${packageId}"
or (builtins.throw "Checksum for ${packageId} not found in Cargo.lock");
in
unpacked {
src = pkgs.fetchurl {
name = "crates-io-${name}-${version}.tar.gz";
# https://www.pietroalbini.org/blog/downloading-crates-io/
# Not rate-limited, CDN URL.
url = "https://static.crates.io/crates/${name}/${name}-${version}.crate";
inherit sha256;
};
inherit sha256;
};
"git" = { name, version, source, ... } @ package:
assert (sourceType package) == "git";
let
packageId = toPackageId package;
sha256 = extendedHashes.${packageId};
parsed = parseGitSource source;
src = pkgs.fetchgit {
name = "${name}-${version}";
inherit sha256;
inherit (parsed) url;
rev =
if isNull parsed.urlFragment
then parsed.rev
else parsed.urlFragment;
};
rootCargo = builtins.fromTOML (builtins.readFile "${src}/Cargo.toml");
isWorkspace = rootCargo ? "workspace";
isPackage = rootCargo ? "package";
containedCrates = rootCargo.workspace.members ++ (if isPackage then [ "." ] else [ ]);
getCrateNameFromPath = path:
let
cargoTomlCrate = builtins.fromTOML (builtins.readFile "${src}/${path}/Cargo.toml");
in
cargoTomlCrate.package.name;
pathToExtract =
if isWorkspace then
builtins.head
(builtins.filter
(to_filter:
(getCrateNameFromPath to_filter) == name
)
containedCrates)
else
".";
in
pkgs.runCommand (lib.removeSuffix ".tar.gz" src.name) { }
''
mkdir -p $out
cp -apR ${src}/${pathToExtract}/* $out
echo '{"package":null,"files":{}}' > $out/.cargo-checksum.json
'';
};
};
};
}