Building rust crate with wasm-pack fails because of missing libz.so.1

I’m trying to build a Rust crate to the wasm32-unknown-unknown target, and that fails, unexpectedly so.

Let me explain:

  • First off, I have rustup installed via nix, and cargo, rustc et al installed via rustup
  • I am using the nixos-unstable channel
  • For now I am using a nix shell by executing nix-shell -p wasm-pack zlib pkgconf (note zlib here)
  • Within that nix-shell I then try to build my library with wasm-pack build --target=nodejs
  • Building the crate with cargo build or cargo build --release works just fine. So do executing cargo test and cargo test --release i.e. it’s not an issue with the crate itself

Furthermore, this is the output of rustup show:

Default host: x86_64-unknown-linux-gnu
rustup home: /home/j/.rustup

installed toolchains
--------------------

stable-x86_64-unknown-linux-gnu (default)
nightly-x86_64-unknown-linux-gnu

installed targets for active toolchain
--------------------------------------

wasm32-unknown-unknown
x86_64-unknown-linux-gnu

active toolchain
----------------

stable-x86_64-unknown-linux-gnu (default)
rustc 1.77.0 (aedd173a2 2024-03-17)

Executing wasm-pack build --target=nodejs yields a wall of text ending in

  = note: rust-lld: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory

This is strange because I explicitly request zlib when opening my nix-shell.

Does anyone have any ideas w.r.t what could be causing this? Any help would be greatly appreciated.

Maybe a very stupid comment, but zlib and libz are different… maybe try to load libz instead of zlib? (Sorry I can’t check locally if it works as I don’t have my computer)

Also tried that. No difference in output whatsoever unfortunately.

I even tried to add all of zlib, zlib-ng and libz to my nix-shell at the same time. Given the first paragraph of this answer, it won’t surprise you that that doesn’t work either.

EDIT: the probleem seems to lie with cargo and/or rustc. I arrived at this by examining the last couple of lines from the wasm-pack output, which look like this:

error: could not compile `sbr-xmile-core` (lib) due to 1 previous error; 3 warnings omitted
Error: Compiling your crate to WebAssembly failed
Caused by: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit status: 101
  full command: cd "/home/j/dev/sbr-xmile-core" && "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"

So I tried executing cargo build --lib --release --target wasm32-unknown-unknown and that results in the same rust-lld error.