about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-10-17 20:47:29 +0200
committerGitHub <noreply@github.com>2024-10-17 20:47:29 +0200
commit7a7c31331210b28b2420350753a0b99fc835f73a (patch)
treeffd5b3f5c422428309bea89367e55d6c4709bab6
parent86bd45979a964678b40b79156744f0057759d840 (diff)
parent002a6b134f910be617722f9d25e0ea8016c2a54e (diff)
downloadrust-7a7c31331210b28b2420350753a0b99fc835f73a.tar.gz
rust-7a7c31331210b28b2420350753a0b99fc835f73a.zip
Rollup merge of #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)
- https://github.com/rust-lang/rustup/pull/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
-rw-r--r--.gitignore2
-rw-r--r--src/tools/nix-dev-shell/envrc-flake8
-rw-r--r--src/tools/nix-dev-shell/envrc-shell7
-rw-r--r--src/tools/nix-dev-shell/flake.nix33
-rw-r--r--src/tools/nix-dev-shell/shell.nix19
-rw-r--r--src/tools/nix-dev-shell/x/default.nix22
-rw-r--r--src/tools/nix-dev-shell/x/x.rs50
7 files changed, 141 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index b170dca88fa..948133cd76e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -57,6 +57,8 @@ build/
 /src/tools/x/target
 # Created by default with `src/ci/docker/run.sh`
 /obj/
+# Created by nix dev shell / .envrc
+src/tools/nix-dev-shell/flake.lock
 
 ## ICE reports
 rustc-ice-*.txt
