Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using fenix to suply cargo and rust to a makeRustPlatform results in "error: linker cc not found" #95

Open
wucke13 opened this issue Feb 24, 2023 · 7 comments
Labels
bug Something isn't working question Further information is requested

Comments

@wucke13
Copy link

wucke13 commented Feb 24, 2023

I tried both "CARGO_TARGET_${ shoutTarget }_LINKER" ="${p.stdenv.cc.targetPrefix}cc"; and RUSTFLAGS = "-C linker=dasdsa${p.stdenv.cc.targetPrefix}cc"; in the call to buildRustPackage, but nonetheless cargo tries to call cc.
Roughly speaking, this is my derivation:

{
my-package = let
  p = pkgs.pkgsStatic;

  # the rust target of `p`
  target = p.rust.toRustTargetSpec p.targetPlatform;

  # the rust target in shout case
  shoutTarget = builtins.replaceStrings ["-"] ["_"] (pkgs.lib.toUpper target);

  # suitable Rust toolchain
  toolchain = with inputs.fenix.packages.${system}; combine [
    stable.cargo
    stable.rustc
    targets.${target}.stable.rust-std
  ];
  customRustPlatform = p.makeRustPlatform {
    cargo = toolchain;
    rustc = toolchain;
  };
 in  customRustPlatform.buildRustPackage {
    # ....
  };
}
@wucke13 wucke13 changed the title using fenix to suplly cargo and rust to a makeRustPlatform results in error: linker cc not found using fenix to suply cargo and rust to a makeRustPlatform results in error: linker cc not found Feb 24, 2023
@wucke13 wucke13 changed the title using fenix to suply cargo and rust to a makeRustPlatform results in error: linker cc not found using fenix to suply cargo and rust to a makeRustPlatform results in "error: linker cc not found" Feb 24, 2023
@figsoda figsoda added bug Something isn't working question Further information is requested labels Feb 24, 2023
@figsoda
Copy link
Member

figsoda commented Feb 24, 2023

It seems to have something to do with using makeRustPlatform from pkgsStatic, I was able to build this with naersk using CARGO_BUILD_TARGET using the flake below, the same thing should also be possible with crane, but I'm not sure about vanilla makeRustPlatform since it has --target baked in

{
  inputs = {
    fenix = {
      url = "github:nix-community/fenix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    flake-utils.url = "github:numtide/flake-utils";
    naersk = {
      url = "github:nix-community/naersk";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    nixpkgs.url = "nixpkgs/nixos-unstable";
  };

  outputs = { self, fenix, flake-utils, naersk, nixpkgs }:
    flake-utils.lib.eachDefaultSystem (system: {
      packages.default =
        let
          inherit (nixpkgs) lib;
          pkgs = nixpkgs.legacyPackages.${system}.pkgsStatic;
          target = pkgs.rust.toRustTargetSpec pkgs.stdenv.targetPlatform;
          toolchain = with fenix.packages.${system}; combine [
            stable.cargo
            stable.rustc
            targets.${target}.stable.rust-std
          ];
          shoutTarget = builtins.replaceStrings [ "-" ] [ "_" ] (lib.toUpper target);
        in

        (naersk.lib.${system}.override {
          cargo = toolchain;
          rustc = toolchain;
        }).buildPackage {
          src = self;
          nativeBuildInputs = [ pkgs.stdenv.cc ];
          CARGO_BUILD_TARGET = target;
          "CARGO_TARGET_${shoutTarget}_LINKER" = "${pkgs.stdenv.cc.targetPrefix}cc";
        };
    });
}

@figsoda
Copy link
Member

figsoda commented Feb 24, 2023

I think cargo was just trying to run cc regardless, I was able to build it with makeRustPlatform by adding p.buildPackages.stdenv.cc to nativeBuildInputs (without needing to set the linker manually)

@wucke13
Copy link
Author

wucke13 commented Feb 26, 2023

@figsoda The issue is, that sometimes cc is not named cc but has the targetPrefix, but cargo still tries to call cc. I have a suspicion though: maybe cc is used when compiing all derive macros (as they are not cross compiled), and only then later CARGO_TARGET_..._LINKER is used for the final build?
Edit: yes, this seems to be the case.

@figsoda
Copy link
Member

figsoda commented Feb 26, 2023

proc macros does sound like a reason to use build platform cc, does using buildPackages.stdenv.cc fix the issue for you?

@wucke13
Copy link
Author

wucke13 commented Feb 26, 2023

Yeah, with it I get further but still no complete build. Again a cc error, but this time for the final linkage:

       >   = note: /nix/store/pnajkjw1fz03z1x27ski1qbx9n3hd4df-binutils-2.39/bin/ld: /nix/store/s652h8znmysdbxcmwlzzcwrj6bb1xyln-gcc-11.3.0/lib/gcc/x86_64-unknown-linux-gnu/11.3.0/crtbeginT.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a shared object
       >           /nix/store/pnajkjw1fz03z1x27ski1qbx9n3hd4df-binutils-2.39/bin/ld: failed to set dynamic section sizes: bad value
       >           collect2: error: ld returned 1 exit status

I believe this again is a musl libc mixed with glibc gcc problem.

@figsoda
Copy link
Member

figsoda commented Feb 26, 2023

Can I see the nix expression you are working with?

@wucke13
Copy link
Author

wucke13 commented Feb 28, 2023

@figsoda Sure! Here, take a look at the rosenpass-static output of https://github.com/rosenpass/rosenpass/blob/dev/wucke13/flake.nix#L89

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants