Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## Unreleased

### Added
* `craneLib.buildPackage` and `craneLib.buildTrunkPackage` now populate the
derivation's `meta.description` from the crate's `package.description` (or
`workspace.package.description`) in its root Cargo.toml, unless
`meta.description` is explicitly set. `crateNameFromCargoToml`
correspondingly exposes a `description` attribute (omitted when absent).

## [0.24.0] - 2026-08-21

### Changed
Expand Down
11 changes: 9 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ raised during evaluation.

`crateNameFromCargoToml :: set -> set`

Extract a crate's name and version from its Cargo.toml file.
Extract a crate's name, version, and description from its Cargo.toml file.

The resulting `pname` attribute will be populated with the value of the
Cargo.toml's (top-level) attributes in the following order, where the first
Expand All @@ -916,14 +916,21 @@ attribute (with a string value) will be chosen:
1. `workspace.package.version`
1. Otherwise a placeholder version will be used

The resulting `description` attribute will be populated from
`package.description` (or `workspace.package.description`), and simply omitted
when neither is present (there is no placeholder fallback).

Note that *only the root `Cargo.toml` of the specified source will be checked*.
Directories **will not be crawled** to resolve potential workspace inheritance.

```nix
craneLib.crateNameFromCargoToml { cargoToml = ./Cargo.toml; }
# { pname = "simple"; version = "0.1.0"; }
# { pname = "simple"; version = "0.1.0"; description = "..."; }
```

`craneLib.buildPackage` and `craneLib.buildTrunkPackage` also populate
`meta.description` from this value unless explicitly set.

### `craneLib.crateRegistries`

`crateRegistries :: set`
Expand Down
4 changes: 4 additions & 0 deletions lib/buildPackage.nix
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ mkCargoDerivation (
fi
'';

meta =
lib.optionalAttrs (crateName ? description) { inherit (crateName) description; }
// (args.meta or { });

nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [
# NB: avoid adding any non-hook packages here. Doing so will end up
# changing PKG_CONFIG_PATH and cause rebuilds of `*-sys` crates.
Expand Down
4 changes: 4 additions & 0 deletions lib/buildTrunkPackage.nix
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ mkCargoDerivation (
cp -r "$(dirname "${trunkIndexPath}")/dist" $out
'';

meta =
lib.optionalAttrs (crateName ? description) { inherit (crateName) description; }
// (args.meta or { });

# Installing artifacts on a distributable dir does not make much sense
doInstallCargoArtifacts = args.doInstallCargoArtifacts or false;

Expand Down
3 changes: 2 additions & 1 deletion lib/crateNameFromCargoToml.nix
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ let

internalName = internalCrateNameFromCargoToml toml debugPath;
in
{
lib.filterAttrs (_: v: v != null) {
pname =
internalName.pname or (traceMsg "name" "pname" "cargo-package" [
''`package.metadata.crane.name` = "..."''
Expand All @@ -69,4 +69,5 @@ in
internalName.version or (traceMsg "version" "version" "0.0.1" [
''`workspace.package.version` = "..."''
]);
description = internalName.description or null;
}
4 changes: 4 additions & 0 deletions lib/internalCrateNameFromCargoToml.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ lib.filterAttrs (_: v: v != null) {
(toml.package.version or null)
(toml.workspace.package.version or null)
];
description = firstNonNull [
(toml.package.description or null)
(toml.workspace.package.description or null)
];
}