···
# Tested in lib/tests/sources.sh
6
+
inherit (lib.strings)
···
406
+
# urlToName : (URL | Path | String) -> String
408
+
# Transform a URL (or path, or string) into a clean package name.
412
+
inherit (lib.strings) stringLength;
413
+
base = baseNameOf (lib.removeSuffix "/" (lib.last (lib.splitString ":" (toString url))));
414
+
# chop away one git or archive-related extension
418
+
matchExt = match "(.*)\\.(git|tar|zip|gz|tgz|bz|tbz|bz2|tbz2|lzma|txz|xz|zstd)$" name;
420
+
if matchExt != null then lib.head matchExt else name;
421
+
# apply function f to string x while the result shrinks
427
+
if stringLength v < stringLength x then shrink f v else x;
429
+
shrink removeExt base;
431
+
# shortRev : (String | Integer) -> String
433
+
# Given a package revision (like "refs/tags/v12.0"), produce a short revision ("12.0").
437
+
baseRev = baseNameOf (toString rev);
438
+
matchHash = match "[a-f0-9]+" baseRev;
439
+
matchVer = match "([A-Za-z]+[-_. ]?)*(v)?([0-9.]+.*)" baseRev;
441
+
if matchHash != null then
442
+
builtins.substring 0 7 baseRev
443
+
else if matchVer != null then
448
+
# revOrTag : String -> String -> String
450
+
# Turn git `rev` and `tag` pair into a revision usable in `repoRevToName*`.
453
+
if tag != null then
455
+
else if rev != null then
460
+
# repoRevToNameFull : (URL | Path | String) -> (String | Integer | null) -> (String | null) -> String
462
+
# See `repoRevToName` below.
463
+
repoRevToNameFull =
464
+
repo_: rev_: suffix_:
466
+
repo = urlToName repo_;
467
+
rev = if rev_ != null then "-${shortRev rev_}" else "";
468
+
suffix = if suffix_ != null then "-${suffix_}" else "";
470
+
"${repo}${rev}${suffix}-source";
472
+
# repoRevToName : String -> (URL | Path | String) -> (String | Integer | null) -> String -> String
474
+
# Produce derivation.name attribute for a given repository URL/path/name and (optionally) its revision/version tag.
476
+
# This is used by fetch(zip|git|FromGitHub|hg|svn|etc) to generate discoverable
477
+
# /nix/store paths.
479
+
# This uses a different implementation depending on the `pretty` argument:
480
+
# "source" -> name everything as "source"
481
+
# "versioned" -> name everything as "${repo}-${rev}-source"
482
+
# "full" -> name everything as "${repo}-${rev}-${fetcher}-source"
485
+
# match on `kind` first to minimize the thunk
486
+
if kind == "source" then
491
+
else if kind == "versioned" then
494
+
repoRevToNameFull repo rev null
496
+
else if kind == "full" then
499
+
throw "repoRevToName: invalid kind";
···