crane/lib/crateNameFromCargoToml.nix
Ivan Petkov 2a14c2d53b
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
2021-12-30 15:08:45 -08:00

29 lines
631 B
Nix

{ fromTOML }:
{ src ? null
, cargoToml ? src + /Cargo.toml
, cargoTomlContents ? null
, ...
}:
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;
}