From ddc0aa16920c9504d661bed22cb8f6c662214223 Mon Sep 17 00:00:00 2001 From: Noratrieb <48135649+Noratrieb@users.noreply.github.com> Date: Thu, 16 Nov 2023 19:58:01 +0100 Subject: [PATCH] mommy x --- custom-pkgs/cargo-mommy.nix | 25 ++++++ custom-pkgs/default.nix | 2 + custom-pkgs/x/Cargo.lock | 7 ++ custom-pkgs/x/Cargo.toml | 8 ++ custom-pkgs/x/README.md | 3 + custom-pkgs/x/default.nix | 15 ++++ custom-pkgs/x/src/main.rs | 142 +++++++++++++++++++++++++++++++ home-manager/common-packages.nix | 7 +- home-manager/desktop.nix | 5 ++ 9 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 custom-pkgs/cargo-mommy.nix create mode 100644 custom-pkgs/x/Cargo.lock create mode 100644 custom-pkgs/x/Cargo.toml create mode 100644 custom-pkgs/x/README.md create mode 100644 custom-pkgs/x/default.nix create mode 100644 custom-pkgs/x/src/main.rs diff --git a/custom-pkgs/cargo-mommy.nix b/custom-pkgs/cargo-mommy.nix new file mode 100644 index 0000000..d69ce17 --- /dev/null +++ b/custom-pkgs/cargo-mommy.nix @@ -0,0 +1,25 @@ +# copied from https://github.com/NixOS/nixpkgs/blob/d4b5a67bbe9ef750bd2fdffd4cad400dd5553af8/pkgs/development/tools/rust/cargo-mommy/default.nix#L15 +{ lib, rustPlatform, fetchFromGitHub, ... }: + +rustPlatform.buildRustPackage { + pname = "cargo-mommy"; + version = "0.1.1"; + + src = fetchFromGitHub { + owner = "Gankra"; + repo = "cargo-mommy"; + rev = "6feb98f10ede68c82d99f70aa79cb3a53530dc88"; + hash = "sha256-DuPDF594KgItrDzjFxP2xHNuzziZCmq5bCrhCh71Y1U="; + + }; + + cargoSha256 = "sha256-YkKHlLv6w3PHjv9Z94QUUO41v0W1FJ7zAUoTsKfaQG0="; + + meta = with lib; { + description = "Cargo wrapper that encourages you after running commands"; + homepage = "https://github.com/Gankra/cargo-mommy"; + license = with licenses; [ mit asl20 ]; + maintainers = with maintainers; [ GoldsteinE ]; + mainProgram = "cargo-mommy"; + }; +} diff --git a/custom-pkgs/default.nix b/custom-pkgs/default.nix index 40280c8..526aae5 100644 --- a/custom-pkgs/default.nix +++ b/custom-pkgs/default.nix @@ -1,4 +1,6 @@ pkgs: { cargo-bisect-rustc = import ./cargo-bisect-rustc/default.nix pkgs; + cargo-mommy = import ./cargo-mommy.nix pkgs; monaspace = import ./monaspace.nix pkgs; + x = import ./x pkgs; } diff --git a/custom-pkgs/x/Cargo.lock b/custom-pkgs/x/Cargo.lock new file mode 100644 index 0000000..678fbac --- /dev/null +++ b/custom-pkgs/x/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "x" +version = "0.1.0" diff --git a/custom-pkgs/x/Cargo.toml b/custom-pkgs/x/Cargo.toml new file mode 100644 index 0000000..fb9d73f --- /dev/null +++ b/custom-pkgs/x/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "x" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/custom-pkgs/x/README.md b/custom-pkgs/x/README.md new file mode 100644 index 0000000..05b956b --- /dev/null +++ b/custom-pkgs/x/README.md @@ -0,0 +1,3 @@ +vendored x from https://github.com/rust-lang/rust/blob/master/src/tools/x to avoid the big clone + +commit 0ea7ddcc35a2fcaa5da8a7dcfc118c9fb4a63b95 diff --git a/custom-pkgs/x/default.nix b/custom-pkgs/x/default.nix new file mode 100644 index 0000000..bb4a054 --- /dev/null +++ b/custom-pkgs/x/default.nix @@ -0,0 +1,15 @@ +pkgs: pkgs.rustPlatform.buildRustPackage { + pname = "x"; + version = "1.0.1"; + + src = ./.; + + cargoLock.lockFile = ./Cargo.lock; + + 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/custom-pkgs/x/src/main.rs b/custom-pkgs/x/src/main.rs new file mode 100644 index 0000000..f2099c1 --- /dev/null +++ b/custom-pkgs/x/src/main.rs @@ -0,0 +1,142 @@ +// git clone https://github.com/rust-lang/rust/blob/0ea7ddcc35a2fcaa5da8a7dcfc118c9fb4a63b95/src/tools/x/src/main.rs +//! 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::{self, consts::EXE_EXTENSION}, + io, + path::Path, + process::{self, Command, ExitStatus}, +}; + +const PYTHON: &str = "python"; +const PYTHON2: &str = "python2"; +const PYTHON3: &str = "python3"; + +fn python() -> &'static str { + let val = match env::var_os("PATH") { + Some(val) => val, + None => return PYTHON, + }; + + let mut python2 = false; + let mut python3 = false; + + for dir in env::split_paths(&val) { + // `python` should always take precedence over python2 / python3 if it exists + if dir.join(PYTHON).with_extension(EXE_EXTENSION).exists() { + return PYTHON; + } + + python2 |= dir.join(PYTHON2).with_extension(EXE_EXTENSION).exists(); + python3 |= dir.join(PYTHON3).with_extension(EXE_EXTENSION).exists(); + } + + // try 3 before 2 + if python3 { + PYTHON3 + } else if python2 { + PYTHON2 + } else { + // Python was not found on path, so exit + eprintln!("Unable to find python in your PATH. Please check it is installed."); + process::exit(1); + } +} + +#[cfg(windows)] +fn x_command(dir: &Path) -> Command { + let mut cmd = Command::new("powershell.exe"); + cmd.args([ + "-NoLogo", + "-NoProfile", + "-NonInteractive", + "-ExecutionPolicy", + "RemoteSigned", + "-Command", + "./x.ps1", + ]) + .current_dir(dir); + cmd +} + +#[cfg(unix)] +fn x_command(dir: &Path) -> Command { + Command::new(dir.join("x")) +} + +#[cfg(not(any(windows, unix)))] +fn x_command(_dir: &Path) -> Command { + compile_error!("Unsupported platform"); +} + +#[cfg(unix)] +fn exec_or_status(command: &mut Command) -> io::Result { + use std::os::unix::process::CommandExt; + Err(command.exec()) +} + +#[cfg(not(unix))] +fn exec_or_status(command: &mut Command) -> io::Result { + command.status() +} + +fn handle_result(result: io::Result, cmd: Command) { + match result { + Err(error) => { + eprintln!("Failed to invoke `{:?}`: {}", cmd, error); + } + Ok(status) => { + process::exit(status.code().unwrap_or(1)); + } + } +} + +fn main() { + match env::args().skip(1).next().as_deref() { + Some("--wrapper-version") => { + let version = env!("CARGO_PKG_VERSION"); + println!("{}", version); + 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 shell_script_candidate = dir.join("x"); + let mut cmd: Command; + if shell_script_candidate.exists() { + cmd = x_command(dir); + cmd.args(env::args().skip(1)).current_dir(dir); + } else { + // For older checkouts that do not have the x shell script, default to python + cmd = Command::new(python()); + cmd.arg(&candidate).args(env::args().skip(1)).current_dir(dir); + } + let result = exec_or_status(&mut cmd); + handle_result(result, cmd); + } + } + + eprintln!( + "x.py not found. Please run inside of a checkout of `https://github.com/rust-lang/rust`." + ); + + process::exit(1); +} \ No newline at end of file diff --git a/home-manager/common-packages.nix b/home-manager/common-packages.nix index 93616aa..b9188f6 100644 --- a/home-manager/common-packages.nix +++ b/home-manager/common-packages.nix @@ -1,8 +1,13 @@ -{ pkgs, ... }: with pkgs; [ +{ pkgs, ... }: +let + customPkgs = import ../custom-pkgs/default.nix pkgs; +in +with pkgs; [ bacon bat cargo-expand cargo-nextest + customPkgs.cargo-mommy fzf gcc gdb diff --git a/home-manager/desktop.nix b/home-manager/desktop.nix index 64eaed3..3d6823f 100644 --- a/home-manager/desktop.nix +++ b/home-manager/desktop.nix @@ -31,6 +31,7 @@ in home.packages = with pkgs; [ audacity + customPkgs.cargo-mommy customPkgs.cargo-bisect-rustc discord jetbrains.idea-ultimate @@ -50,6 +51,10 @@ in ''; shellAbbrs = { flamegraph = "perf script | inferno-collapse-perf | inferno-flamegraph > out.svg && firefox out.svg"; + + }; + shellAliases = { + x = "CARGO=${lib.getExe customPkgs.x} ${lib.getExe customPkgs.cargo-mommy}"; }; };