How to build webassembly via rust using just nix

September 23, 2020 ยท View on GitHub

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 https://rustwasm.github.io/wasm-pack/book/prerequisites/non-rustup-setups.html 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