Skip to content

API documentation (rustdoc)

rootCrate and every entry of workspaceMembers have two attributes that build HTML API documentation with rustdoc:

Attribute Cargo equivalent Content
doc cargo doc --no-deps the docs of the crate only
docWithDependencies cargo doc the docs of the crate and all its normal dependencies
Terminal window
nix build -f Cargo.nix rootCrate.docWithDependencies
xdg-open result/share/doc/my_crate/index.html

In a flake, e.g. to publish the docs of a library:

{
packages.docs = cargoNix.workspaceMembers.my_lib.docWithDependencies;
}

Both are overridable with the same arguments as build:

cargoNix.rootCrate.doc.override {
features = [ "default" "serde" ];
extraRustdocOpts = [ "--document-private-items" ];
}

The output looks like cargo’s target/doc: one directory per crate, plus the shared static files, search-index.js and the source pages, under share/doc:

result/share/doc/
├── index.html -> redirect to my_crate/index.html
├── my_crate/
├── serde/ (docWithDependencies only)
├── search-index.js
├── src/
└── static.files/
  • Only crates with a library target have API docs. For a binary-only crate, doc documents the binary, like cargo doc does.
  • rustdoc is called with the same edition, features, --cfg flags, environment and --extern flags as the library build. Build scripts run as for the normal build (in fact, their output is reused), so crates that generate code into OUT_DIR can be documented.
  • docWithDependencies documents the crate and its transitive normal dependencies (not build or dev dependencies) in a single derivation, in dependency order, so that links between the crates work and the search index covers all of them. Proc-macro crates are included.
  • [package.metadata.docs.rs] is ignored.
  • extraRustdocOpts (a crate attribute, so it can also be set in crate overrides) is appended to the rustdoc command line. RUSTDOCFLAGS from .cargo/config.toml (build.rustdocflags) are applied, too.
  • rustdoc must come from the same toolchain as rustc. It does for nixpkgs’ rustc, rust-overlay and fenix toolchains.
  • Cross-compilation: the docs are generated for the host platform (so cfg(target_os = ...) items match the target), but rustdoc runs on the build platform. No binaries are executed.
  • JSON mode: lib/build-from-json.nix gets the same attributes.
  • Caching: docWithDependencies rebuilds the docs of all dependencies when any of them changes. rustdoc’s unstable --merge/--parts-out-dir options would allow one cached derivation per crate whose outputs are merged cheaply, but they need nightly or RUSTC_BOOTSTRAP.
  • Should doc live on the crate derivation (build.doc, a passthru attribute) instead of next to build?
  • Should there be an allWorkspaceMembers.doc that documents all members in one tree, as cargo doc --workspace does? That’s probably the most common use case for publishing docs.
  • Implementation: buildRustCrate doesn’t support rustdoc. Either it gets a buildDocs flag in nixpkgs (similar to buildTests), or crate2nix runs rustdoc in its own derivation and takes the build script output from the library build.