Building wasm-pack-example (rust) crate fails?

This is possibly related to this question.

I’ll add a short summary of what I did here to hopefully make my perspective clearer:

nix-shell -p wasm-pack cargo rustc -I nixpkgs=channel:nixos-unstable
cargo generate-lockfile
crate2nix generate -n '<nixos-unstable>'
nix-build -I nixpkgs=channel:nixos-unstable
wasm-pack build --target web

[INFO]: Checking for the Wasm target…
Error: wasm32-unknown-unknown target not found in sysroot: “/nix/store/lm64cjp5c5kc772rpmf88q22s8lr2zhi-rustc-1.40.0”

Used rustc from the following path: “/nix/store/lm64cjp5c5kc772rpmf88q22s8lr2zhi-rustc-1.40.0/bin/rustc”
It looks like Rustup is not being used. For non-Rustup setups, the wasm32-unknown-unknown target needs to be installed manually. See Non-rustup setups - Hello wasm-pack! on how to do this.

Did exactly that:

$(which rustc) --print sysroot

/nix/store/lm64cjp5c5kc772rpmf88q22s8lr2zhi-rustc-1.40.0

ls -lr /nix/store/lm64cjp5c5kc772rpmf88q22s8lr2zhi-rustc-1.40.0/lib/rustlib/wasm32-unknown-unknown/

drwxr-xr-x 1 me me 4096 Dec 16 17:43 lib

wasm-pack build --target web

[INFO]: Checking for the Wasm target…
[INFO]: Compiling to Wasm…
Compiling proc-macro2 v1.0.8
Compiling unicode-xid v0.2.0
Compiling syn v1.0.14
Compiling wasm-bindgen-shared v0.2.58
Compiling log v0.4.8
Compiling cfg-if v0.1.10
Compiling bumpalo v3.2.0
Compiling lazy_static v1.4.0
Compiling wasm-bindgen v0.2.58
error[E0514]: found crate core compiled by an incompatible version of rustc
|
= help: please recompile that crate using this compiler (rustc 1.40.0)
= note: the following crate versions were found:
crate core compiled by rustc 1.40.0 (73528e339 2019-12-16): /nix/store/lm64cjp5c5kc772rpmf88q22s8lr2zhi-rustc-1.40.0/lib/rustlib/wasm32-unknown-unknown/lib/libcore-ef54709e300503ed.rlib

error: aborting due to previous error

error: could not compile cfg-if.
warning: build failed, waiting for other jobs to finish…
error: build failed
Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute cargo build: exited with exit code: 101

For a minimal example setup see that gist.

You need to override your rust targets for wasm build. Here it is a example:

let
  moz_overlay = import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz);
  nixpkgs = import <nixpkgs> { overlays = [ moz_overlay ]; };
in
  with nixpkgs;
  stdenv.mkDerivation {
    name = "moz_overlay_shell";
    buildInputs = [
      # to use the latest nightly:
      (nixpkgs.latest.rustChannels.nightly.rust.override {
        targets = ["wasm32-unknown-unknown"];
      })
      # to use a specific nighly:
      (nixpkgs.rustChannelOf { date = "2018-04-11"; channel = "nightly"; }).rust
      # to use the project's rust-toolchain file:
      # (nixpkgs.rustChannelOf { rustToolchain = ./rust-toolchain; }).rust
    ];
  }

3 Likes

Is there a way to do this without the mozilla overlay? Stable is fine, I don’t need a specific nightly.