1010 rust-bin ,
1111 udev ,
1212 crane ,
13- runCommand ,
13+ writeShellScriptBin ,
1414 version ? "0.31.1" ,
1515} :
1616let
1717 pname = "anchor-cli" ;
1818
19+ # Anchor IDL generation makes use of rust-nightly
20+ # therefore we want to expose it to the environment to be used
21+ # Due to https://github.com/solana-foundation/anchor/pull/3663
22+ # latest nightly is always preferred when possible, since breakage may occur
23+ # due to differing dependency versions as well
1924 versionsDeps = {
2025 "0.31.1" = {
2126 hash = "sha256-c+UybdZCFL40TNvxn0PHR1ch7VPhhJFDSIScetRpS3o=" ;
22- # Unfortunately dependency on nightly compiler seems to be common
23- # in rust projects
24- rust-nightly = rust-bin . nightly . "2025-04-21" . minimal ;
2527 rust = rust-bin . stable . "1.85.0" . default ;
28+ rust-nightly = rust-bin . nightly . latest . default ;
2629 platform-tools = solana-platform-tools . override { version = "1.45" ; } ;
2730 patches = [ ./patches/anchor-cli/0.31.1.patch ] ;
2831 } ;
2932 "0.31.0" = {
3033 hash = "sha256-CaBVdp7RPVmzzEiVazjpDLJxEkIgy1BHCwdH2mYLbGM=" ;
3134 rust = rust-bin . stable . "1.85.0" . default ;
35+ rust-nightly = rust-bin . nightly . latest . default ;
3236 platform-tools = solana-platform-tools . override { version = "1.45" ; } ;
3337 patches = [ ./patches/anchor-cli/0.31.0.patch ] ;
3438 } ;
3539 "0.30.1" = {
3640 hash = "sha256-3fLYTJDVCJdi6o0Zd+hb9jcPDKm4M4NzpZ8EUVW/GVw=" ;
3741 rust = rust-bin . stable . "1.78.0" . default ;
42+ # anchor-syn v0.30.1 still uses the old API
43+ rust-nightly = rust-bin . nightly . "2025-04-15" . default ;
3844 platform-tools = solana-platform-tools . override { version = "1.43" ; } ;
3945 patches = [ ./patches/anchor-cli/0.30.1.patch ] ;
4046 } ;
8995 } ;
9096
9197 cargoArtifacts = craneLib . buildDepsOnly commonArgs ;
98+
99+ # We create a small cargo shim to dispatch to a compatible nightly version when necessary
100+ cargoShim = writeShellScriptBin "cargo" ''
101+ # Check that required env variables are set
102+ if [[ -z "$_NIX_SUPPORT_STABLE_TOOLCHAIN" || -z "$_NIX_SUPPORT_NIGHTLY_TOOLCHAIN" ]]; then
103+ echo "Error: Both _NIX_SUPPORT_STABLE_TOOLCHAIN and _NIX_SUPPORT_NIGHTLY_TOOLCHAIN environment variables must be set." >&2
104+ exit 1
105+ fi
106+
107+ if [[ "$1" == "+nightly" ]]; then
108+ # Shift off +nightly and pass through all remaining args
109+ shift
110+ export PATH="$_NIX_SUPPORT_NIGHTLY_TOOLCHAIN":$PATH
111+ exec cargo "$@"
112+ else
113+ export PATH="$_NIX_SUPPORT_STABLE_TOOLCHAIN":$PATH
114+ exec cargo "$@"
115+ fi
116+ '' ;
92117in
93118craneLib . buildPackage (
94119 commonArgs
95120 // {
96121 inherit cargoArtifacts ;
97122
98- # Ensure anchor has access to Solana's cargo and rust binaries
99- postInstall = let
100- # Due to th way anchor is calling cargo if its not wrapped
101- # with its own toolchain, it'll access solana rust compiler instead
102- # hence the nightly entry points must be wrapped with the nightly bins
103- # to guarantee correct usage
104- # In this case we've limited the nightly bin access to `cargo`
105- cargo-nightly = runCommand "cargo-nightly" {
106- nativeBuildInputs = [ makeWrapper ] ;
107- } ''
108- mkdir -p $out/bin
109-
110- ln -s ${ versionDeps . rust-nightly } /bin/cargo $out/bin/cargo
111-
112- # Wrap cargo nightly so it uses the nightly toolchain only
113- wrapProgram $out/bin/cargo \
114- --prefix PATH : "${ versionDeps . rust-nightly } /bin"
115- ''
116- ;
117- in
118- ''
123+ # Ensure anchor has access to Solana's rust binaries and our cargo shim with nightly
124+ postInstall = ''
119125 rust=${ versionDeps . platform-tools } /bin/platform-tools-sdk/sbf/dependencies/platform-tools/rust/bin
120126 wrapProgram $out/bin/anchor \
121- --prefix PATH : "$rust" ${ if versionDeps ? rust-nightly then "--set RUST_NIGHTLY_BIN \" ${ cargo-nightly } /bin\" " else "" }
127+ --prefix PATH : "${ cargoShim } /bin" \
128+ --set _NIX_SUPPORT_STABLE_TOOLCHAIN "$rust" \
129+ --set _NIX_SUPPORT_NIGHTLY_TOOLCHAIN "${ versionDeps . rust-nightly } /bin"
122130 '' ;
123131
124132 cargoExtraArgs = "-p ${ pname } " ;
@@ -130,6 +138,7 @@ craneLib.buildPackage (
130138
131139 passthru = {
132140 otherVersions = builtins . attrNames versionsDeps ;
141+ rustNightly = versionDeps . rust-nightly . _version ;
133142 } ;
134143 }
135144)
0 commit comments