Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

DeterminateSystems/nix-wasm-example

Repository files navigation

Nix + WebAssembly example project

[!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.

Setup

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.

Packages

Below are some packages you can build in this project.

Build Wasm binary

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).

Build a full Wasm package

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)

Run the binary

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"

Advantages of Nix for Wasm development

  • 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.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages