Skip to content

Add a Nix configuration for the cargo rust-gpu environment #3027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions .nix/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,52 @@
inherit system overlays;
};

rustc-wasm = pkgs.rust-bin.stable.latest.default.override {
rustExtensions = [ "rust-src" "rust-analyzer" "clippy" "cargo" ];
rust = pkgs.rust-bin.stable.latest.default.override {
targets = [ "wasm32-unknown-unknown" ];
extensions = [ "rust-src" "rust-analyzer" "clippy" "cargo" ];
extensions = rustExtensions;
};

rustGPUToolchainPkg = pkgs.rust-bin.nightly."2025-06-23".default.override {
extensions = rustExtensions ++ [ "rustc-dev" "llvm-tools" ];
};
rustGPUToolchainRustPlatform = pkgs.makeRustPlatform {
cargo = rustGPUToolchainPkg;
rustc = rustGPUToolchainPkg;
};
rustc_codegen_spirv = rustGPUToolchainRustPlatform.buildRustPackage (finalAttrs: {
pname = "rustc_codegen_spirv";
version = "0-unstable-2025-08-04";
src = pkgs.fetchFromGitHub {
owner = "Rust-GPU";
repo = "rust-gpu";
rev = "df1628a032d22c864397417c2871b74d602af986";
hash = "sha256-AFt3Nc+NqK8DxNUhDBcOUmk3XDVcoToVeFIMYNszdbY=";
};
cargoHash = "sha256-en3BYJWQabH064xeAwYQrvcr6EuWg/QjvsG+Jd6HHCk";
cargoBuildFlags = [ "-p" "rustc_codegen_spirv" "--features=use-installed-tools" "--no-default-features" ];
doCheck = false;
});

# Wrapper script for running rust commands with the rust toolchain used by rust-gpu.
# For example `rust-gpu cargo --version` or `rust-gpu rustc --version`.
execWithRustGPUEnvironment = pkgs.writeShellScriptBin "rust-gpu" ''
#!${pkgs.lib.getExe pkgs.bash}

filtered_args=()
for arg in "$@"; do
case "$arg" in
+nightly|+nightly-*) ;;
*) filtered_args+=("$arg") ;;
esac
done

export PATH="${pkgs.lib.makeBinPath [ rustGPUToolchainPkg pkgs.spirv-tools ]}:$PATH"
export RUSTC_CODEGEN_SPIRV_PATH="${rustc_codegen_spirv}/lib/librustc_codegen_spirv.so"

exec ${"\${filtered_args[@]}"}
'';
Comment on lines +66 to +81
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's so much easier to use RUSTUP_TOOLCHAIN for this, I was just asking @Firestar99 why the + syntax when the env var also works.

All you need in the script, if you want to have dynamic dispatch, is:

''
  #!${pkgs.lib.getExe pkgs.bash}
  exec "${toolchains}/${"\$"}{RUSTUP_TOOLCHAIN:-stable}/bin/$(basename "$0")" "$@"
''

(I don't like that ${"\$"} but am not sure I can make it much nicer)

I suppose something different than what you are doing today is you would want to define toolchains as one of those sym trees with one directory per supported toolchain - maybe the oxalica overlay rust-bin has something along those lines? (it's been a while since I looked at it anyway)


Although, if cargo-gpu/spirv-builder switched to setting RUSTUP_TOOLCHAIN, the fact that this script only has to filter out the + syntax, makes me think the script can just go away? (since I guess there is no dynamic dispatch right now, just a pesky first argument)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want devs mistaking the pinned rust-gpu rust nightly toolchain for a normal nightly toolchain. We want a way to ensure that it is only ever usable to compile rust-gpu code. A wrapper script is the best way to ensure that in my opinion.

But getting rid of the filter logic would be great. Thanks for the comment.

Copy link
Collaborator

@Firestar99 Firestar99 Aug 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want to note that on master, we kill the RUSTUP_TOOLCHAIN env var. I'm unsure if my branch has these changes already or not, so we could see behaviour differences between now and me rebasing the branch.

https://github.com/Rust-GPU/rust-gpu/blob/e0dfdd1112d9b2cf27d10e25104dc997db9acdf0/crates/spirv-builder/src/cargo_cmd.rs#L52

Yes we do need a better solution for this, but since this has to work within the next week, it'll have to wait.


libcef = pkgs.libcef.overrideAttrs (finalAttrs: previousAttrs: {
version = "139.0.17";
gitRevision = "6c347eb";
Expand All @@ -51,7 +92,6 @@
strip $out/lib/*
'';
});

libcefPath = pkgs.runCommand "libcef-path" {} ''
mkdir -p $out

Expand Down Expand Up @@ -85,7 +125,7 @@

# Development tools that don't need to be in LD_LIBRARY_PATH
buildTools = [
rustc-wasm
rust
pkgs.nodejs
pkgs.nodePackages.npm
pkgs.binaryen
Expand All @@ -97,6 +137,8 @@

# Linker
pkgs.mold

execWithRustGPUEnvironment
];
# Development tools that don't need to be in LD_LIBRARY_PATH
devTools = with pkgs; [
Expand Down
Loading