crane/lib/vendorMultipleCargoDeps.nix
Jörg Thalheim 16f5732c14
allow to pass outputHashes to crane (#266)
This makes it possible to evaluate crane in a nixos test without network
as well as allow to backup all fetched input derivations properly in a
binary cache, whereas fetchGit will fallback to downloading from a
repository, which also requires a `git` binary to be present.

Co-authored-by: Ivan Petkov <ivanppetkov@gmail.com>
2023-09-22 04:08:53 +00:00

80 lines
1.7 KiB
Nix

{ lib
, runCommandLocal
, vendorCargoRegistries
, vendorGitDeps
}:
{ cargoConfigs ? [ ]
, cargoLockContentsList ? [ ]
, cargoLockList ? [ ]
, cargoLockParsedList ? [ ]
, outputHashes ? { }
}@args:
let
inherit (builtins)
attrNames
attrValues
fromTOML
groupBy
readFile;
inherit (lib)
concatMapStrings
escapeShellArg;
inherit (lib.attrsets)
filterAttrs
optionalAttrs;
inherit (lib.lists)
flatten
unique;
cargoLocksParsed = (map fromTOML ((map readFile cargoLockList) ++ cargoLockContentsList))
++ cargoLockParsedList;
# Extract all packages from all Cargo.locks and trim any unused attributes from the parsed
# data so we do not get any faux duplicates
allowedAttrs = {
name = true;
version = true;
source = true;
checksum = true;
};
allPackagesTrimmed = map
(l: map
(filterAttrs (k: _: allowedAttrs.${k} or false))
(l.package or [ ])
)
cargoLocksParsed;
lockPackages = flatten (map unique (attrValues (groupBy
(p: "${p.name}:${p.version}:${p.source or "local-path"}")
(flatten allPackagesTrimmed)
)));
vendoredRegistries = vendorCargoRegistries ({
inherit cargoConfigs lockPackages;
} // optionalAttrs (args ? registries) { inherit (args) registries; });
vendoredGit = vendorGitDeps {
inherit lockPackages outputHashes;
};
linkSources = sources: concatMapStrings
(name: ''
ln -s ${escapeShellArg sources.${name}} $out/${escapeShellArg name}
'')
(attrNames sources);
in
runCommandLocal "vendor-cargo-deps" { } ''
mkdir -p $out
cat >>$out/config.toml <<EOF
${vendoredRegistries.config}
${vendoredGit.config}
EOF
${linkSources vendoredRegistries.sources}
${linkSources vendoredGit.sources}
''