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.
https://github.com/NixOS/nixpkgs/pull/321484 introduced a bug when when
`forceFetchGit=true`.
This PR revert the change only for this case.
The previous PR made work cases like:
```bash
nix-build -E 'let pkgs = import ./nixpkgs {}; in pkgs.fetchFromGitHub {
owner = "<username>"; repo = "<repo>"; rev = "HEAD"; hash = "";
private=true; forceFetchGit=false; }'
```
You should see the download working and nix complaining about hash
mismatch.
Now if you take the case where `forceFetchGit=true`, this is a different
story:
```
nix-build -E 'let pkgs = import ./nixpkgs {}; in pkgs.fetchFromGitHub {
owner = "<username>"; repo = "<repo>"; rev = "HEAD"; hash = "";
private=true; forceFetchGit=true; }'
```
Logs look like:
```
Initialized empty Git repository in /nix/store/<hash>-source/.git/
fatal: could not read Username for 'https://github.com': No such device or address
fatal: could not read Username for 'https://github.com': No such device or address
fatal: could not read Username for 'https://github.com': No such device or address
fatal: could not read Username for 'https://github.com': No such device or address
Unable to checkout HEAD from https://github.com/<username>/<repo>.git.
```
I was using `forceFetchGit` for the exact reason the previous PR was
made, I will move to `forceFetchGit=false` now.
Still the previous PR was a broke usage of `fetchFromGitHub` force private repository when `forceFetchGit` is used.
GitHub currently has two kinds of personal access token: "classic" and
"fine-grained". Fine-grained personal access tokens, as the name
suggests, allow much more control over what the token can and cannot do,
and in particular allow users to specify which repositories the token
should provide access to.
Unfortunately, fine-grained tokens don't allow access to repository
archive tarballs for private repositories at (say)
https://github.com/me-and/private-demo/archive/HEAD.tar.gz.
Fortunately, the GitHub API endpoint does provide this access, and also
works with classic tokens and -- for public repositories -- no token at
all.
To allow folk to use fine-grained access tokens, use the GitHub API for
accessing private repos. Keep using the existing interface for
non-private repos, as we can only assume an authenticated user for
private repos, and unauthenticated users have restrictive rate limits on
the API interface.
Fixes#321481
It's become a common pattern to use `rev = "refs/tags/${version}"` rather than
just `rev = version` to ensure that the tag gets fetched rather than a branch
that has the same name. This has so far been done using boilerplate though, so
let's add a simple abstraction to fetch a tag instead.
Without the change metadata evaluation fails on package like
`zammad.src` where no fields are defined in `.nix `files:
src = fetchFromGitHub (lib.importJSON ./source.json);
There evaluation fails as:
$ nix-instantiate --strict --eval --expr 'with import ./. {}; zammad.src.meta'
error:
23| # to indicate where derivation originates, similar to make-derivation.nix's mkDerivation
24| position = "${position.file}:${toString position.line}";
| ^
25| };
error: value is null while a set was expected
After the change evaluation succeeds as:
$ nix-instantiate --strict --eval --expr 'with import ./. {}; zammad.src.meta'
{ homepage = "https://github.com/zammad/zammad"; }
Passing a (multi-line) string was deprecated in #200082 in favour of
list of strings, but still supported (with warning). Now, enforce use of
list of strings.