Skip to main content

Modules

CUE modules are collections of packages that are released, versioned, and distributed together. They provide a way to organize and manage dependencies in CUE projects. Here's an explanation of how they work and how to use them:

A CUE module is defined by a cue.mod directory in its root, containing a module.cue file. This file specifies the module's path and other metadata. Here's an example of a module.cue file:

module: "example.com/mymodule"
language: {
version: "v0.9.0" // CUE language version
}

Package Organization:

Within a module, CUE files are organized into packages. All files with the same package name within a module belong to the same package. Packages can be imported and used in other CUE files.

Importing External Modules:

To use packages from external modules, you can import them in your CUE files. Here are some examples of import snippets:

// Importing a package from an external module
import "github.com/example.com/mymodule/:package1"

// Importing a package with an alias
import foo "github.com/example.com/mymodule/:package1"

// Importing multiple packages
import (
"github.com/example.com/mymodule1/:package1"
"github.com/example.com/mymodule2/:package2"
)

Publishing to an OCI Registry:

To package and publish a CUE module to an OCI registry, you can use the cue mod publish command. You can find details about using CUE custom registries here.

# Initialize a new module
cue mod init example.com/mymodule@v1

# Add your CUE files to the module
# ...
# Download dependencies
cue mod tidy

# Setup your OCI registry url
export CUE_REGISTRY=localhost:5000/cuemodules

# Publish the module to an OCI registry
cue mod publish v1.0.0

Module Dependencies:

When your module depends on other modules, you can specify these dependencies in the module.cue file. The cue mod tidy command will automatically add and update these dependencies. For example:

module: "example.com/mymodule@v1"
deps: {
"github.com/example/dependency@v1": v: "v1.2.3"
}

Using Modules in a Project:

When working on a project that uses modules, you typically have a main module and potentially several dependency modules. The cue.mod directory in your project root defines the main module, and the cue mod tidy command ensures all dependencies are properly managed.

By using modules, you can easily share and reuse CUE configurations across projects, manage versioning, and maintain a clear structure for your CUE code.

For more detailed information on CUE modules, packages, and instances, you can refer to the CUE documentation.