When trying to install a rust development environment utilizing the standard configuration.nix, the build chain is not fully installed or initialized, resulting in a linker error.
environment.systemPackages = [
rustc
cargo
];
environment.systemPackages = with pkgs; [
rustc
cargo
];
sudo nixos-rebuild switch
Now, you can create a project: cargo new rusttest
But when you try to build it: cargo build
I get the following error:
error: linker `cc` not found
|
= note: No such file or directory (os error 2)
error: could not compile `rusttest` due to previous error\
I'm aware that utilizing the nix-shell (nix-shell -p rustc cargo
) will result in a working environment, and for now, that's my workaround. However, I would like the standard configuration method to work.
Suggestions are appreciated.
While it is true that you can install compilers globally to environment.systemPackages
, this is actually not the "standard configuration" for development tools. The recommended method is in fact nix-shell
(or nix-develop
for a flake-based environment), which was specifically designed to create cleanly separated development environments.
For example, you can place a file named shell.nix
in your project folder with this code:
{ pkgs ? import <nixpkgs> {}
}: pkgs.mkShell {
nativeBuildInputs = with pkgs.buildPackages; [
cargo
rustc
];
}
Then, you can run nix-shell
with no arguments from your project directory to get a consistent Rust development environment each time, while still granting you the ability to use different tools and configurations for different projects without creating conflicts. Because it runs in user-space and has such reproducible results (including on non-NixOS hosts) it can even be safely checked into source control for others. Reproducibility is what Nix is all about!
This will also install the default C compiler into your shell, which as of this writing is gcc-12.3.0
for nixos-23.11
, but you can swap it out as needed for a different version, a cross-compiler, clang, etc.
Don't worry about wasting disk space, either: as long as two nix-shell
s use the exact same version and configuration of the same tool, they will typically share the same package in /nix/store
. If things change and packages go unused, the garbage collector will eventually remove them.