diff --git a/CHANGELOG.md b/CHANGELOG.md index f3dcc864..af6af986 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/API.md b/docs/API.md index 06acaa8e..93bdc884 100644 --- a/docs/API.md +++ b/docs/API.md @@ -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 @@ -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` diff --git a/lib/buildPackage.nix b/lib/buildPackage.nix index c263135d..7c232043 100644 --- a/lib/buildPackage.nix +++ b/lib/buildPackage.nix @@ -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. diff --git a/lib/buildTrunkPackage.nix b/lib/buildTrunkPackage.nix index c88dca6f..d1ec3ee8 100644 --- a/lib/buildTrunkPackage.nix +++ b/lib/buildTrunkPackage.nix @@ -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; diff --git a/lib/crateNameFromCargoToml.nix b/lib/crateNameFromCargoToml.nix index abc6351e..5ea305a9 100644 --- a/lib/crateNameFromCargoToml.nix +++ b/lib/crateNameFromCargoToml.nix @@ -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` = "..."'' @@ -69,4 +69,5 @@ in internalName.version or (traceMsg "version" "version" "0.0.1" [ ''`workspace.package.version` = "..."'' ]); + description = internalName.description or null; } diff --git a/lib/internalCrateNameFromCargoToml.nix b/lib/internalCrateNameFromCargoToml.nix index 4c1ee64a..7588aa4b 100644 --- a/lib/internalCrateNameFromCargoToml.nix +++ b/lib/internalCrateNameFromCargoToml.nix @@ -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) + ]; }