NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.32k stars 13.56k forks source link

Rustup with NixOS causes an cc linker error #103642

Closed Siilwyn closed 3 years ago

Siilwyn commented 3 years ago

Describe the bug Using Rustup to install Rust and then building a project causes an error linkerccnot found.

To Reproduce Steps to reproduce the behavior:

  1. Put rustup under environment.systemPackages in /etc/nixos/configuration.nix. 2.Run sudo nixos-rebuild switch
  2. Install stable Rust: rustup default stable.
  3. Compile any rust code with cargo build or rustc, for example running rustc test.rs on a file containing: fn main() {}.
  4. Get output:
    
    error: linker `cc` not found
    |
    = note: No such file or directory (os error 2)

error: aborting due to previous error


**Expected behavior**
The rust code to compile.

**Additional context**
<details>
    <summary>Full `configuration.nix`.</summary>

{ config, pkgs, ... }:

{ imports = [ # Include the results of the hardware scan. ./hardware-configuration.nix ];

boot.loader.grub.enable = true; boot.loader.grub.version = 2; boot.loader.grub.device = "/dev/sdb";

time.timeZone = "Europe/Amsterdam"; i18n.defaultLocale = "en_US.UTF-8";

Enable the GNOME 3 Desktop Environment.

services.xserver.enable = true; services.xserver.displayManager.gdm.enable = true; services.xserver.desktopManager.gnome3.enable = true;

users.users.siilwyn = { isNormalUser = true; extraGroups = [ "wheel" ]; # Enable ‘sudo’ for the user. shell = pkgs.fish; };

programs.fish.enable = true;

environment.systemPackages = with pkgs; [ git neovim firefox chromium

docker
rustup
syncthing

httpie
glow

contrast
vscodium
fira-code

# Needed for Volta
openssl

];

Enable CUPS to print documents.

services.printing.enable = true;

Enable sound.

sound.enable = true;

hardware.pulseaudio.enable = true;

The global useDHCP flag is deprecated, therefore explicitly set to false here.

Per-interface useDHCP will be mandatory in the future, so this generated config

replicates the default behaviour.

networking.useDHCP = false; networking.interfaces.enp0s25.useDHCP = true; networking.interfaces.wlp3s0.useDHCP = true;

This value determines the NixOS release from which the default

settings for stateful data, like file locations and database versions

on your system were taken. It‘s perfectly fine and recommended to leave

this value at the release version of the first install of this system.

system.stateVersion = "20.09"; }


</details>

**Notify maintainers**
@Mic92 

**Metadata**
Please run `nix-shell -p nix-info --run "nix-info -m"` and paste the result.

λ nix-shell -p nix-info --run "nix-info -m" these paths will be fetched (0.05 MiB download, 0.28 MiB unpacked): /nix/store/rlknkw60sngvky1rkrdws0qik3scjcln-bash-interactive-4.4-p23-dev copying path '/nix/store/rlknkw60sngvky1rkrdws0qik3scjcln-bash-interactive-4.4-p23-dev' from 'https://cache.nixos.org'...

Maintainer information:

# a list of nixpkgs attributes affected by the problem
attribute:
# a list of nixos modules affected by the problem
module:
Mic92 commented 3 years ago

The error you see is that it cannot find a C compiler. Our C/C++ compiler infrastructure requires a nix-shell:

with import <nixpkgs> {}:
mkShell {
  # this way you could also load rustup with the nix-shell as well... rather
  # than installing it globally.
  #buildInputs [ rustup ];
}
$ nix-shell
nix-shell> echo $CC
nix-shell> cargo build

Also it feasible to hard code a c compiler into cargo, I rather don't want to do this since it makes it hard to use a different compiler. Also almost any C library would require a nix-shell anyway in order fo the C compiler to be able to pick it up. If you need a better workflow for automatically loading a nix-shell have a look at direnv's nix-shell integration. Our wiki also contains further information on how C/C++ are integrated into nixpkgs.

Siilwyn commented 3 years ago

Ah okay, thought I would be able to define everything globally.

Thanks for the reply!

bew commented 3 years ago

For those using the experimental nix flakes system and stumbling on this issue, you can get around with:

nix shell nixpkgs#cargo nixpkgs#gcc --command cargo build

(note that here the default/global nixpkgs always points to the master branch, I'd recommend using another one with nix registry add nixpkgs github:nixos/nixpkgs/nixpkgs-unstable to follow the branch nixpkgs-unstable for which most packages are cached by hydra)

samuela commented 5 months ago

nix shell nixpkgs#cargo nixpkgs#gcc --command cargo build

This doesn't work for me for some reason:

root@e0aed42f33d7:/workspaces/sshenanigans# nix shell nixpkgs#cargo nixpkgs#gcc --command cargo build
error: path '«unknown»/nix/store/swqzdxbim9vyc8bbzibvia7gq30ih86j-gcc-wrapper-13.2.0-man' is a symlink

FWIW I'm running on Ubuntu with Nix installed via the Determinate installer, but I'm not sure if that ought to matter here?