hercules-ci.cargo-publish
Complete Example
{
description = "A Rust project with automated publishing";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
hercules-ci-effects.url = "github:hercules-ci/hercules-ci-effects";
};
outputs = inputs@{ self, flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [
inputs.hercules-ci-effects.flakeModule
];
systems = [ "x86_64-linux" ];
hercules-ci.cargo-publish = {
enable = true;
secretName = "crates-io";
assertVersions = true; # Check all packages match the tag version
};
};
}
Workflow
Initial Setup
-
Enable the module in your
flake.nixwithhercules-ci.cargo-publish.enable = true -
Set the version in
Cargo.tomlto a version that doesn’t exist on crates.io yet (e.g.,0.1.0) -
On each push, Hercules CI runs
cargo publish --dry-runto verify the package is publishable
Releasing a Version
-
Review the version — the version in
Cargo.tomlis probably a patch bump from the last release. Consider whether breaking changes or new features warrant a minor or major bump instead. -
Push a tag matching the version in
Cargo.toml:git tag 0.1.0 git push origin 0.1.0shell -
Hercules CI detects the tag and runs
cargo publish -
Bump the version in
Cargo.tomlto the next version (e.g.,0.1.1) to allow dry-run checks to verify subsequent commits
With assertVersions = true (module) or assertVersions = "0.1.0" (function), the effect verifies that all workspace package versions match the tag name before publishing.
Rate Limits
When publishing a workspace with many crates for the first time, you may hit crates.io rate limits. The registry limits new crate registrations more strictly than updates to existing crates. At the time of writing, crates.io allows a burst of about 5 new packages, then requires a wait of approximately 10 minutes before publishing more.
If you encounter rate limit errors during an initial release:
-
Wait for the rate limit to reset (approximately 10 minutes)
-
Use
extraPublishArgsto publish remaining crates individually:hercules-ci.cargo-publish = { enable = true; secretName = "crates-io"; extraPublishArgs = [ "--package" "my-unpublished-crate" ]; };nix -
Run locally with
hci effect runto iterate through remaining crates:hci effect run onPush.default.effects.cargo-publish --pretend-ref refs/tags/0.1.0shell
After the initial release, subsequent version updates are less likely to hit rate limits.
See also
-
cargoPublishfunction reference for more options -
hercules-ci.cargo-publishoptions reference -
Getting Started with flake-parts