simplify git shortcut + add versionField

This commit is contained in:
DavHau 2021-10-03 18:52:26 +07:00
parent 2ea153f320
commit a3c542fa6d
5 changed files with 71 additions and 8 deletions

View File

@ -35,6 +35,19 @@ rec {
fetchViaShortcut = shortcut:
let
checkArgs = fetcherName: args:
let
fetcher = fetchers."${fetcherName}";
unknownArgNames = lib.filter (argName: ! lib.elem argName fetcher.inputs) (lib.attrNames args);
missingArgNames = lib.filter (inputName: ! args ? "${inputName}") fetcher.inputs;
in
if lib.length unknownArgNames > 0 then
throw "Received unknown arguments for fetcher '${fetcherName}': ${b.toString unknownArgNames}"
else if lib.length missingArgNames > 0 then
throw "Missing arguments for fetcher '${fetcherName}': ${b.toString missingArgNames}"
else
args;
fetchViaHttpUrl =
let
fetcher = fetchers.fetchurl;
@ -60,7 +73,7 @@ rec {
));
fetcher = fetchers.git;
args = params // { inherit url; };
fetcherOutputs = fetcher.outputs args;
fetcherOutputs = fetcher.outputs (checkArgs "git" args);
in
rec {
hash = fetcherOutputs.calcHash "sha256";

View File

@ -17,16 +17,28 @@ in
"rev"
];
outputs = { url, ref ? null, rev ? null, ... }@inp:
if ref == null && rev == null then
throw "At least 'rev' or 'ref' must be specified for fetcher 'git'"
else if rev != null && ! (builtins.match "[a-f0-9]*" rev) then
throw "Argument 'rev' for fetcher git must be a sha1 hash. Try using 'ref' instead"
else if ref != null && b.match "refs/(heads|tags)/.*" ref == null then
throw ''ref must be of format "refs/heads/branch-name" or "refs/tags/tag-name"''
versionField = "rev";
outputs = { url, rev }@inp:
if b.match "refs/(heads|tags)/.*" rev == null && builtins.match "[a-f0-9]*" rev == null then
throw ''rev must either be a sha1 hash or "refs/heads/branch-name" or "refs/tags/tag-name"''
else
let
b = builtins;
ref =
if b.match "refs/(heads|tags)/.*" inp.rev != null then
inp.rev
else
null;
rev =
if b.match "refs/(heads|tags)/.*" inp.rev != null then
null
else
inp.rev;
refAndRev =
(lib.optionalAttrs (ref != null) {
inherit ref;
@ -35,6 +47,7 @@ in
(lib.optionalAttrs (rev != null) {
inherit rev;
});
in
{

View File

@ -17,6 +17,8 @@
"rev"
];
versionField = "rev";
outputs = { owner, repo, rev, ... }@inp:
let
b = builtins;

View File

@ -14,6 +14,8 @@
"rev"
];
versionField = "rev";
outputs = { owner, repo, rev, ... }@inp:
let
b = builtins;

View File

@ -0,0 +1,33 @@
{
python3,
utils,
# config
allowBuiltinFetchers,
...
}:
{
inputs = [
"pname"
"version"
];
versionField = "version";
outputs = { pname, version, extension ? "tar.gz", ... }@inp:
let
b = builtins;
in
{
calcHash = algo: utils.hashPath algo (b.fetchurl {
url = "https://files.pythonhosted.org/packages/${builtins.substring 0 1 pname}/${pname}/${pname}-${version}.${extension}";
});
fetched = hash:
python3.pkgs.fetchPypi {
inherit pname version extension hash;
};
};
}