Skip to content

Commit 9c202ac

Browse files
authored
Unrolled build for rust-lang#129620
Rollup merge of rust-lang#129620 - WaffleLapkin:flake, r=Noratrieb Provide a more convinient way of developing rustc on NixOS This PR adds envrc files which, once symlinked as `.envrc` will activates a dev shell from `src/tools/nix-dev-shell/flake.nix` or `src/tools/nix-dev-shell/shell.nix`. This is based on - [Current rustc dev guide recommendation for NixOS](https://rustc-dev-guide.rust-lang.org/building/suggested.html?highlight=nix#using-nix-shell) - https://github.com/oxalica/rust-overlay?tab=readme-ov-file#use-in-devshell-for-nix-develop - [Nora's `x` nix package](https://github.com/Noratrieb/nixos/tree/26ea68e1a0aadaab313c1b5a8c1033a9770bd138/custom-pkgs/x) - rust-lang/rustup#2891 - [Direnv: use flake/nix according to availability](https://discourse.nixos.org/t/direnv-use-flake-nix-according-to-availability/29825) This is something that I plan to use personally, but I thought it might be worth upstreaming :) r? Noratrieb
2 parents 86bd459 + 002a6b1 commit 9c202ac

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ build/
5757
/src/tools/x/target
5858
# Created by default with `src/ci/docker/run.sh`
5959
/obj/
60+
# Created by nix dev shell / .envrc
61+
src/tools/nix-dev-shell/flake.lock
6062

6163
## ICE reports
6264
rustc-ice-*.txt

src/tools/nix-dev-shell/envrc-flake

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# If you want to use this as an .envrc file to create a shell with necessery components
2+
# to develop rustc, use the following command in the root of the rusr checkout:
3+
#
4+
# ln -s ./src/tools/nix-dev-shell/envrc-flake ./.envrc && echo .envrc >> .git/info/exclude
5+
6+
if nix flake show path:./src/tools/nix-dev-shell &> /dev/null; then
7+
use flake path:./src/tools/nix-dev-shell
8+
fi

src/tools/nix-dev-shell/envrc-shell

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# If you want to use this as an .envrc file to create a shell with necessery components
2+
# to develop rustc, use the following command in the root of the rusr checkout:
3+
#
4+
# ln -s ./src/tools/nix-dev-shell/envrc-shell ./.envrc && echo .envrc >> .git/info/exclude
5+
6+
use nix ./src/tools/nix-dev-shell/shell.nix
7+

src/tools/nix-dev-shell/flake.nix

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
description = "rustc dev shell";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
};
8+
9+
outputs = { self, nixpkgs, flake-utils, ... }:
10+
flake-utils.lib.eachDefaultSystem (system:
11+
let
12+
pkgs = import nixpkgs { inherit system; };
13+
x = import ./x { inherit pkgs; };
14+
in
15+
{
16+
devShells.default = with pkgs; mkShell {
17+
name = "rustc-dev-shell";
18+
nativeBuildInputs = with pkgs; [
19+
binutils cmake ninja pkg-config python3 git curl cacert patchelf nix
20+
];
21+
buildInputs = with pkgs; [
22+
openssl glibc.out glibc.static x
23+
];
24+
# Avoid creating text files for ICEs.
25+
RUSTC_ICE = "0";
26+
# Provide `libstdc++.so.6` for the self-contained lld.
27+
LD_LIBRARY_PATH = "${with pkgs; lib.makeLibraryPath [
28+
stdenv.cc.cc.lib
29+
]}";
30+
};
31+
}
32+
);
33+
}

src/tools/nix-dev-shell/shell.nix

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{ pkgs ? import <nixpkgs> {} }:
2+
let
3+
x = import ./x { inherit pkgs; };
4+
in
5+
pkgs.mkShell {
6+
name = "rustc";
7+
nativeBuildInputs = with pkgs; [
8+
binutils cmake ninja pkg-config python3 git curl cacert patchelf nix
9+
];
10+
buildInputs = with pkgs; [
11+
openssl glibc.out glibc.static x
12+
];
13+
# Avoid creating text files for ICEs.
14+
RUSTC_ICE = "0";
15+
# Provide `libstdc++.so.6` for the self-contained lld.
16+
LD_LIBRARY_PATH = "${with pkgs; lib.makeLibraryPath [
17+
stdenv.cc.cc.lib
18+
]}";
19+
}

src/tools/nix-dev-shell/x/default.nix

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
pkgs ? import <nixpkgs> { },
3+
}:
4+
pkgs.stdenv.mkDerivation {
5+
name = "x";
6+
7+
src = ./x.rs;
8+
dontUnpack = true;
9+
10+
nativeBuildInputs = with pkgs; [ rustc ];
11+
12+
buildPhase = ''
13+
PYTHON=${pkgs.lib.getExe pkgs.python3} rustc -Copt-level=3 --crate-name x $src --out-dir $out/bin
14+
'';
15+
16+
meta = with pkgs.lib; {
17+
description = "Helper for rust-lang/rust x.py";
18+
homepage = "https://github.com/rust-lang/rust/blob/master/src/tools/x";
19+
license = licenses.mit;
20+
mainProgram = "x";
21+
};
22+
}

src/tools/nix-dev-shell/x/x.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// git clone https://github.com/rust-lang/rust/blob/0ea7ddcc35a2fcaa5da8a7dcfc118c9fb4a63b95/src/tools/x/src/main.rs
2+
// patched to stop doing python probing, stop the probe, please dont, i have a python
3+
//! Run bootstrap from any subdirectory of a rust compiler checkout.
4+
//!
5+
//! We prefer `exec`, to avoid adding an extra process in the process tree.
6+
//! However, since `exec` isn't available on Windows, we indirect through
7+
//! `exec_or_status`, which will call `exec` on unix and `status` on Windows.
8+
//!
9+
//! We use `powershell.exe x.ps1` on Windows, and `sh -c x` on Unix, those are
10+
//! the ones that call `x.py`. We use `sh -c` on Unix, because it is a standard.
11+
//! We also don't use `pwsh` on Windows, because it is not installed by default;
12+
13+
use std::env;
14+
use std::os::unix::process::CommandExt;
15+
use std::process::{self, Command};
16+
17+
fn main() {
18+
match env::args().skip(1).next().as_deref() {
19+
Some("--wrapper-version") => {
20+
println!("0.1.0");
21+
return;
22+
}
23+
_ => {}
24+
}
25+
let current = match env::current_dir() {
26+
Ok(dir) => dir,
27+
Err(err) => {
28+
eprintln!("Failed to get current directory: {err}");
29+
process::exit(1);
30+
}
31+
};
32+
33+
for dir in current.ancestors() {
34+
let candidate = dir.join("x.py");
35+
if candidate.exists() {
36+
let mut cmd = Command::new(env!("PYTHON"));
37+
cmd.arg(dir.join("x.py"));
38+
cmd.args(env::args().skip(1)).current_dir(dir);
39+
40+
let error = cmd.exec();
41+
eprintln!("Failed to invoke `{:?}`: {}", cmd, error);
42+
}
43+
}
44+
45+
eprintln!(
46+
"x.py not found. Please run inside of a checkout of `https://github.com/rust-lang/rust`."
47+
);
48+
49+
process::exit(1);
50+
}

0 commit comments

Comments
 (0)