Build and package your first application
Create a Wasm application from the template, run it in Xila, package it as a signed .xpk file, and prepare it for the registry.
In this tutorial, you create a WebAssembly (Wasm) application from the Xila template, run it in Xila’s headless runtime, and package it as a signed .xpk file. At the end, you have a signed package and the steps to publish it in the Xila registry.
The following diagram shows the files that you create in each stage. The last two steps run xila-package bundle build and xila-package bundle archive.
The project builds a guest. The runtime runs the guest in developer mode. The package tool signs the guest into a bundle and an archive. The registry lists the released archive.
Before you begin
Section titled “Before you begin”- An x86-64 Linux computer. The tutorial commands ran on Linux.
- Git and Rustup. Rustup reads the
rust-toolchain.tomlfile of the template. It installs Rust 1.97.0 and thewasm32-wasip1target. - Docker, set up so that you can start containers. Docker runs Xila’s headless runtime.
- A network connection. The first build downloads the Xila guest library from GitHub.
- A GitHub account and the GitHub CLI, for the last stage only.
Create the project
Section titled “Create the project”-
In a host shell, clone the Wasm application template into a new directory and enter it:
git clone https://github.com/xila-project/wasm_application_template.git hello-xilacd hello-xilaYou can also select Use this template on GitHub and clone your new repository.
-
In the project directory, open
Cargo.tomland change the package name:name = "hello-xila" -
In the project directory, open
src/main.rs. Change the greeting in the function that has thewasm32condition:#[cfg(target_arch = "wasm32")]fn main() {println!("Hello from my first Xila application!");}
The project now has your name and your greeting. The second main function runs when you build for another target. Leave it unchanged.
Build the application
Section titled “Build the application”-
In the project directory, build the release version for the
wasm32-wasip1target:cargo build --release --target wasm32-wasip1Rustup first installs the toolchain of the template. Cargo then downloads and builds the Xila guest library. The last line is similar to this one:
Finished `release` profile [optimized] target(s) in 11.55s -
In the same directory, check the result:
file target/wasm32-wasip1/release/hello-xila.wasmThe output is:
target/wasm32-wasip1/release/hello-xila.wasm: WebAssembly (wasm) binary module version 0x1 (MVP)
The file target/wasm32-wasip1/release/hello-xila.wasm is your application. The build.rs file of the template links it with a 32 KiB stack and byte-sized memory pages, as the Xila applications do.
Run the application in Xila
Section titled “Run the application in Xila”The browser demonstration runs only guests that are already on its disk, so this stage uses the headless runtime. This runtime is the native Xila runtime in a Docker image. It starts a command, prints its output and exits.
-
In a host shell, download the image and create a volume. The volume keeps the Xila disk between runs.
docker pull ghcr.io/xila-project/xila-runtime:latestdocker volume create xila-first-application -
In the project directory, run the application. The first
--mountoption gives the container read access to the build directory. The second--mountoption, afterrun, makes that directory visible to Xila as/guest.docker run --rm --network none --read-only \--tmpfs /tmp:rw,noexec,nosuid,size=16m \--mount type=volume,src=xila-first-application,dst=/var/lib/xila \--mount "type=bind,src=$PWD/target/wasm32-wasip1/release,dst=/mnt/xila/guest,readonly" \ghcr.io/xila-project/xila-runtime:latest run --network=none \--mount host=/mnt/xila/guest,xila=/guest,ro \-- /applications/executables/wasm --developer-mode /guest/hello-xila.wasmThe runtime prints a warning and your greeting. The warning has colors in a terminal:
W | wasm::host::main | Running unverified WebAssembly executable /guest/hello-xila.wasm in developer modeHello from my first Xila application! -
In the same shell, check the exit status:
echo $?The output is
0.
Xila runs only guests from an installed package. The --developer-mode option allows an unverified guest, and the warning says so. The next stage creates the package that you distribute. See Run a guest application to start a guest from Xila’s own terminal.
Package the application
Section titled “Package the application”-
In a host shell, install the
xila-packagetool:cargo install --locked --git https://github.com/xila-project/package_manager xila-package-builderCargo installs the
xila-packagecommand in~/.cargo/bin. Thexila packagecommand of the Xila tools runs the same code with the same subcommands. -
In the project directory, create the package source directory. Replace
examplein the identifier with your GitHub account name in lowercase. The identifier must have at least three parts. The registry gives a package identifier only to the developer key that holds its namespace.xila-package bundle init ./package io.github.example.hello-xila 0.1.0 wasm32-wasip1 \--xila-version 0.1 \--entrypoint executables/hello-xila.wasm \--display-name "Hello Xila"The command creates
package/xila-package.jsonand the directoriespackage/executablesandpackage/resources. -
In the project directory, copy the application into the package source:
cp target/wasm32-wasip1/release/hello-xila.wasm package/executables/ -
Open
package/xila-package.json. Replace"metadata":nullwith the description of your package. The registry copies this text into its entry. Replace the source URL with the URL of your repository."metadata": {"summary": "Prints a greeting.","license": "MIT","source": "https://github.com/example/hello-xila","homepage": null} -
In the project directory, create a signing key pair outside the project. The command creates the files
private.seedandpublic.key.mkdir ../hello-xila-keysxila-package key generate ../hello-xila-keys/private.seed ../hello-xila-keys/public.key -
In the project directory, build and sign the bundle:
xila-package bundle build ./package ./target/HelloXila.app \--signing-key ../hello-xila-keys/private.seedThe command prints nothing. The directory
target/HelloXila.apphas the files.xila/manifest.json,.xila/manifest.sigandexecutables/hello-xila.wasm. The manifest lists the size and the SHA-256 hash of every file. The signature covers the manifest. -
In the same directory, verify the bundle with your public key. Then create the archive and verify it:
xila-package bundle verify ./target/HelloXila.app --public-key ../hello-xila-keys/public.keyxila-package bundle archive ./target/HelloXila.app ./target/HelloXila.xpkxila-package verify ./target/HelloXila.xpk ../hello-xila-keys/public.keyEach command prints nothing and exits with status
0. The filetarget/HelloXila.xpkis your package. It has the same three files as the bundle, and the manifest comes first.
Publish the package
Section titled “Publish the package”The registry lists a package by its download address. You must therefore publish the .xpk file first. The registry stores only the metadata of each version, and published versions cannot change.
-
Push your project to a public GitHub repository. Its URL must be the
sourcevalue of your package metadata. -
In the project directory, create a GitHub release that has the package as an asset. This command uses the GitHub CLI:
gh release create v0.1.0 target/HelloXila.xpk --title "Hello Xila 0.1.0" --notes "First release." -
In a host shell, print your public key as base64 text. A new publisher gives this text in the submission form:
base64 -w0 ../hello-xila-keys/public.key -
Open the Submit a package issue form of the registry. Give the HTTPS address of the released
.xpkfile, your publisher identifier, your base64 public key and your author name.
The Package submission workflow of the registry downloads the archive and checks its signature, hashes and metadata. It writes the result as a comment on the issue and opens a pull request with the registry entry. For an identifier in io.github.<account> that you submit from that GitHub account, the workflow also creates the namespace of your account. A maintainer reviews the first submission of a new publisher. To submit a pull request without the form, see Submit a package.
Check your result
Section titled “Check your result”You created the project hello-xila from the template. You built hello-xila.wasm, and Xila’s headless runtime printed its greeting. You also have the signed package target/HelloXila.xpk, and your public key verifies it.
To remove the Docker volume of this tutorial, run:
docker volume rm xila-first-applicationYour signing keys stay in ../hello-xila-keys. Keep them if you want to publish new versions.
Next steps
Section titled “Next steps”- Add a launcher shortcut so that the graphical shell lists your application.
- Package an application for the package options in detail.
- Guest API to read what a guest can call.
- Package management to learn how the registry and the signing keys work together.