Skip to content

Commit 0c84ffb

Browse files
Update wkg docs to be more comprehensive
Signed-off-by: Kate Goldenring <[email protected]>
1 parent 5e559ce commit 0c84ffb

File tree

1 file changed

+66
-42
lines changed

1 file changed

+66
-42
lines changed

component-model/src/creating-and-consuming/distributing.md

+66-42
Original file line numberDiff line numberDiff line change
@@ -6,86 +6,110 @@ Publishing and distribution are not defined by the core component model, but for
66

77
You can get involved with improving the packaging and hosting of Wasm components by joining the [Bytecode Alliance Packaging Special Interest Group (SIG)](https://github.com/bytecodealliance/governance/blob/main/SIGs/sig-packaging/proposal.md).
88

9-
## Registry Tooling
9+
## The `wkg` Registry Tool
1010

11-
The [`wasm-pkg-tools` project](https://github.com/bytecodealliance/wasm-pkg-tools) enables fetching and publishing Wasm Components to OCI or Warg registries. It contains a `wkg` CLI tool that eases distributing and fetching components and WIT packages. The following examples walk through using `wkg`.
11+
The [`wasm-pkg-tools` project](https://github.com/bytecodealliance/wasm-pkg-tools) enables fetching and publishing Wasm components to OCI registries. It contains a `wkg` CLI tool that eases distributing and fetching components and WIT packages. `wkg` contains several subcommand:
1212

13-
## Distributing Components Using `wkg`
13+
- `wkg oci` - is a CLI wrapper around the [oci-wasm](https://github.com/bytecodealliance/rust-oci-wasm) crate which enables pushing/pulling Wasm artifacts to/from any OCI registry
14+
- `wkg publish` - pushes *library* components or WIT packages
15+
- `wkg get` - pulls *library* components or WIT packages
16+
- `wkg wit` - commands for interacting with WIT files and dependencies
17+
- `wkg config` - interact with the `wkg` configuration
1418

15-
A component is pushed to an OCI registry using `wkg oci push`. The example below pushes to a GHCR:
19+
The following sections detail a subset of actions that can be performed with `wkg`.
1620

17-
```sh
18-
wkg oci push ghcr.io/user/hello:0.1.0 hello.wasm
19-
```
21+
## `wkg` Configuration Files
2022

21-
## Fetching Components Using `wkg`
23+
The `wkg` tool uses a configuration file to understand where to publish and fetch specific packages to and from. It provides the ability to configure:
2224

23-
A component is pulled from a OCI registry using `wkg oci pull`. The example below pulls a component from GHCR:
24-
25-
```sh
26-
wkg oci pull ghcr.io/user/hello:0.1.0 -o hello.wasm
27-
```
25+
- a default registry for all packages at the top level of the file
26+
- a default registry for all packages of a specific namespace under `[namespace_registries]`. This section can be used to configure the registry of all `wasi` namespaced packages, such as `wasi:clocks` and `wasi:http`.
27+
- an override for package of a specific namespace under `[package_registry_overrides]`. This section can be used to fetch/publish a specific package of a namespace from/to a different location than all other packages of that namespace. For example, maybe you want to fetch `wasi:http` from a different registry.
28+
- credentials for a registry under `[registry."<registry-name>".oci]`
29+
- and more! See the [`wkg` docs for more configuration options](https://github.com/bytecodealliance/wasm-pkg-tools?tab=readme-ov-file#configuration).
2830

29-
## Configuring `wkg` to Fetch WASI Packages
31+
For example, to fetch WASI packages, such as `wasi:clocks` and `wasi:http`, a line can be added under the `namespace_registries` section for the `wasi` namespace. Specifically, the example below configures `wkg` to fetch WASI packages from the [WebAssembly OCI GitHub Container Registry](https://github.com/orgs/WebAssembly/packages), where the latest interfaces are published upon WASI releases. To edit your `wkg` config file, simply run `wkg config --edit`.
3032

31-
The `wkg` tool uses a configuration file to store settings with a default location of `$XDG_CONFIG_HOME/wasm-pkg/config.toml`. It must be configured to know which registry to use for which package namespaces. The following is a convenient configuration to ensure you can fetch WASI packages from the [WebAssembly OCI registry](https://github.com/WebAssembly/WASI/pkgs/container/wasi), where the latest interfaces are published upon WASI releases.
33+
> Remember, all package names consist of the a namespace field followed by the package field. The package name `wasi:clocks` has a namespace of `wasi` and package field of `clocks`. In this way, the following configuration ensures that `wkg` will know to route fetches and publishes of any `wasi:x` to the configured location.
3234
3335
```toml
3436
# $XDG_CONFIG_HOME/wasm-pkg/config.toml
3537
default_registry = "ghcr.io"
3638

3739
[namespace_registries]
40+
# Instruct wkg to use the OCI protocol to fetch packages with the `wasi` namespace from ghcr.io/webassembly
3841
wasi = { registry = "wasi", metadata = { preferredProtocol = "oci", "oci" = {registry = "ghcr.io", namespacePrefix = "webassembly/" } } }
39-
40-
[package_registry_overrides]
41-
42-
[registry]
4342
```
4443

45-
## Distributing WIT Packages using `wkg`
46-
47-
The `wkg` tool uses a [configuration file](https://github.com/bytecodealliance/wasm-pkg-tools?tab=readme-ov-file#configuration) to store settings with a default location of `$XDG_CONFIG_HOME/wasm-pkg/config.toml`. It must be configured to know which registry to use for which package namespaces. The following configuration, instructs `wkg` to use [ttl.sh](https://ttl.sh/) OCI registry for all packages with the `foo` namespace.
44+
As a more generic example, The following configuration, instructs `wkg` to use [ttl.sh](https://ttl.sh/) OCI registry for all packages with the `docs` namespace.
4845

4946
```toml
5047
# $XDG_CONFIG_HOME/wasm-pkg/config.toml
5148
default_registry = "ghcr.io"
5249

5350
[namespace_registries]
54-
foo = { registry = "foo-registry", metadata = { preferredProtocol = "oci", "oci" = {registry = "ttl.sh", namespacePrefix = "wasm-components/" } } }
51+
# Instruct wkg to use the OCI protocol to fetch packages with the `foo` namespace from ttl.sh/wasm-components
52+
docs = { registry = "docs-registry", metadata = { preferredProtocol = "oci", "oci" = {registry = "ttl.sh", namespacePrefix = "wasm-components/" } } }
53+
```
5554

56-
[package_registry_overrides]
55+
## Distributing WIT and Library Components using `wkg publish`
5756

58-
[registry]
59-
```
57+
Once you've [configured `wkg`](#wkg-configuration-files) to know where to publish packages to, you can use the `wkg publish` command to publish library *components* or *interfaces* to be consumed by others.
6058

61-
Now, `foo` packages can be built and published using `wkg`.
59+
Imagine you have defined the following `adder` world in WIT:
6260

63-
```sh
64-
mkdir wit
65-
cat > wit/foo.wit << EOL
66-
package foo:[email protected];
61+
```wit
62+
package docs:[email protected];
6763
68-
interface do-something {
69-
reduce: func(a: u32, b: u32) -> u32;
64+
interface add {
65+
add: func(a: u32, b: u32) -> u32;
7066
}
7167
72-
world example {
73-
export do-something;
68+
world adder {
69+
export add;
7470
}
75-
EOL
71+
```
72+
73+
You can publish this *WIT* using `wkg` by wrapping it up as a Wasm component. Yes, you heard that right! We are packaging WIT as Wasm.
74+
75+
```sh
7676

7777
wkg wit build
78-
wkg publish foo:bar@0.1.0.wasm
78+
wkg publish docs:adder@0.1.0.wasm
7979
```
8080

81-
This will publish the component to `ttl.sh/wasm-components/foo/bar:0.1.0`
81+
If you had configured `wkg` as described in the [`wkg` configuration section](#wkg-configuration-files), this would publish the component to `ttl.sh/wasm-components/docs/adder:0.1.0`. This WIT can then be fetched using `wkg get`, specifying the format `wit`:
8282

83-
## Configuring `wkg` to Fetch Custom Packages
83+
```sh
84+
wkg get --format wit docs:[email protected]
85+
```
86+
87+
Instead of publishing the WIT interface, you could publish the built component by running:
88+
89+
```sh
90+
wkg publish adder.wasm --package docs:[email protected]
91+
```
92+
93+
This component can then be fetched by running:
94+
95+
```sh
96+
wkg get docs:[email protected]
97+
```
98+
99+
## More Generic Operations with `wkg oci`
100+
101+
The `wkg oci` subcommand is a CLI wrapper around the [oci-wasm](https://github.com/bytecodealliance/rust-oci-wasm) crate which enables pushing/pulling Wasm artifacts to/from any OCI registry. Unlike `wkg publish` and `wkg get` it is not limited to library components, as providing the WIT package is not required.
102+
103+
A component is pushed to an OCI registry using `wkg oci pull`. The example below pulls a component from a GitHub Container Registry.
104+
105+
```sh
106+
wkg oci push ghcr.io/user/component:0.1.0 component.wasm
107+
```
84108

85-
After configuring `wkg` to know where to pull `foo` namespaced packages from, the `bar` package can be pulled with `wkg get`:
109+
To pull a component, run:
86110

87111
```sh
88-
wkg get --format wit foo:bar@0.1.0
112+
wkg oci pull ghcr.io/user/component:0.1.0 -o component.wasm
89113
```
90114

91115
## Fetching WIT Package Dependencies using `wkg`
@@ -100,7 +124,7 @@ world target-world {
100124
}
101125
```
102126

103-
One may be tempted to simply get the `wasi:http` package with `wkg get --format wit wasi:[email protected] -o wit/deps/http/`. However, `wasi:http` depends on other WASI packages such as `wasi:clocks` and `wasi:io`. To make sure to fetch a package and all its dependencies, use `wkg fetch`, which will read the package containing the world(s) you have defined in the given wit directory (`wit` by default). It will then fetch the
127+
You may be tempted to simply get the `wasi:http` package with `wkg get --format wit wasi:[email protected] -o wit/deps/http/`. However, `wasi:http` depends on other WASI packages such as `wasi:clocks` and `wasi:io`. To make sure to fetch a package and all its dependencies, use `wkg wit fetch`, which will read the package containing the world(s) you have defined in the given wit directory (`wit` by default). It will then fetch the
104128
dependencies and write them to the `deps` directory along with a lock file.
105129

106130
After placing the above file in `./wit`, run the following to fetch the dependencies:

0 commit comments

Comments
 (0)