Nix-shell rust hello world ld linkage issue

Hi all, I was looking to explore rust by following this rust-cli guide https://rust-cli.github.io/book/index.html.

So I looked at https://nixos.wiki/wiki/Rust to see what was recommended in configuring a nix shell for rust (never having used rust before).

So to start with the simplest thing:

{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
  buildInputs = with pkgs; [ rustc cargo gcc ];
}

And then generated a new project:

% cargo new grrs
% cd grrs
% cargo run          
   Compiling grrs v0.1.0 (/Users/ldeck/Documents/dev/rust/cli-tut/grrs)
error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "-arch" "x86_64" "/Users/ldeck/Documents/dev/rust/cli-tut/grrs/target/debug/deps/grrs-b40a810d5eeb5159.16y3hmcuvg15q72g.rcgu.o" "/Users/ldeck/Documents/dev/rust/cli-tut/grrs/target/debug/deps/grrs-b40a810d5eeb5159.1rpdnn5bvbfk5hvb.rcgu.o" "/Users/ldeck/Documents/dev/rust/cli-tut/grrs/target/debug/deps/grrs-b40a810d5eeb5159.26tn9bv6r2mrnlpn.rcgu.o" "/Users/ldeck/Documents/dev/rust/cli-tut/grrs/target/debug/deps/grrs-b40a810d5eeb5159.2nmut3stedhsdyij.rcgu.o" "/Users/ldeck/Documents/dev/rust/cli-tut/grrs/target/debug/deps/grrs-b40a810d5eeb5159.344eaeidxbex7yfr.rcgu.o" "/Users/ldeck/Documents/dev/rust/cli-tut/grrs/target/debug/deps/grrs-b40a810d5eeb5159.39vgo5bc3f8n3trf.rcgu.o" "/Users/ldeck/Documents/dev/rust/cli-tut/grrs/target/debug/deps/grrs-b40a810d5eeb5159.3qrji5g33xnggkeb.rcgu.o" "/Users/ldeck/Documents/dev/rust/cli-tut/grrs/target/debug/deps/grrs-b40a810d5eeb5159.4898hz1lezjygwi3.rcgu.o" "/Users/ldeck/Documents/dev/rust/cli-tut/grrs/target/debug/deps/grrs-b40a810d5eeb5159.4t58cw4fxnm1x60y.rcgu.o" "/Users/ldeck/Documents/dev/rust/cli-tut/grrs/target/debug/deps/grrs-b40a810d5eeb5159.4vmchlw4047apcs8.rcgu.o" "/Users/ldeck/Documents/dev/rust/cli-tut/grrs/target/debug/deps/grrs-b40a810d5eeb5159.2df5y79clgj6l2ri.rcgu.o" "-L" "/Users/ldeck/Documents/dev/rust/cli-tut/grrs/target/debug/deps" "-L" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/libstd-d43b1180a7c500d2.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-ae539223d447dfc6.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/libobject-11a458d9b32a239c.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/libmemchr-34cc6ee434399709.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/libaddr2line-e4ce48db41321be4.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/libgimli-925f8e621e87c5d1.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/libstd_detect-f98ef5df1f347ad0.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-8ede71df0f2d7c4f.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-0a273c8fcff748a9.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-12eb4f87886f4dd7.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/libunwind-93f5cbe2b90daf72.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-8032011eb1f3eb82.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/liblibc-22466d52281abdf1.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/liballoc-69c5dbacefc7f8b1.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-de984f3632fc3c42.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/libcore-ec0cd9f6516ee01f.rlib" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-eefc3cdb499abe56.rlib" "-lSystem" "-lresolv" "-lc" "-lm" "-liconv" "-L" "/nix/store/2dhyhzx18w0kqb1qq3h4wp1a0ng9dr3q-rustc-1.56.1/lib/rustlib/x86_64-apple-darwin/lib" "-o" "/Users/ldeck/Documents/dev/rust/cli-tut/grrs/target/debug/deps/grrs-b40a810d5eeb5159" "-Wl,-dead_strip" "-nodefaultlibs"
  = note: ld: library not found for -liconv
          clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: could not compile `grrs` due to previous error

This problem seems familiar. I’ve seen the clang error before (macOS):

which clang
/nix/store/gsfxxazc51sx6vjdrz6cq8s19x7h5mwh-clang-wrapper-7.1.0/bin/clang

I’ve looked at impossible to link (ld / clang) liboping on macOS Big Sur 11 Beta · Issue #91748 · NixOS/nixpkgs · GitHub and tried the more complete nix-shell config:

{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
  nativeBuildInputs = with pkgs; [ rustc cargo gcc ];
  buildInputs = with pkgs; [ rustfmt clippy ];

  RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
}

No difference. Any known work-arounds? Thanks!

nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-darwin"`
 - host os: `Darwin 21.2.0, macOS 10.16`
 - multi-user?: `no`
 - sandbox: `no`
 - version: `nix-env (Nix) 2.5.1`
 - channels(ldeck): `"nixpkgs-22.05pre334669.f366af7a1b3"`
 - nixpkgs: `/Users/ldeck/.nix-defexpr/channels/nixpkgs`
1 Like

Add libiconv to your buildInputs on Darwin.

Ah, thanks @reckenrode! I was distracted by the clang-7 thinking it was using an old cc/gcc compiler.

So what buildInput would be equivalent on linux?

From what I understand, it’s a Darwin-specific issue. I don’t think including libiconv in your buildInputs would hurt on Linux, but if you want restrict it to just Darwin, you can use something like the following for your buildInputs:

buildInputs = with pkgs; [ rustfmt clippy ] ++ lib.optional stdenv.isDarwin libiconv;
1 Like