Skip to content

cargoSetupHook incorrect platform comparison #355803

@apt-install-coffee

Description

Describe the bug

cargoSetupHook:70
compares stdenv.buildPlatform to stdenv.hostPlatform when writing the desired config.toml, but cargoSetupHook is host tooling, so buildPlatorm and hostPlatform are going to be the same value even when cross compiling; we should be comparing hostPlatform and targetPlatform to see if our target package is being cross compiled.

The same goes for line 74: stdenv.hostPlatform.isStatic should be targetPlatform.isStatic.

Steps To Reproduce

Steps to reproduce the behavior:

  1. cross compile a rust package to a host not already supported in the target's .cargo/config.toml file, and keep the failed result e.g.
nix build .#nixosConfigurations.<SYSTEM>.pkgs.pkgsCross.aarch64-multiplatform-musl.uutils-coreutils --keep-failed
  1. output the resulting cargo.toml file:
[root@jordan:/tmp/nix-build-uutils-coreutils-aarch64-unknown-linux-musl-0.0.27.drv-1]# cat .cargo/config.toml
[source."crates-io"]
"replace-with" = "vendored-sources"

[source."vendored-sources"]
"directory" = "/build/uutils-coreutils-0.0.27-vendor.tar.gz"


    [target."x86_64-unknown-linux-gnu"]
"linker" = "/nix/store/n89l3mzh49hgwvd4dmgmd90h59a7j7lq-gcc-wrapper-13.3.0/bin/cc"

"rustflags" = [ "-C", "target-feature=-crt-static" ]

Expected behavior

Target information should also be contained in the cargo.toml file, like so:

[root@jordan:/tmp]# cat nix-build-uutils-coreutils-aarch64-unknown-linux-musl-0.0.27.drv-2/.cargo/config.toml
[source."crates-io"]
"replace-with" = "vendored-sources"

[source."vendored-sources"]
"directory" = "/build/uutils-coreutils-0.0.27-vendor.tar.gz"


    [target."x86_64-unknown-linux-gnu"]
"linker" = "/nix/store/n89l3mzh49hgwvd4dmgmd90h59a7j7lq-gcc-wrapper-13.3.0/bin/cc"
[target."aarch64-unknown-linux-musl"]
"linker" = "/nix/store/0655rcnl111my4fyyrhrkk9hdw6r7yrf-aarch64-unknown-linux-musl-gcc-wrapper-13.3.0/bin/aarch64-unknown-linux-musl-cc"

"rustflags" = [ "-C", "target-feature=-crt-static" ]

Additional context

  • an overlay, modifying the cargoSetupHook nativeBuildInput to
    nativeBuildInputs = [ final.pkgsHostHost.rustPlatform.cargoSetupHook ]; works around the problem.
  • in order to successfully cross-compile packages using cargoSetupHooks, a fix for ‎cargoSetupPostUnpackHook writes config.toml in incorrect directory #355788 is also needed.
  • I am not 100% certain about scenarios where we're using rustPlatform to build rust tooling; where a package's target and host are different, but given cargo is still building the correct target, I'd suggest just changing cargoSetupHook to targetPlatform shouldn't cause any issues.

Metadata

  • system: "x86_64-linux"
  • host os: Linux 6.11.5, NixOS, 24.11 (Vicuna), 24.11.20241109.76612b1
  • multi-user?: yes
  • sandbox: yes
  • version: nix-env (Lix, like Nix) 2.91.1 System type: x86_64-linux Additional system types: aarch64-linux, i686-linux Features: gc, signed-caches System configuration file: /etc/nix/nix.conf User configuration files: /home/jordan/.config/nix/nix.conf:/etc/xdg/nix/nix.conf:/home/jordan/.nix-profile/etc/xdg/nix/nix.conf:/home/jordan/.local/state/nix/profile/etc/xdg/nix/nix.conf:/home/jordan/.local/state/nix/profile/etc/xdg/nix/nix.conf:/etc/profiles/per-user/jordan/etc/xdg/nix/nix.conf:/nix/var/nix/profiles/default/etc/xdg/nix/nix.conf:/run/current-system/sw/etc/xdg/nix/nix.conf Store directory: /nix/store State directory: /nix/var/nix Data directory: /nix/store/k1iys4jifr01ql4bzafl6ny71qzw32wz-lix-2.91.1/share
  • nixpkgs: /nix/store/0sxsfzswhjck7f4sbsvznz285s4i7y64-source

Notify maintainers

@mmlb @winterqt @figsoda


Note for maintainers: Please tag this issue in your PR.


Add a 👍 reaction to issues you find important.

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions