0
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-07-14 06:00:33 +03:00

lib, treewide: introduce repoRevToName, use it in most fetch* functions

This patch adds `lib.repoRevToName` function that generalizes away most of the
code used for derivation name generation by `fetch*` functions (`fetchzip`,
`fetchFromGitHub`, etc, except those which are delayed until latter commits
for mass-rebuild reasons).

It's first argument controls how the resulting name will look (see below).

Since `lib` has no equivalent of Nixpkgs' `config`, this patch adds
`config.fetchedSourceNameDefault` option to Nixpkgs and then re-exposes
`lib.repoRevToName config.fetchedSourceNameDefault` expression as
`pkgs.repoRevToNameMaybe` which is then used in `fetch*` derivations.

The result is that different values of `config.fetchedSourceNameDefault` now
control how the `src` derivations produced by `fetch*` functions are to be
named, e.g.:

- `fetchedSourceNameDefault = "source"` (the default):

  ```
  $ nix-instantiate -A fuse.src
  /nix/store/<hash>-source.drv
  ```

- `fetchedSourceNameDefault = "versioned"`:

  ```
  $ nix-instantiate -A fuse.src
  /nix/store/<hash>-libfuse-2.9.9-source.drv
  ```

- `fetchedSourceNameDefault = "full"`:

  ```
  $ nix-instantiate -A fuse.src
  /nix/store/<hash>-libfuse-2.9.9-github-source.drv
  ```

See the documentation of `config.fetchedSourceNameDefault` for more info.
This commit is contained in:
Jan Malakhovski 2025-03-19 14:25:00 +00:00
parent 27b01599c5
commit 7c8a4efb68
13 changed files with 193 additions and 26 deletions

View file

@ -3,7 +3,7 @@
# Tested in lib/tests/sources.sh
let
inherit (builtins)
inherit (lib.strings)
match
split
storeDir
@ -403,6 +403,101 @@ let
};
};
# urlToName : (URL | Path | String) -> String
#
# Transform a URL (or path, or string) into a clean package name.
urlToName =
url:
let
inherit (lib.strings) stringLength;
base = baseNameOf (lib.removeSuffix "/" (lib.last (lib.splitString ":" (toString url))));
# chop away one git or archive-related extension
removeExt =
name:
let
matchExt = match "(.*)\\.(git|tar|zip|gz|tgz|bz|tbz|bz2|tbz2|lzma|txz|xz|zstd)$" name;
in
if matchExt != null then lib.head matchExt else name;
# apply function f to string x while the result shrinks
shrink =
f: x:
let
v = f x;
in
if stringLength v < stringLength x then shrink f v else x;
in
shrink removeExt base;
# shortRev : (String | Integer) -> String
#
# Given a package revision (like "refs/tags/v12.0"), produce a short revision ("12.0").
shortRev =
rev:
let
baseRev = baseNameOf (toString rev);
matchHash = match "[a-f0-9]+" baseRev;
matchVer = match "([A-Za-z]+[-_. ]?)*(v)?([0-9.]+.*)" baseRev;
in
if matchHash != null then
builtins.substring 0 7 baseRev
else if matchVer != null then
lib.last matchVer
else
baseRev;
# revOrTag : String -> String -> String
#
# Turn git `rev` and `tag` pair into a revision usable in `repoRevToName*`.
revOrTag =
rev: tag:
if tag != null then
tag
else if rev != null then
rev
else
"HEAD";
# repoRevToNameFull : (URL | Path | String) -> (String | Integer | null) -> (String | null) -> String
#
# See `repoRevToName` below.
repoRevToNameFull =
repo_: rev_: suffix_:
let
repo = urlToName repo_;
rev = if rev_ != null then "-${shortRev rev_}" else "";
suffix = if suffix_ != null then "-${suffix_}" else "";
in
"${repo}${rev}${suffix}-source";
# repoRevToName : String -> (URL | Path | String) -> (String | Integer | null) -> String -> String
#
# Produce derivation.name attribute for a given repository URL/path/name and (optionally) its revision/version tag.
#
# This is used by fetch(zip|git|FromGitHub|hg|svn|etc) to generate discoverable
# /nix/store paths.
#
# This uses a different implementation depending on the `pretty` argument:
# "source" -> name everything as "source"
# "versioned" -> name everything as "${repo}-${rev}-source"
# "full" -> name everything as "${repo}-${rev}-${fetcher}-source"
repoRevToName =
kind:
# match on `kind` first to minimize the thunk
if kind == "source" then
(
repo: rev: suffix:
"source"
)
else if kind == "versioned" then
(
repo: rev: suffix:
repoRevToNameFull repo rev null
)
else if kind == "full" then
repoRevToNameFull
else
throw "repoRevToName: invalid kind";
in
{
@ -431,6 +526,11 @@ in
pathHasContext
canCleanSource
urlToName
shortRev
revOrTag
repoRevToName
sourceByRegex
sourceFilesBySuffices