wordy backstory
If your Rust project uses openssl from nixpkgs and has not been linked correctly you may see an error like error while loading shared libraries: libssl.so.3 when trying to execute it.
I saw this when upgrading to Rust 1.90 by way of fenix stable toolchain in my projects. Rust 1.90 rustc ships their own lld for a linker. (See the official release notes for more!)
I tried a bunch of tweaks to my nix expressions to correctly link and compile against openssl using the upstream provided rustc and ldd.
25 commits later. No working binary. ![]()
The Rust release notes point out you can configure rustc to use a provided lld.
I tried that. It worked! ![]()
step by step guide
Here is a guide to help you configure the Rust compiler to use nixpkgs lld and openssl.
- Modify your expression to pass the following code gen flag to
rustc:-Clink-self-contained=-linker. - Provide
llvmPackages.bintoolsto your package build- The Rust page on NixOS wiki says to use this instead of
llddirectly. I don’t know why
- The Rust page on NixOS wiki says to use this instead of
Here is a concrete example package expression link-me.nix that puts those steps together:
{
lib,
rustPlatform,
pkg-config,
openssl,
stdenv,
fenix,
llvmPackages,
}:
rustPlatform.buildRustPackage rec {
pname = "link-me";
version = "0.0.0";
src = ./.;
cargoHash = "sha256-iCQ0L2efC3x2yUi/jQ7rOQXDcAvxOpcGyICjpqBjxN4=";
nativeBuildInputs = [
pkg-config
fenix.stable.rustc
fenix.stable.cargo
llvmPackages.bintools
];
buildInputs = [
openssl
];
OPENSSL_NO_VENDOR = 1;
RUSTFLAGS = "-Clink-self-contained=-linker";
doCheck = false;
}