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 |
nix build -f Cargo.nix rootCrate.docWithDependenciesxdg-open result/share/doc/my_crate/index.htmlIn 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" ];}Output layout
Section titled “Output layout”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/Semantics
Section titled “Semantics”- Only crates with a library target have API docs. For a binary-only crate,
docdocuments the binary, likecargo docdoes. rustdocis called with the same edition, features,--cfgflags, environment and--externflags as the library build. Build scripts run as for the normal build (in fact, their output is reused), so crates that generate code intoOUT_DIRcan be documented.docWithDependenciesdocuments 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 therustdoccommand line.RUSTDOCFLAGSfrom.cargo/config.toml(build.rustdocflags) are applied, too.rustdocmust come from the same toolchain asrustc. 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), butrustdocruns on the build platform. No binaries are executed. - JSON mode:
lib/build-from-json.nixgets the same attributes.
Open questions / alternatives
Section titled “Open questions / alternatives”- Caching:
docWithDependenciesrebuilds the docs of all dependencies when any of them changes. rustdoc’s unstable--merge/--parts-out-diroptions would allow one cached derivation per crate whose outputs are merged cheaply, but they need nightly orRUSTC_BOOTSTRAP. - Should
doclive on the crate derivation (build.doc, a passthru attribute) instead of next tobuild? - Should there be an
allWorkspaceMembers.docthat documents all members in one tree, ascargo doc --workspacedoes? That’s probably the most common use case for publishing docs. - Implementation:
buildRustCratedoesn’t support rustdoc. Either it gets abuildDocsflag in nixpkgs (similar tobuildTests), or crate2nix runsrustdocin its own derivation and takes the build script output from the library build.