Skip to content

Commit

Permalink
rustPlatform.importCargoLock: add support for git dependencies that u…
Browse files Browse the repository at this point in the history
…se workspace inheritance

Rust 1.64.0 added support for Cargo workspace inheritance, which allows
for workspace members to inherit values such as dependency version
constraints or package metadata information from their workspaces [0].

This works by having workspace members specify a value as a table, with
`workspace` set to true. Thus, supporting this in importCargoLock is as
simple as walking the workspace member's Cargo.toml, replacing inherited
values with their workspace counterpart.

This is also what a forthcoming Cargo release will do [1], but we can get
ahead of it ;)

[0]: https://blog.rust-lang.org/2022/09/22/Rust-1.64.0.html#cargo-improvements-workspace-inheritance-and-multi-target-builds
[1]: rust-lang/cargo#11414
  • Loading branch information
winterqt committed Feb 19, 2023
1 parent a3c24f9 commit c2bcd63
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 2 deletions.
12 changes: 11 additions & 1 deletion pkgs/build-support/rust/import-cargo-lock.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ fetchgit, fetchurl, lib, runCommand, cargo, jq }:
{ fetchgit, fetchurl, lib, writers, python3Packages, runCommand, cargo, jq }:

{
# Cargo lock file
Expand Down Expand Up @@ -92,6 +92,11 @@ let
sha256 = pkg.checksum;
};

# Replaces values inherited by workspace members.
replaceWorkspaceValues = writers.writePython3 "replace-workspace-values"
{ libraries = [ python3Packages.toml ]; flakeIgnore = [ "E501" ]; }
(builtins.readFile ./replace-workspace-values.py);

# Fetch and unpack a crate.
mkCrate = pkg:
let
Expand Down Expand Up @@ -170,6 +175,11 @@ let
cp -prvd "$tree/" $out
chmod u+w $out
if grep -q workspace "$out/Cargo.toml"; then
chmod u+w "$out/Cargo.toml"
${replaceWorkspaceValues} "$out/Cargo.toml" "${tree}/Cargo.toml"
fi
# Cargo is happy with empty metadata.
printf '{"files":{},"package":null}' > "$out/.cargo-checksum.json"
Expand Down
43 changes: 43 additions & 0 deletions pkgs/build-support/rust/replace-workspace-values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import sys
import toml

crate_manifest = toml.load(sys.argv[1])
workspace_manifest = toml.load(sys.argv[2])


def replace_key(table, section, key):
if "workspace" in table[key] and table[key]["workspace"] is True:
print("replacing " + key)

replaced = table[key]
del replaced["workspace"]

workspace_copy = workspace_manifest["workspace"][section][key]

if section == "dependencies":
if isinstance(workspace_copy, str):
replaced["version"] = workspace_copy
else:
replaced.update(workspace_copy)
elif section == "package":
table[key] = replaced = workspace_copy


def replace_dependencies(root):
for key in ["dependencies", "dev-dependencies", "build-dependencies"]:
if key in root:
for k, v in root[key].items():
replace_key(root[key], "dependencies", k)


for key in crate_manifest["package"].keys():
replace_key(crate_manifest["package"], "package", key)

replace_dependencies(crate_manifest)

if "target" in crate_manifest:
for key in crate_manifest["target"].keys():
replace_dependencies(crate_manifest["target"][key])

with open(sys.argv[1], "w") as f:
toml.dump(crate_manifest, f)
7 changes: 6 additions & 1 deletion pkgs/build-support/rust/test/import-cargo-lock/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ callPackage }:
{ callPackage, writers, python3Packages }:

# Build like this from nixpkgs root:
# $ nix-build -A tests.importCargoLock
Expand All @@ -11,4 +11,9 @@
gitDependencyTag = callPackage ./git-dependency-tag { };
gitDependencyBranch = callPackage ./git-dependency-branch { };
maturin = callPackage ./maturin { };
gitDependencyWorkspaceInheritance = callPackage ./git-dependency-workspace-inheritance {
replaceWorkspaceValues = writers.writePython3 "replace-workspace-values"
{ libraries = [ python3Packages.toml ]; flakeIgnore = [ "E501" ]; }
(builtins.readFile ../../replace-workspace-values.py);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
version = { workspace = true }

[dependencies]
foo = { workspace = true }
bar = "1.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{ replaceWorkspaceValues, runCommand }:

runCommand "git-dependency-workspace-inheritance-test" { } ''
cp --no-preserve=mode ${./crate.toml} "$out"
${replaceWorkspaceValues} "$out" ${./workspace.toml}
diff "$out" ${./want.toml}
''
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
version = "1.0.0"

[dependencies]
bar = "1.0.0"

[dependencies.foo]
version = "1.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace.package]
version = "1.0.0"

[workspace.dependencies]
foo = "1.0.0"

0 comments on commit c2bcd63

Please sign in to comment.