2019-06-27 15:49:59 +00:00
|
|
|
{ lib, writeText, runCommand, remarshal }:
|
2019-11-19 19:28:02 +00:00
|
|
|
let
|
|
|
|
builtinz =
|
2019-11-19 19:30:49 +00:00
|
|
|
builtins // import ./builtins
|
|
|
|
{ inherit lib writeText remarshal runCommand; };
|
2019-11-19 19:28:02 +00:00
|
|
|
in
|
2019-06-25 15:36:22 +00:00
|
|
|
rec
|
|
|
|
{
|
2019-11-19 19:30:49 +00:00
|
|
|
# The list of _all_ crates (incl. transitive dependencies) with name,
|
|
|
|
# version and sha256 of the crate
|
|
|
|
# Example:
|
|
|
|
# [ { name = "wabt", version = "2.0.6", sha256 = "..." } ]
|
|
|
|
mkVersions = cargolock:
|
|
|
|
if builtins.hasAttr "metadata" cargolock then
|
|
|
|
|
|
|
|
# TODO: this should nub by <pkg-name>-<pkg-version>
|
|
|
|
(
|
|
|
|
lib.concatMap (
|
|
|
|
x:
|
|
|
|
let
|
|
|
|
mdk = mkMetadataKey x.name x.version;
|
|
|
|
in
|
|
|
|
(
|
|
|
|
lib.optional (builtins.hasAttr mdk cargolock.metadata)
|
|
|
|
{
|
|
|
|
inherit (x) version name;
|
|
|
|
sha256 = cargolock.metadata.${mkMetadataKey x.name x.version};
|
|
|
|
}
|
|
|
|
) ++ (lib.concatMap (parseDependency cargolock) (x.dependencies or []))
|
2019-06-25 15:36:22 +00:00
|
|
|
|
|
|
|
)
|
2019-11-19 19:30:49 +00:00
|
|
|
cargolock.package
|
|
|
|
)
|
|
|
|
else [];
|
|
|
|
|
|
|
|
# Turns "lib-name lib-ver (registry+...)" to [ { name = "lib-name", etc } ]
|
|
|
|
# iff the package is present in the Cargo.lock (otherwise returns [])
|
|
|
|
parseDependency = cargolock: str:
|
|
|
|
let
|
|
|
|
components = lib.splitString " " str;
|
|
|
|
name = lib.elemAt components 0;
|
|
|
|
version = lib.elemAt components 1;
|
|
|
|
mdk = mkMetadataKey name version;
|
|
|
|
in
|
2019-11-19 19:28:02 +00:00
|
|
|
lib.optional (builtins.hasAttr mdk cargolock.metadata)
|
2019-11-19 19:30:49 +00:00
|
|
|
(
|
|
|
|
let
|
|
|
|
sha256 = cargolock.metadata.${mkMetadataKey name version};
|
|
|
|
in
|
|
|
|
{ inherit name version sha256; }
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
# crafts the key used to look up the sha256 in the cargo lock; no
|
|
|
|
# robustness guarantee
|
|
|
|
mkMetadataKey = name: version:
|
|
|
|
"checksum ${name} ${version} (registry+https://github.com/rust-lang/crates.io-index)";
|
|
|
|
|
2019-12-13 15:37:19 +00:00
|
|
|
# a record:
|
|
|
|
# { "." = # '.' is the directory of the cargotoml
|
|
|
|
# [
|
|
|
|
# {
|
|
|
|
# name = "rand";
|
|
|
|
# url = "https://github.com/...";
|
|
|
|
# checkout = "/nix/store/checkout"
|
|
|
|
# }
|
|
|
|
# ]
|
|
|
|
# }
|
|
|
|
findGitDependencies =
|
|
|
|
{ cargotomls
|
|
|
|
}:
|
|
|
|
let
|
2019-12-17 15:55:52 +00:00
|
|
|
tomlDependencies = cargotoml:
|
2019-12-13 15:37:19 +00:00
|
|
|
lib.filter (x: ! isNull x) (
|
|
|
|
lib.mapAttrsToList
|
|
|
|
(k: v:
|
2019-12-17 15:55:52 +00:00
|
|
|
if ! (lib.isAttrs v && builtins.hasAttr "git" v)
|
2019-12-13 15:37:19 +00:00
|
|
|
then null
|
|
|
|
else
|
|
|
|
{ name = k;
|
|
|
|
url = v.git;
|
2019-12-17 15:55:52 +00:00
|
|
|
rev = v.rev;
|
2019-12-13 15:37:19 +00:00
|
|
|
checkout = builtins.fetchGit {
|
|
|
|
url = v.git;
|
|
|
|
rev = v.rev;
|
|
|
|
};
|
|
|
|
}
|
2019-12-17 15:55:52 +00:00
|
|
|
) cargotoml.dependencies or {});
|
2019-12-13 15:37:19 +00:00
|
|
|
in
|
2019-12-17 15:55:52 +00:00
|
|
|
lib.mapAttrs (_: tomlDependencies) cargotomls;
|
|
|
|
|
2019-11-19 19:30:49 +00:00
|
|
|
# A very minimal 'src' which makes cargo happy nonetheless
|
|
|
|
dummySrc =
|
|
|
|
{ cargoconfig # string
|
|
|
|
, cargotomls # attrset
|
|
|
|
, cargolock # attrset
|
|
|
|
, patchedSources # list of paths that should be copied to the output
|
|
|
|
}:
|
2019-07-30 10:41:32 +00:00
|
|
|
let
|
2019-08-28 17:21:13 +00:00
|
|
|
config = writeText "config" cargoconfig;
|
2019-10-08 09:57:16 +00:00
|
|
|
cargolock' = builtinz.writeTOML "Cargo.lock" cargolock;
|
2019-10-09 08:34:40 +00:00
|
|
|
fixupCargoToml = cargotoml:
|
2019-11-19 19:30:49 +00:00
|
|
|
let
|
|
|
|
attrs =
|
|
|
|
# Since we pretend everything is a lib, we remove any mentions
|
|
|
|
# of binaries
|
|
|
|
removeAttrs cargotoml [ "bin" "example" "lib" "test" "bench" ];
|
|
|
|
in
|
|
|
|
attrs // lib.optionalAttrs (lib.hasAttr "package" attrs) {
|
|
|
|
package = removeAttrs attrs.package [ "build" ];
|
|
|
|
};
|
2019-10-30 14:45:53 +00:00
|
|
|
|
|
|
|
# a list of tuples from member to cargo toml:
|
|
|
|
# "foo-member:/path/to/toml bar:/path/to/other-toml"
|
|
|
|
cargotomlss = lib.mapAttrsToList
|
2019-11-19 19:30:49 +00:00
|
|
|
(k: v: "${k}:${builtinz.writeTOML "Cargo.toml" (fixupCargoToml v)}")
|
|
|
|
cargotomls;
|
2019-08-28 17:21:13 +00:00
|
|
|
|
2019-07-30 10:41:32 +00:00
|
|
|
in
|
2019-11-19 19:30:49 +00:00
|
|
|
runCommand "dummy-src"
|
|
|
|
{ inherit patchedSources cargotomlss; }
|
|
|
|
''
|
|
|
|
mkdir -p $out/.cargo
|
|
|
|
${lib.optionalString (! isNull cargoconfig) "cp ${config} $out/.cargo/config"}
|
|
|
|
cp ${cargolock'} $out/Cargo.lock
|
|
|
|
|
|
|
|
for p in $patchedSources; do
|
|
|
|
echo "Copying patched source $p to $out..."
|
|
|
|
cp -R "$p" "$out/"
|
|
|
|
done
|
|
|
|
|
|
|
|
for tuple in $cargotomlss; do
|
|
|
|
member="''${tuple%%:*}"
|
|
|
|
cargotoml="''${tuple##*:}"
|
|
|
|
|
|
|
|
final_dir="$out/$member"
|
|
|
|
mkdir -p "$final_dir"
|
|
|
|
final_path="$final_dir/Cargo.toml"
|
|
|
|
cp $cargotoml "$final_path"
|
|
|
|
|
|
|
|
# make sure cargo is happy
|
|
|
|
pushd $out/$member > /dev/null
|
|
|
|
mkdir -p src
|
|
|
|
touch src/lib.rs
|
2020-01-21 13:14:30 +00:00
|
|
|
# pretend there's a `build.rs`, otherwise cargo doesn't build
|
|
|
|
# the `[build-dependencies]`. Custom locations of build scripts
|
|
|
|
# aren't an issue because we strip the `build` field in
|
|
|
|
# `fixupCargoToml`; so cargo always thinks there's a build
|
|
|
|
# script which is `./build.rs`.
|
|
|
|
echo 'fn main(){}' > build.rs
|
2019-11-19 19:30:49 +00:00
|
|
|
popd > /dev/null
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
|
|
|
|
mkPackages = cargolock:
|
|
|
|
lib.foldl' lib.recursiveUpdate {} (
|
|
|
|
map (p: { ${p.name} = { ${p.version} = p; }; })
|
|
|
|
cargolock.package
|
|
|
|
);
|
|
|
|
|
|
|
|
directDependencies = cargolock: name: version:
|
|
|
|
let
|
|
|
|
packages = mkPackages cargolock;
|
|
|
|
package = packages.${name}.${version};
|
|
|
|
in
|
2019-06-28 15:16:53 +00:00
|
|
|
lib.optionals (builtins.hasAttr "dependencies" package)
|
|
|
|
(map parseDependency' package.dependencies);
|
|
|
|
|
2019-11-19 19:30:49 +00:00
|
|
|
transitiveDeps = cargolock: name: version:
|
|
|
|
let
|
|
|
|
wrap = p:
|
|
|
|
{
|
|
|
|
key = "${p.name}-${p.version}";
|
|
|
|
package = p;
|
|
|
|
};
|
|
|
|
packages = mkPackages cargolock;
|
|
|
|
in
|
2019-06-27 15:49:59 +00:00
|
|
|
builtins.genericClosure
|
2019-11-19 19:30:49 +00:00
|
|
|
{
|
|
|
|
startSet = [ (wrap packages.${name}.${version}) ];
|
|
|
|
operator = p: map (dep: wrap (packages.${dep.name}.${dep.version})) (
|
|
|
|
(
|
|
|
|
lib.optionals (builtins.hasAttr "dependencies" p.package)
|
|
|
|
(map parseDependency' p.package.dependencies)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
2019-06-27 15:49:59 +00:00
|
|
|
|
2019-11-19 19:30:49 +00:00
|
|
|
# turns "<package> <version> ..." into { name = <package>, version = <version>; }
|
|
|
|
parseDependency' = str:
|
|
|
|
let
|
|
|
|
components = lib.splitString " " str;
|
|
|
|
in
|
2019-06-28 14:45:41 +00:00
|
|
|
{ name = lib.elemAt components 0; version = lib.elemAt components 1; };
|
2019-06-25 15:36:22 +00:00
|
|
|
}
|