Skip to main content

Canpack

Canpack is a code generation tool designed to simplify communication across canisters written in different languages. It currently supports calling a Rust crate from Motoko code. Canpack generates a separate canister for the host language, then combines the other language's code fragments that are defined across different libraries.

Canpack supports the Mops package manager.

Canpack is still in early development. Breaking changes may be introduced at any time.

Installation

Install the IC SDK.
Download and install Rust.
Download and install the wasm32-unknown-unknown target: rustup target add wasm32-unknown-unknown

Then, install the Canpack CLI using npm:

npm install -g canpack

Using Canpack

In a project directory that contains a dfx.json and mops.toml file, you can use Canpack by first creating a new file called canpack.json with the following content:

{
    "canisters": {
        "motoko_rust": {
            "type": "rust",
            "parts": [{
                "package": "canpack-example-hello",
                "version": "^0.1"
            }]
        }
    }
}

Then, generate the required files using the command:

canpack

In the project's dfx.json file, configure the "dependencies" for your project's Motoko canister:

{
    "canisters": {
        "my_project_backend": {
            "dependencies": ["motoko_rust"],
            "main": "src/my_project_backend/main.mo",
            "type": "motoko"
        }
    },
}

Then, to write a Motoko canister that imports Rust crates, import Rust from the motoko_rust canister:

import Rust "canister:motoko_rust";

actor {
    public composite query func hello(name : Text) : async Text {
        await Rust.canpack_example_hello(name)
    }
}

Motoko canisters can import any Rust crate that is compatible with Canpack. Canpack supports any ICP Wasm-compatible crate.

Resources

Canpack is open to contributions and encourages you to report bugs, ask questions, or request features using the project's GitHub issues.

Logo