Building rustc in NixOS

I’m looking to implement a fix to rustc, but am having trouble getting it to build on NixOS. A hacky workaround I’ve been using so far is to install some packages globally via home-manager (e.g. llvmPackages.bintools-unwrapped and glibc) so that I can have them available without needing a shell.nix or flake.nix (which the Rust repo doesn’t have).

But I’ve got stuck at this error message, when linking with cc:

   = note: /nix/store/rq6bh3qfrqnyqwik0w3q6w180zg3w2pa-binutils-2.38/bin/ld: cannot find -lutil: No such file or directory
           /nix/store/rq6bh3qfrqnyqwik0w3q6w180zg3w2pa-binutils-2.38/bin/ld: cannot find -lrt: No such file or directory
           /nix/store/rq6bh3qfrqnyqwik0w3q6w180zg3w2pa-binutils-2.38/bin/ld: cannot find -lpthread: No such file or directory
           /nix/store/rq6bh3qfrqnyqwik0w3q6w180zg3w2pa-binutils-2.38/bin/ld: cannot find -lm: No such file or directory
           /nix/store/rq6bh3qfrqnyqwik0w3q6w180zg3w2pa-binutils-2.38/bin/ld: cannot find -ldl: No such file or directory
           /nix/store/rq6bh3qfrqnyqwik0w3q6w180zg3w2pa-binutils-2.38/bin/ld: cannot find -lc: No such file or directory

I’m not super familiar with Nix, so I guess this is a two-part question:

  1. Does anyone here build rustc on NixOS and have a working config? Or understands this error/how to fix it?
  2. Is there a better way to work with codebases that don’t use Nix, but require certain system libraries to be present?

Thanks :grin:

I expect that there are not many people building rustc using nix. But I would recommend to write a shell.nix or flake.nix (whichever is more easy to you) and add it to your fork and share it when you are stuck.

Necro-bumping this thread for future reference.

Managed to build rustc and run all tests using the following flake, after some serious head scratching. Note that there may be some useless stuff in it though:

{
  description = "A flake for rust development";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs =
    {
      self,
      nixpkgs,
      flake-utils,
    }:
    flake-utils.lib.eachDefaultSystem (
      system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        libs = with pkgs.stdenv.cc; {
          ccLib = cc.lib;
          libc = libc;
          libcDev = libc.dev;
          libcStatic = libc.static;
          libgcc = cc.libgcc;
        };
      in
      with pkgs;
      {
        devShells.default = mkShell {
          name = "rust-dev-gcc";

          # Make clang aware of a few headers
          BINDGEN_EXTRA_CLANG_ARGS = ''-isystem ${libs.libcDev}/include'';

          # libc dynamic libraries
          LD_LIBRARY_PATH = lib.makeLibraryPath [
            libs.ccLib
            libs.libc
            libs.libgcc
            zlib
          ];

          # libc static libraries
          LIBRARY_PATH = lib.makeLibraryPath [ libs.libcStatic ];

          nativeBuildInputs = [
            cmake
            curl
            python3
            pkg-config
          ];

          shellHook = ''
            alias x_wrapped="setarch $(uname -m) $(pwd)/x"
            no_randomize=$(setarch --show | grep "ADDR_NO_RANDOMIZE")
            if [ -n no_randomize ];
            then
              echo 'The ADDR_NO_RANDOMIZE flag is set.'
              echo "Use the \"setarch\" program to unset this flag: \"setarch $(uname -m) ./x ...\"."
              echo "Alternatively, you can use the following alias: \"x_wrapped\"."
            fi;
          '';
        };
      }
    );
}

Comments

  1. the x.py script automatically detects if the current system is NixOS and patches binaries accordingly. This is done in two locations: src/bootstrap/bootstrap.py and src/bootstrap/src/core/download.rs, where the nix-build command is invoked.
  2. some tests require static glibc
  3. some tests require ASLR, which – for some reason – is disabled inside the devShell:
[juul_mc_goa@Meh:~/rust]$ setarch --show
PER_LINUX (ADDR_NO_RANDOMIZE)

This is the reason why an alias x_wrapped is created inside shellHook.

1 Like