[!INFO] This repo was created as a complement to the Nix as a WebAssembly build tool blog post on the Determinate Systems blog and fulfilled its purpose. It won't be updated further but you're free to use it as you see fit!
This repo houses an example project that uses Nix to build and hack on WebAssembly (Wasm) in Rust.
First, make sure that Nix is installed with flakes enabled. We recommend using our Determinate Nix Installer:
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix \
| sh -s -- install
With Nix installed, you can activate the development environment:
nix develop
Note
This should happen automatically if you have direnv installed and run direnv allow
.
Below are some packages you can build in this project.
To build a Wasm binary from the Rust sources:
nix build ".#hello-wasm"
This generates a stripped Wasm binary at result/lib/hello-wasm.wasm
(where result
is a symlink to a Nix store path).
To build a Wasm binary from the Rust sources plus some other goodies:
nix build ".#hello-wasm-pkg"
# Inspect the package
tree result
Inside the package you should see:
lib/hello-wasm.wasm
(the same stripped binary from above)share/hello-wasm-dump.txt
(an object dump)share/hello-wasm.dist
(a stats file)share/hello-wasm.wat
(a human-readable WAT file)
You can also run the compiled binary using two different Wasm runtimes.
To run it using WasmEdge:
nix run ".#hello-wasmedge-exec"
To run it using Wasmtime:
nix run ".#hello-wasmtime-exec"
- Many languages can build Wasm but creating multi-language development environments (without Nix, of course) is hard.
- Successful Wasm development environments often a wide range of tools, compilers, runtimes, etc.
- Nix can not only provide arbitrarily complex development environments but it can do so across Unix-based platforms.