Building rustc in NixOS

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