Hello,
I try to build a static rust binary using pkgsStatic
. This works when using nix build
but not when trying the same with nix develop -i --command bash -c "cargo build"
due to the linker cc
not found.
It is not clear to me if passing stdenv = pkgs.pkgsStatic.stdenv
to mkShell
is the right thing to do. I also don’t know what is the difference between pkgsStatic
and pkgsMusl
.
Does someone has a hint what I am missing? Goal is that the same environment used to build the package with nix build
is available in the development shell to run cargo build.
Thank you.
{
description = "Example";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
outputs = { self, nixpkgs }:
let
system = "x86_64-linux";
pkgs = import nixpkgs { inherit system; };
nativeBuildInputs = [
pkgs.pkg-config
];
buildInputs = [
pkgs.pkgsStatic.openssl
];
cargoToml = (pkgs.lib.importTOML ./Cargo.toml);
example = pkgs.pkgsStatic.rustPlatform.buildRustPackage {
pname = "example";
version = cargoToml.package.version;
src = pkgs.lib.cleanSource ./.;
cargoLock.lockFile = ./Cargo.lock;
inherit nativeBuildInputs;
inherit buildInputs;
};
in
{
packages.${system} = {
default = example;
};
devShells.${system} = {
default = pkgs.mkShell.override { stdenv = pkgs.pkgsStatic.stdenv; } {
inherit nativeBuildInputs;
inherit buildInputs;
packages = [
pkgs.cargo
pkgs.rustfmt
pkgs.rustc
pkgs.rust-analyzer
pkgs.clippy
];
};
};
};
}
Output:
markus@nbmf:~/dev/nix/static-example (main) $: nix develop -i --command bash -c "cargo build"
Compiling proc-macro2 v1.0.86
Compiling libc v0.2.155 ] 0/25: proc-macro2(build.rs)
Compiling openssl v0.10.66
Compiling openssl-sys v0.9.103
error: linker `cc` not found ] 9/25: proc-macro2(build.rs), openssl(build.rs), libc(build.rs), openssl-sys(build.rs)
|
= note: No such file or directory (os error 2)
error: could not compile `openssl` (build script) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `proc-macro2` (build script) due to 1 previous error(build.rs), openssl-sys(build.rs)
error: could not compile `libc` (build script) due to 1 previous errorssl-sys(build.rs)
error: could not compile `openssl-sys` (build script) due to 1 previous error