How to use naersk with stable rust tools?

TLDR

How can naersk be used with stable rust?

The gory details

I have created a toy repository by doing little (if anything) more than

  1. cargo init
  2. Copy and paste the example from the bottom of the naersk repo.
  3. Adjust the sha256

This works[*], as can be seen by trying (in a flakes-enabled Nix)

nix shell github:jacg/naersk-test-drive/nightly -c naersk-test-drive 

which prints a well-known friendly message.

Switching the rust toolchain from nightly to 1.50.0 like this

-        channel = "nightly";
-        sha256 = "sha256-hTj47PwUeP276uF6+HLDzsHYoDvfJa+y9o+vmxZqV0g=";
+        channel = "1.50.0";
+        sha256 = "sha256-PkX/nhR3RAi+c7W6bbphN3QbFcStg49hPEOYfvG51lA=";

and attempting the equivalent

nix shell github:jacg/naersk-test-drive/stable-1.50.0 -c naersk-test-drive 

results in the following failure

error: --- Error --- nix-daemon
builder for '/nix/store/3ss36ngs5250w5clnmjil3xrpslz2h2i-naersk-test-drive-deps-0.1.0.drv' failed with exit code 101; last 10 log lines:
  [naersk] cargo_build_output_json (created): /build/tmp.FLuJtRxLLj
  [naersk] crate_sources: /nix/store/dxg5jxai9kzgzq1jwxxar53ymggi2w2j-crates-io
  [naersk] RUSTFLAGS: 
  [naersk] CARGO_BUILD_RUSTFLAGS: 
  [naersk] CARGO_BUILD_RUSTFLAGS (updated): --remap-path-prefix /nix/store/dxg5jxai9kzgzq1jwxxar53ymggi2w2j-crates-io=/sources
  building
  cargo -Z unstable-options build $cargo_release -j "$NIX_BUILD_CORES" --out-dir out --message-format=$cargo_message_format
  error: the `-Z` flag is only accepted on the nightly channel of Cargo, but this is the `stable` channel
  See https://doc.rust-lang.org/book/appendix-07-nightly-rust.html for more information about Rust release channels.
  [naersk] cargo returned with exit code 101, exiting
error: --- Error ------------------------------------------------------------------------------------------------------------------- nix
1 dependencies of derivation '/nix/store/9f2zzdrg1hlwxidic5q2csvdsrdna6jm-naersk-test-drive-0.1.0.drv' failed to build

On the one hand, the error contains

  cargo -Z unstable-options build $cargo_release -j "$NIX_BUILD_CORES" --out-dir out --message-format=$cargo_message_format
  error: the `-Z` flag is only accepted on the nightly channel of Cargo, but this is the `stable` channel

on the other, the naersk documentation states that

Note: naersk relies on the --out-dir out option

which is an unstable option. This seems to imply that naersk can only be used with unstable channels, however @jtojnar suggests here that naersk should be usable with stable rust.

How can this example be adapted to work with stable rust?

Excruciatingly slow rust installation time

[*] On my machine, Nix takes around 17 minutes to install a version of the rust toolchain that I haven’t used before. Most of which seems to be spent on copying an almost infinite number of Rust HTML doc files into the Nix store. This doesn’t feel right, and I don’t recall it being this slow before I started experimenting with flakes.

The cargo derivation passed to naersk doesn’t have to be the same as the rustc one, you can compile stable Rust with a stable rustc and an unstable cargo

EDIT: regarding long installation times, I think I’ve experienced something similar, maybe the amount of HTML docs blew up in recent releases. Filtering out the Rust documentation from the “toolchain derivation” seems like a sensible solution. From what I can tell, nixpkgs-mozilla doesn’t let you select which components should be included in the derivation that aggregates all tools (it does let you pick extensions), that might be worth creating a feature request for, possibly following the profiles schema

1 Like

It works. Thank you.

Only, with two distinct rust versions in the inputs …

… when I say to a friend ‘Hey, try this cool thing: nix run github:jacg/my-cool-thing’, it comes with the caveat that there’s a 35 minute wait before anything interesting happens.

Indeed. In my copious spare time :frowning: