Update crateNameFromCargoToml with better fallback error messages

* If a derivation is created without a name *and* we cannot infer a name
  from a Cargo.toml file, we'll throw a descriptive error message which
  hints towards the remediation
* Otherwise nix can throw a pretty obtuse "derivation has no name" error
  with no error trace
This commit is contained in:
Ivan Petkov 2021-12-30 15:08:45 -08:00
parent 0aadea3b08
commit 2a14c2d53b
No known key found for this signature in database
GPG Key ID: BB6F9EFC065832B6

View File

@ -2,15 +2,27 @@
{ src ? null
, cargoToml ? src + /Cargo.toml
, cargoTomlContents ? builtins.readFile cargoToml
, cargoTomlContents ? null
, ...
}:
let
toml = fromTOML cargoTomlContents;
p = toml.package;
in
{
inherit (p) version;
pname = p.name;
}
if cargoTomlContents == null && (src == null || !builtins.pathExists cargoToml)
then
throw ''
unable to infer crate name and version. Please make sure the src directory
contains a valid Cargo.toml file, or consider setting a derivation name explicitly
''
else
let
cargoTomlRealContents =
if cargoTomlContents != null
then cargoTomlContents
else builtins.readFile cargoToml;
toml = fromTOML cargoTomlRealContents;
p = toml.package;
in
{
inherit (p) version;
pname = p.name;
}