Skip to content

Commit

Permalink
feat: use the revision from Cargo.lock
Browse files Browse the repository at this point in the history
  • Loading branch information
yusdacra committed Jan 4, 2021
1 parent a76924c commit a70e790
Showing 1 changed file with 128 additions and 118 deletions.
246 changes: 128 additions & 118 deletions lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,36 @@ rec
mkVersions = cargolock:
if builtins.hasAttr "metadata" cargolock then

# TODO: this should nub by <pkg-name>-<pkg-version>
# TODO: this should nub by <pkg-name>-<pkg-version>
(
lib.concatMap (
x:
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 []))
(
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 [ ]))

)
)
cargolock.package
)
else if builtins.hasAttr "package" cargolock then
map (
p:
map
(
p:
{
inherit (p) name version;
sha256 = p.checksum;
}
) (builtins.filter (builtins.hasAttr "checksum") cargolock.package)
else [];
)
(builtins.filter (builtins.hasAttr "checksum") 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 [])
Expand All @@ -50,13 +53,13 @@ rec
version = lib.elemAt components 1;
mdk = mkMetadataKey name version;
in
lib.optional (builtins.hasAttr mdk cargolock.metadata)
(
let
sha256 = cargolock.metadata.${mkMetadataKey name version};
in
{ inherit name version sha256; }
);
lib.optional (builtins.hasAttr mdk cargolock.metadata)
(
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
Expand All @@ -78,38 +81,45 @@ rec
{ cargotomls
, cargolock
}:
let
tomlDependencies = cargotoml:
lib.filter (x: ! isNull x) (
let
tomlDependencies = cargotoml:
lib.filter (x: ! isNull x) (
lib.mapAttrsToList
(k: v:
if ! (lib.isAttrs v && builtins.hasAttr "git" v)
then null
else lib.filterAttrs (n: _: n == "rev" || n == "tag" || n == "branch") v //
{ name = k;
url = v.git;
key = v.rev or v.tag or v.branch or
(throw "No 'rev', 'tag' or 'branch' available to specify key");
else
let
query = p: p.name == k && (lib.substring 0 (4 + lib.stringLength v.git) p.source) == "git+${v.git}";
extractRevision = url: lib.last (lib.splitString "#" url);
parseLock = lock: rec { inherit (lock) name source; revision = extractRevision source; };
packageLocks = builtins.map parseLock (lib.filter query cargolock.package);
matchByName = lib.findFirst (p: p.name == k) null packageLocks;
matchByRev =
if (isNull (v.rev or null))
then null
else lib.findFirst (p: lib.substring 0 7 p.revision == lib.substring 0 7 v.rev) null packageLocks;
val = if (isNull (v.rev or null)) then v // { rev = matchByRev.revision or matchByName.revision; } else v;
in
lib.filterAttrs (n: _: n == "rev" || n == "tag" || n == "branch") val //
{
name = k;
url = val.git;
key = val.rev or val.tag or val.branch or
(throw "No 'rev', 'tag' or 'branch' available to specify key, nor a git revision was found in Cargo.lock");
checkout = builtins.fetchGit ({
url = v.git;
} // lib.optionalAttrs (v ? rev) {
rev = let
query = p: p.name == k && (lib.substring 0 (4 + lib.stringLength v.git) p.source) == "git+${v.git}";
extractRevision = url: lib.last (lib.splitString "#" url);
parseLock = lock: rec { inherit (lock) name source; revision = extractRevision source; };
packageLocks = builtins.map parseLock (lib.filter query cargolock.package);
match = lib.findFirst (p: lib.substring 0 7 p.revision == lib.substring 0 7 v.rev) null packageLocks;
in
if ! (isNull match) then match.revision else v.rev;
} // lib.optionalAttrs (v ? branch) {
ref = v.branch;
} // lib.optionalAttrs (v ? tag) {
ref = v.tag;
url = val.git;
} // lib.optionalAttrs (val ? rev) {
rev = val.rev;
} // lib.optionalAttrs (val ? branch) {
ref = val.branch;
} // lib.optionalAttrs (val ? tag) {
ref = val.tag;
});
}
) cargotoml.dependencies or {});
in
lib.mapAttrs (_: tomlDependencies) cargotomls;
) cargotoml.dependencies or { });
in
lib.mapAttrs (_: tomlDependencies) cargotomls;

# A very minimal 'src' which makes cargo happy nonetheless
dummySrc =
Expand All @@ -118,66 +128,66 @@ rec
, cargolock # attrset
, patchedSources # list of paths that should be copied to the output
}:
let
config = writeText "config" cargoconfig;
cargolock' = builtinz.writeTOML "Cargo.lock" cargolock;
fixupCargoToml = cargotoml:
let
attrs =
# Since we pretend everything is a lib, we remove any mentions
# of binaries
removeAttrs cargotoml [ "bin" "example" "lib" "test" "bench" "default-run" ]
// lib.optionalAttrs (builtins.hasAttr "package" cargotoml) ({ package = removeAttrs cargotoml.package [ "default-run" ] ; })
;
in
attrs // lib.optionalAttrs (lib.hasAttr "package" attrs) {
package = removeAttrs attrs.package [ "build" ];
};

# a list of tuples from member to cargo toml:
# "foo-member:/path/to/toml bar:/path/to/other-toml"
cargotomlss = lib.mapAttrsToList
(k: v: "${k}:${builtinz.writeTOML "Cargo.toml" (fixupCargoToml v)}")
cargotomls;

in
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
# 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
popd > /dev/null
done
'';
let
config = writeText "config" cargoconfig;
cargolock' = builtinz.writeTOML "Cargo.lock" cargolock;
fixupCargoToml = cargotoml:
let
attrs =
# Since we pretend everything is a lib, we remove any mentions
# of binaries
removeAttrs cargotoml [ "bin" "example" "lib" "test" "bench" "default-run" ]
// lib.optionalAttrs (builtins.hasAttr "package" cargotoml) ({ package = removeAttrs cargotoml.package [ "default-run" ]; })
;
in
attrs // lib.optionalAttrs (lib.hasAttr "package" attrs) {
package = removeAttrs attrs.package [ "build" ];
};

# a list of tuples from member to cargo toml:
# "foo-member:/path/to/toml bar:/path/to/other-toml"
cargotomlss = lib.mapAttrsToList
(k: v: "${k}:${builtinz.writeTOML "Cargo.toml" (fixupCargoToml v)}")
cargotomls;

in
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
# 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
popd > /dev/null
done
'';

mkPackages = cargolock:
lib.foldl' lib.recursiveUpdate {} (
lib.foldl' lib.recursiveUpdate { } (
map (p: { ${p.name} = { ${p.version} = p; }; })
cargolock.package
);
Expand All @@ -187,8 +197,8 @@ rec
packages = mkPackages cargolock;
package = packages.${name}.${version};
in
lib.optionals (builtins.hasAttr "dependencies" package)
(map parseDependency' package.dependencies);
lib.optionals (builtins.hasAttr "dependencies" package)
(map parseDependency' package.dependencies);

transitiveDeps = cargolock: name: version:
let
Expand All @@ -199,21 +209,21 @@ rec
};
packages = mkPackages cargolock;
in
builtins.genericClosure
{
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)
)
);
};
builtins.genericClosure
{
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)
)
);
};

# turns "<package> <version> ..." into { name = <package>, version = <version>; }
parseDependency' = str:
let
components = lib.splitString " " str;
in
{ name = lib.elemAt components 0; version = lib.elemAt components 1; };
{ name = lib.elemAt components 0; version = lib.elemAt components 1; };
}

0 comments on commit a70e790

Please sign in to comment.