Skip to content

nvs ext new and nvs ext build

nvs ext new creates an extension project, and nvs ext build makes the .nvsx file from it.

An extension adds one class to Novis. You write its code in Rust, C or another language that compiles to WebAssembly. nvs ext new --lang rust <dir> or nvs ext new --lang c <dir> writes a project with one function, a test and a README. You build the project with your own compiler. Then nvs ext build reads nvsx.toml, puts the wasm file and the Novis source files into one .nvsx file, and checks that the file loads. It prints the path of the file and its sha256.

A program uses the class after nvs.toml lists the file in an [[extension]] entry with that sha256.

Good to know: nvs ext build does not compile your code. Build for wasm32-wasip2, because a WASI preview 1 module does not build. The same project always gives the same bytes.

Examples

The examples below run with this configuration file.

nvs.toml
# The extension `Geo\Atlas` is a `.nvsx` file that `nvs ext build` made.
# `sha256` is the line that `nvs ext build` printed. The extension may read the folder `data/`.
[[extension]]
path = "../../config/extension-grants-what-a-component-may-reach/atlas.nvsx"
sha256 = "8bb2081b5d76340eb1cdc2cf22b0eaa2144bb7851b83abb5662c1b369ce9694b"
grants = { read = ["data/"] }
# The program in this folder may read the folder `data/` too.
[capabilities.fs]
read = ["data/"]

Call the class you built

<?nvs
// Shows a program that calls the class of an extension you built with `nvs ext build`.
use Geo\Atlas;
// The class `Geo\Atlas` comes from the `.nvsx` file that `nvs.toml` lists.
// `Atlas::read` returns the text of a file in `data/`, so this prints `Welcome to the atlas.`.
echo Atlas::read('welcome.txt');
// `missing.txt` is not in `data/`. So the call throws a `RuntimeError`, and this prints `no such file`.
try {
echo Atlas::read('missing.txt');
} catch (RuntimeError $error) {
echo $error->message, "\n";
}
Output
Welcome to the atlas.
no such file