diff --git a/src/tools/nix-dev-shell/envrc-flake b/src/tools/nix-dev-shell/envrc-flake
new file mode 100644
index 00000000000..218d88d8721
--- /dev/null
+++ b/src/tools/nix-dev-shell/envrc-flake
@@ -0,0 +1,8 @@
+# If you want to use this as an .envrc file to create a shell with necessery components 
+# to develop rustc, use the following command in the root of the rusr checkout:
+#	
+# ln -s ./src/tools/nix-dev-shell/envrc-flake ./.envrc && echo .envrc >> .git/info/exclude
+
+if nix flake show path:./src/tools/nix-dev-shell &> /dev/null; then
+  use flake path:./src/tools/nix-dev-shell
+fi
diff --git a/src/tools/nix-dev-shell/envrc-shell b/src/tools/nix-dev-shell/envrc-shell
new file mode 100644
index 00000000000..fb7231a6c30
--- /dev/null
+++ b/src/tools/nix-dev-shell/envrc-shell
@@ -0,0 +1,7 @@
+# If you want to use this as an .envrc file to create a shell with necessery components 
+# to develop rustc, use the following command in the root of the rusr checkout:
+#	
+# ln -s ./src/tools/nix-dev-shell/envrc-shell ./.envrc && echo .envrc >> .git/info/exclude
+
+use nix ./src/tools/nix-dev-shell/shell.nix
+  
diff --git a/src/tools/nix-dev-shell/flake.nix b/src/tools/nix-dev-shell/flake.nix
new file mode 100644
index 00000000000..8ab5e097427
--- /dev/null
+++ b/src/tools/nix-dev-shell/flake.nix
@@ -0,0 +1,33 @@
+{
+  description = "rustc dev shell";
+
+  inputs = {
+    nixpkgs.url      = "github:NixOS/nixpkgs/nixos-unstable";
+    flake-utils.url  = "github:numtide/flake-utils";
+  };
+
+  outputs = { self, nixpkgs, flake-utils, ... }:
+	flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = import nixpkgs { inherit system; };
+        x = import ./x { inherit pkgs; };
+      in
+      {
+        devShells.default = with pkgs; mkShell {
+          name = "rustc-dev-shell";
+          nativeBuildInputs = with pkgs; [
+            binutils cmake ninja pkg-config python3 git curl cacert patchelf nix
+          ];
+          buildInputs = with pkgs; [
+            openssl glibc.out glibc.static x
+          ];
+          # Avoid creating text files for ICEs.
+          RUSTC_ICE = "0";
+          # Provide `libstdc++.so.6` for the self-contained lld.
+          LD_LIBRARY_PATH = "${with pkgs; lib.makeLibraryPath [
+            stdenv.cc.cc.lib
+          ]}";
+        };
+      }
+    );
+}
diff --git a/src/tools/nix-dev-shell/shell.nix b/src/tools/nix-dev-shell/shell.nix
new file mode 100644
index 00000000000..8a5cbb7c89e
--- /dev/null
+++ b/src/tools/nix-dev-shell/shell.nix
@@ -0,0 +1,19 @@
+{ pkgs ? import <nixpkgs> {} }:
+let 
+  x = import ./x { inherit pkgs; };
+in
+pkgs.mkShell {
+  name = "rustc";
+  nativeBuildInputs = with pkgs; [
+    binutils cmake ninja pkg-config python3 git curl cacert patchelf nix
+  ];
+  buildInputs = with pkgs; [
+    openssl glibc.out glibc.static x
+  ];
+  # Avoid creating text files for ICEs.
+  RUSTC_ICE = "0";
+  # Provide `libstdc++.so.6` for the self-contained lld.
+  LD_LIBRARY_PATH = "${with pkgs; lib.makeLibraryPath [
+    stdenv.cc.cc.lib
+  ]}";
+}
diff --git a/src/tools/nix-dev-shell/x/default.nix b/src/tools/nix-dev-shell/x/default.nix
new file mode 100644
index 00000000000..e6dfbad6f19
--- /dev/null
+++ b/src/tools/nix-dev-shell/x/default.nix
@@ -0,0 +1,22 @@
+{
+  pkgs ? import <nixpkgs> { },
+}:
+pkgs.stdenv.mkDerivation {
+  name = "x";
+
+  src = ./x.rs;
+  dontUnpack = true;
+
+  nativeBuildInputs = with pkgs; [ rustc ];
+
+  buildPhase = ''
+    PYTHON=${pkgs.lib.getExe pkgs.python3} rustc -Copt-level=3 --crate-name x $src --out-dir $out/bin
+  '';
+
+  meta = with pkgs.lib; {
+    description = "Helper for rust-lang/rust x.py";
+    homepage = "https://github.com/rust-lang/rust/blob/master/src/tools/x";
+    license = licenses.mit;
+    mainProgram = "x";
+  };
+}
diff --git a/src/tools/nix-dev-shell/x/x.rs b/src/tools/nix-dev-shell/x/x.rs
new file mode 100644
index 00000000000..9f83b8fd62e
--- /dev/null
+++ b/src/tools/nix-dev-shell/x/x.rs
@@ -0,0 +1,50 @@
+// git clone https://github.com/rust-lang/rust/blob/0ea7ddcc35a2fcaa5da8a7dcfc118c9fb4a63b95/src/tools/x/src/main.rs
+// patched to stop doing python probing, stop the probe, please dont, i have a python
+//! Run bootstrap from any subdirectory of a rust compiler checkout.
+//!
+//! We prefer `exec`, to avoid adding an extra process in the process tree.
+//! However, since `exec` isn't available on Windows, we indirect through
+//! `exec_or_status`, which will call `exec` on unix and `status` on Windows.
+//!
+//! We use `powershell.exe x.ps1` on Windows, and `sh -c x` on Unix, those are
+//! the ones that call `x.py`. We use `sh -c` on Unix, because it is a standard.
+//! We also don't use `pwsh` on Windows, because it is not installed by default;
+
+use std::env;
+use std::os::unix::process::CommandExt;
+use std::process::{self, Command};
+
+fn main() {
+    match env::args().skip(1).next().as_deref() {
+        Some("--wrapper-version") => {
+            println!("0.1.0");
+            return;
+        }
+        _ => {}
+    }
+    let current = match env::current_dir() {
+        Ok(dir) => dir,
+        Err(err) => {
+            eprintln!("Failed to get current directory: {err}");
+            process::exit(1);
+        }
+    };
+
+    for dir in current.ancestors() {
+        let candidate = dir.join("x.py");
+        if candidate.exists() {
+            let mut cmd = Command::new(env!("PYTHON"));
+            cmd.arg(dir.join("x.py"));
+            cmd.args(env::args().skip(1)).current_dir(dir);
+
+            let error = cmd.exec();
+            eprintln!("Failed to invoke `{:?}`: {}", cmd, error);
+        }
+    }
+
+    eprintln!(
+        "x.py not found. Please run inside of a checkout of `https://github.com/rust-lang/rust`."
+    );
+
+    process::exit(1);
+}