Compile static rust binary with pkgsStatic also in devshell

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

So I made some progress (I guess?!) by adapting the flake.nix to the following:

{
    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.pkgsStatic.mkShell.override { stdenv = pkgs.pkgsStatic.clangStdenv;} {
                name = "shell";
                inherit nativeBuildInputs;
                inherit buildInputs;
                packages = [
                    pkgs.pkgsStatic.rustPlatform.rust.cargo
                    pkgs.rustfmt
                    pkgs.pkgsStatic.rustPlatform.rust.rustc
                    pkgs.rust-analyzer
                    pkgs.clippy
                ];
            };
        };
    };

}

At least with that I can compile successful in that environment using the same command which is used in the log of nix build -L. But that seems not very practical.

CC_X86_64_UNKNOWN_LINUX_MUSL=/nix/store/vsn4c10s241prxrh0zqk8pvzm41bv7by-x86_64-unknown-linux-musl-gcc-wrapper-13.3.0/bin/x86_64-unknown-linux-musl-cc CXX_X86_64_UNKNOWN_LINUX_MUSL=/nix/store/vsn4c10s241prxrh0zqk8pvzm41bv7by-x86_64-unknown-linux-musl-gcc-wrapper-13.3.0/bin/x86_64-unknown-linux-musl-c++ CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=/nix/store/vsn4c10s241prxrh0zqk8pvzm41bv7by-x86_64-unknown-linux-musl-gcc-wrapper-13.3.0/bin/x86_64-unknown-linux-musl-cc CC_X86_64_UNKNOWN_LINUX_GNU=/nix/store/kp2j7yn0wzwq5piy494r54dafrh83s6s-gcc-wrapper-13.3.0/bin/cc CXX_X86_64_UNKNOWN_LINUX_GNU=/nix/store/kp2j7yn0wzwq5piy494r54dafrh83s6s-gcc-wrapper-13.3.0/bin/c++ CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=/nix/store/kp2j7yn0wzwq5piy494r54dafrh83s6s-gcc-wrapper-13.3.0/bin/cc CARGO_BUILD_TARGET=x86_64-unknown-linux-gnu HOST_CC=/nix/store/kp2j7yn0wzwq5piy494r54dafrh83s6s-gcc-wrapper-13.3.0/bin/cc HOST_CXX=/nix/store/kp2j7yn0wzwq5piy494r54dafrh83s6s-gcc-wrapper-13.3.0/bin/c++ cargo build -j 8 --target x86_64-unknown-linux-musl --offline --profile release

Using pkgs.pkgsStatic.cargo is no option as this gives the following error:

error: Package ‘cargo-1.78.0’ in /nix/store/69pygfzcwihik1l871avmg5rgcsngf7f-source/pkgs/development/compilers/rust/cargo.nix:74 is marked as broken, refusing to evaluate