主要观点:作者长期看好 WebAssembly(Wasm),认为其具有超越 Linux 虚拟机和 OCI 容器的可移植性和操作简单性,可在多种环境运行,但构建 Wasm 并不简单,存在多种语言编译工具、Wasm 特定工具和运行时,使用 Nix 打包 Wasm 是不错的选择。
关键信息:
- 作者创建了 DeterminateSystems/nix-wasm-example 项目展示如何用 Nix 处理 Wasm,构建的 Rust 程序输出特定字符串,符合 WebAssembly System Interface(WASI)。
- 通过特殊的 Nix 函数
buildRustWasiWasm
编译为符合 WASI 的 Wasm,还使用wasm-strip
和wasm-validate
优化和验证二进制。 nix build
命令能从 Rust 源构建单个 Wasm 二进制,buildRustWasmPackage
函数能构建包含更多信息的 Wasm 包,如hello-wasm-dump.txt
、hello-wasm.dist
和hello-wasm.wat
。- 用 Nix 可直接解决将 WASI 兼容的 Wasm 转换为系统调用的问题,通过创建使用 Wasm 运行时的派生式来运行编译后的 Wasm 二进制,如用 Wasmtime 和 WasmEdge 运行。
重要细节: - 多种语言可编译为 Wasm,各有其工具和方法。
- 有众多 Wasm 特定工具,如 wasm-tools 和 WABT。
- 当前有多个 Wasm 运行时,如 WasmEdge 和 Wasmtime。
- Nix 能确定性地构建 WebAssembly,无需使用
apt-get
或 Homebrew 等,且能做更多,如构建包含多种信息的 Wasm 包。 - 此示例项目虽简单,但展示了 Nix 在 WebAssembly 领域的可能性,说明 Nix 在构建和打包 Wasm 方面优于常规工具,即使在未来行业完全转向 Wasm 且超越容器和虚拟机的世界,Nix 也不可或缺。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。