Nix as a WebAssembly build tool


Great post thank you! I always have wondered how to get that wasm-enabled Rust compiler and use it to successfully build a wasm binary :+1:

Do you have planned followups around this?
Thinking like making this easier for everyone by making wasm compilation first class in nixpkgs :thinking:

1 Like

I just tried some of those examples out:

nix build "*.tar.gz#hello-wasm"
       … while updating the lock file of flake ''

       error: unsupported tarball input attribute 'lastModified'

(deleting the flake.lock worked and produced a diff with out the lastModified field - is this a nix version thing?)

Yes, this is definitely a Nix version thing that I’ve encountered as well, although sadly I’m not quite sure which exact versions will work

No, I don’t have any specific follow-up planned. My helper functions worked well enough for my example and I’m not sure what a more generalized, cross-language solution for Nixpkgs would look like.

Check out this PR I’ve been working on recently to add support for wasm to buildRustCrate/crate2nix (offers much faster build time compared to naersk or buildRustPackage) buildRustCrate: add support for compiling to wasm32-unknown-unknown by Kranzes · Pull Request #319153 · NixOS/nixpkgs · GitHub

For something like buildRustPackage which uses Cargo to build your simply have to pass --target via cargoBuildFlags and add pkgs.rustc.llvmPackages.lld to nativeBuildInputs, the reason this works is because the rustc in nixpkgs is built with support for targeting wasm32-unknown-unknown out of the box. For Wasi you might need to configure it a bit differently though.


Awesome thank you for working on this!
I’d be more interested by wasi than wasm but this is a good start :sparkles:

@bew This project does build WASI, FYI