First steps writing Holochain hApps with Rust

Willem Olding
Published in
7 min readNov 19, 2018


Now updated for Holochain v0.0.4-alpha!

If you have been following the development of Holochain you may be aware that a developer preview of the next iteration has been released.

Holochain hApps are made of compiled WebAssembly that encodes the rules of the hApp, the data it can store and how users will interact with it. This means that any language that can compile to WebAssembly can one day be used for Holochain.

Writing WebAssembly that complies with the Holochain runtime can be tricky. To make development as streamlined as possible the core team has been developing a Holochain-dev-kit (HDK) for the first supported language, Rust! In the near future the community is encouraged to develop an HDK for their language of choice.

In this article we will walk through the steps of creating a simple hApp using Rust and highlight some of the differences from the previous version.


First step is to download the appropriate dev preview release for your OS. If you decide to build the latest version from source, be warned that the API is undergoing rapid change, so some of the steps in this article may not work. The release contains the binary for the holochain developer command line tool, hc, which is used to generate a skeleton app, run tests and build the app package. Holochain also depends on ZeroMQ being installed locally. Follow the installations on this page to install the required dependencies.

Ensure that hc is available on your path. If you instead decide to build from source cargo will ensure the binaries are on your path automatically.

If you want to jump ahead to see what the completed project will look like, the full source code is available on GitHub.

First steps

We will be making a classic to-do list hApp. A user can create new lists and add items to a list. They should also be able to retrieve a list by its address and all of the items on each list.

Let’s begin by generating an empty hApp skeleton by running:

hc init todo-list

This will generate the following directory structure:

├── app.json
├── test
│ └── …
└── zomes

Notice the `zomes` directory. All Holochain hApps are comprised of one or more zomes. They can be thought of as similar to modules in JavaScript, each one should provide some self-contained functionality. Every zome has its own build system so it is possible to combine zomes written in different languages to produce a single hApp.

We will create a single zome called `lists` that uses a Rust build system:

cd todo-list
hc generate zomes/lists rust

The project structure should now be as follows:

├── app.json
├── test
│ └── …
└── zomes
└── lists
├── code
│ ├── .hcbuild
│ ├── Cargo.toml
│ └── src
│ └──
└── zome.json

Writing the lists zome

The Rust HDK makes use of Rust macros to reduce the need for boilerplate code. The most important of which is the `define_zome!` macro. Every zome must use this to define the structure of the zome, what entries it contains, which functions it exposes and what to do on first start-up (genesis).

Open up `` and replace its contents with the following:

This is the simplest possible zome with no entries and no exposed functions.

Adding some Entries

Unlike in holochain-proto, where you needed to define a JSON schema to validate entries, holochain entries in Rust map to a native struct type. We can define our list and listItem structs as follows:

You might notice that the `List` struct does not contain a field that holds a collection of `ListItem`s. This will be implemented using links, which we will discuss later.

Also be sure to add the following to the list of imports:

use std::convert::TryFrom;
extern crate hdk;
extern crate serde_derive;
extern crate holochain_core_types_derive;
use hdk::{
error::{ZomeApiResult, ZomeApiError},
entry::{AppEntryValue, Entry},

The `Serialize` and `Deserialize` derived traits allow the structs to be converted to and from JSON, which is how entries are managed internally in Holochain. The DefaultJson derived trait comes from the holochain HDK itself and allows for seamless converting between data stored in the DHT and rust structs.

These structs on their own are not yet valid Holochain entries. To create these we must include them in the `define_zome!` macro by using the `entry!` macro:

Take note of the `native_type` field of the macro which gives which Rust struct represents the entry type. The `validation_package` field is a function that defines what data should be passed to the validation function through the `ctx` argument. In this case we use a predefined function to only include the entry itself, but it is also possible to pass chain headers, chain entries or the full local chain. The validation field is a function that performs custom validation for the entry. In both our cases we are just returning `Ok(())`.

Take note also of the `links` field. As we will see later links are the main way to encode relational data in holochain. The `links` section of the entry macro defines what other types of entries are allowed to link to and from this type. This also includes a validation function for fine grain control over linking.

Adding Functions

Finally we need a way to interact with the hApp. We will define the following functions: create_list, add_item and get_list. get_list will retrieve a list and all the items linked to each list.

For each of these functions we must define a handler, which is a Rust function that will be executed when the conductor calls the function. (For more on conductors, read Nico’s recent post.) It is best practice for functions to always return a ZomeApiResult<T>, where T is the type the function should return if it runs without error. This is an extension of the Rust Result type and allows zome functions to abort early on errors using the ? operator. At the moment the handler function names cannot be the same as the function itself so we will prefix them with handle_. This will be fixed in an upcoming release. The handler for create_list could be written as:

The `hdk::commit_entry` function is how a zome can interact with holochain core to add entries to the DHT or local chain. This will trigger the validation function for the entry and if successful will store the entry and return its hash/address.

The `add_item` function requires the use of holochain links to associate two entries. In holochain-proto this required the use of a commit with a special Links entry but it can now be done using the HDK function `link_entries(address1, address2, tag)`. The add item handler accepts a `ListItem` and an address of a list, commits the `ListItem`, then links it to the list address:

At the moment there is no validation done on the link entries. This will be added soon with an additional validation callback.

Finally, `get_list` requires us to use the HDK function `get_links(base_address, tag)`. As you may have guessed, this will return the addresses of all the entries that are linked to the `base_address` with a given tag. As this only returns the addresses, we must then map over each of then and load the required entry.

Phew! That is all the handlers set up. Finally the function definitions must be added to the `define_zome!` macro. Before doing that, it is worth briefly discussing a new concept in Holochain, *traits*. Traits allow functions to be grouped to control access and in the future will allow hApps to connect to other hApps that implement a particular trait. At this time the only trait we need to consider is the hc_public trait. This is a special named trait that exposes all of the contained functions to the outside world.

The function field of our zome definition should be updated to:

and there we have it! If you are coding along the full should now look like this:

The zome we created should now build if we run:

hc package

from the root directory. This will compile the Rust to WebAssembly and produce a `bundle.json` file which contains the compiled WASM code and the required metadata. This is the file that we can load and run using `hc`.

Writing tests

Developers who have worked previously with holochain-proto will be pleased to hear there is an entirely new testing framework built on JavaScript. The framework is build around Tape.js and allows for writing single agent and muti-agent tests using javascript async/await syntax.

Opening up the `test/index.js` file you will see a skeleton test file already created:

This illustrates the `` function that is exposed by the conductor for each app and that can be used to call our functions. Take note that the input-data should be a JSON object that matches the function signature. `call` will also return a JSON object.

Lets add some tests for our todo list:

Running `hc test` will build the test file and run it using `node` which is able to load and execute holochain hApps via the holochain node conductor. If everything has worked correctly you should see some test output with everything passing.

Pro tip: Pipe the output to tap-spec (which must be installed via npm first) to get beautifully formatted test output.


And there we have it! A simple zome created in holochain using the Rust HDK. Those with previous experience developing for holochain-proto will appreciate the reduced boilerplate, strong typing and vastly improved testing framework.

The complete working version of this project is available on github. This builds under the 0.0.4-alpha release but as the API and HDK are changing it will likely fail under newer releases.