Using Nx Release with Rust

This recipe guides you through versioning Rust libraries, generating changelogs, and publishing Rust crates in a monorepo with Nx Release.

Initialize Nx Release in Your Workspace

Install Nx

Ensure that Nx is installed in your monorepo. Check out the Installation docs for instructions on created a new Nx workspace or adding Nx to an existing project.

Add the @monodon/rust plugin

The @monodon/rust package is required for Nx Release to manage and release Rust crates. Add it if it is not already installed:

โฏ

nx add @monodon/rust

Configure Projects to Release

Nx Release uses Nx's powerful Project Graph to understand your projects and their dependencies.

If you want to release all of the projects in your workspace, such as when dealing with a series of Rust crates, no configuration is required.

If you have a mixed workspace in which you also have some applications, e2e testing projects or other things you don't want to release, you can configure nx release to target only the projects you want to release.

Configure which projects to release by adding the release.projects property to nx.json. The value is an array of strings, and you can use any of the same specifiers that are supported by nx run-many's projects filtering, such as explicit project names, Nx tags, directories and glob patterns, including negation using the ! character.

For example, to release just the projects in the crates directory:

1{ 2 "release": { 3 "projects": ["crates/*"] 4 } 5} 6

Create the First Release

The first time you release with Nx Release in your monorepo, you will need to use the --first-release option. This tells Nx Release not to expect the existence of any git tags, changelog files, or published packages.

Use the --dry-run option

The --dry-run option is useful for testing your configuration without actually creating a release. It is always recommended to run Nx Release once with --dry-run first to ensure everything is configured correctly.

To preview your first release, run:

โฏ

nx release --first-release --dry-run

Pick a New Version

Nx Release will prompt you to pick a version bump for all the crates in the release. By default, all package versions are kept in sync, so the prompt only needs to be answered one time.

~/workspaceโฏ

nx release --first-release --dry-run

1 2NX Running release version for project: pkg-1 3 4pkg-1 ๐Ÿ” Reading data for crate "pkg-1" from crates/crates/pkg-1/Cargo.toml 5pkg-1 ๐Ÿ“„ Resolved the current version as 0.1.0 from crates/pkg-1/Cargo.toml 6? What kind of change is this for the 3 matched projects(s)? โ€ฆ 7โฏ major 8 premajor 9 minor 10 preminor 11 patch 12 prepatch 13 prerelease 14 Custom exact version 15

Preview the Results

After this prompt, the command will finish, showing you the preview of changes that would have been made if the --dry-run option was not passed.

~/workspaceโฏ

nx release --first-release --dry-run

1 2NX Running release version for project: pkg-1 3 4pkg-1 ๐Ÿ” Reading data for crate "pkg-1" from crates/crates/pkg-1/Cargo.toml 5pkg-1 ๐Ÿ“„ Resolved the current version as 0.1.0 from crates/pkg-1/Cargo.toml 6โœ” What kind of change is this for the 3 matched projects(s)? ยท patch 7pkg-1 โœ๏ธ New version 0.1.1 written to crates/crates/pkg-1/Cargo.toml 8 9NX Running release version for project: pkg-2 10 11pkg-2 ๐Ÿ” Reading data for crate "pkg-2" from crates/crates/pkg-2/Cargo.toml 12pkg-2 ๐Ÿ“„ Resolved the current version as 0.1.0 from crates/pkg-2/Cargo.toml 13pkg-2 โœ๏ธ New version 0.1.1 written to crates/crates/pkg-2/Cargo.toml 14 15NX Running release version for project: pkg-3 16 17pkg-3 ๐Ÿ” Reading data for crate "pkg-3" from crates/crates/pkg-3/Cargo.toml 18pkg-3 ๐Ÿ“„ Resolved the current version as 0.1.0 from crates/pkg-3/Cargo.toml 19pkg-3 โœ๏ธ New version 0.1.1 written to crates/crates/pkg-3/Cargo.toml 20 21UPDATE crates/pkg-1/Cargo.toml [dry-run] 22 23 [package] 24 name = "pkg-1" 25- version = "0.1.0" 26+ version = "0.1.1" 27 edition = "2021" 28 29 30UPDATE crates/pkg-2/Cargo.toml [dry-run] 31 32 [package] 33 name = "pkg-2" 34- version = "0.1.0" 35+ version = "0.1.1" 36 edition = "2021" 37 38 39UPDATE crates/pkg-3/Cargo.toml [dry-run] 40 41 [package] 42 name = "pkg-3" 43- version = "0.1.0" 44+ version = "0.1.1" 45 edition = "2021" 46 47 48NX Updating Cargo.lock file 49 50 51NX Staging changed files with git 52 53 54NX Previewing an entry in CHANGELOG.md for v0.1.1 55 56 57CREATE CHANGELOG.md [dry-run] 58+ ## 0.1.1 (2024-02-29) 59+ 60+ This was a version bump only, there were no code changes. 61 62 63NX Staging changed files with git 64 65 66NX Committing changes with git 67 68 69NX Tagging commit with git 70 71 72NX Skipped publishing packages. 73 74 75NOTE: The "dryRun" flag means no changes were made. 76

Run Without --dry-run

If the preview looks good, run the command again without the --dry-run option to actually create the release.

โฏ

nx release --first-release

The command will proceed as before, prompting for a version bump and showing a preview of the changes. However, this time, it will prompt you to publish the crates to the remote registry. If you say no, the publishing step will be skipped. If you say yes, the command will publish the crates to https://crates.io.

~/workspaceโฏ

nx release --first-release

1... 2 3โœ” Do you want to publish these versions? (y/N) ยท true 4 5NX Running target nx-release-publish for 3 projects: 6 7- pkg-1 8- pkg-2 9- pkg-3 10 11โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€” 12 13(...cargo publish output here...) 14 15โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€” 16 17NX Successfully ran target nx-release-publish for 3 projects 18 19

Manage Git Operations

By default, Nx Release will stage all changes it makes with git. This includes updating Cargo.toml files, creating changelog files, and updating the Cargo.lock file. After staging the changes, Nx Release will commit the changes and create a git tag for the release.

Customize the Commit Message and Tag Pattern

The commit message created by Nx Release defaults to 'chore(release): publish {version}', where {version} will be dynamically interpolated with the relevant value based on your actual release, but can be customized with the release.git.commitMessage property in nx.json.

The structure of the git tag defaults to v{version}. For example, if the version is 1.2.3, the tag will be v1.2.3. This can be customized by setting the release.releaseTagPattern property in nx.json.

For this same example, if you want the commit message to be 'chore(release): 1.2.3' and the tag to be release/1.2.3, you would configure nx.json like this:

1{ 2 "release": { 3 "releaseTagPattern": "release/{version}", 4 "git": { 5 "commitMessage": "chore(release): {version}" 6 } 7 } 8} 9

Future Releases

After the first release, the --first-release option will no longer be required. Nx Release will expect to find git tags and changelog files for each package.

Future releases will also generate entries in CHANGELOG.md based on the changes since the last release. Nx Release will parse the feat and fix type commits according to the Conventional Commits specification and sort them into appropriate sections of the changelog. An example of these changelogs can be seen on the Nx releases page.