2020-04-24 23:36:52 +00:00
|
|
|
{ lib, fetchgit, fetchzip }:
|
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
lib.makeOverridable (
|
2020-04-24 23:36:52 +00:00
|
|
|
{ owner, repo, rev, name ? "source"
|
2021-02-22 21:28:39 +00:00
|
|
|
, fetchSubmodules ? false, leaveDotGit ? null
|
2021-10-04 12:37:57 +00:00
|
|
|
, deepClone ? false, private ? false, forceFetchGit ? false
|
2022-11-27 09:42:12 +00:00
|
|
|
, sparseCheckout ? []
|
2020-04-24 23:36:52 +00:00
|
|
|
, githubBase ? "github.com", varPrefix ? null
|
2022-02-10 20:34:41 +00:00
|
|
|
, meta ? { }
|
2020-04-24 23:36:52 +00:00
|
|
|
, ... # For hash agility
|
2021-02-22 21:28:39 +00:00
|
|
|
}@args:
|
2022-02-10 20:34:41 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
let
|
2022-02-10 20:34:41 +00:00
|
|
|
|
|
|
|
position = (if args.meta.description or null != null
|
|
|
|
then builtins.unsafeGetAttrPos "description" args.meta
|
|
|
|
else builtins.unsafeGetAttrPos "rev" args
|
|
|
|
);
|
2020-04-24 23:36:52 +00:00
|
|
|
baseUrl = "https://${githubBase}/${owner}/${repo}";
|
2022-02-10 20:34:41 +00:00
|
|
|
newMeta = meta // {
|
|
|
|
homepage = meta.homepage or baseUrl;
|
2024-01-13 08:15:51 +00:00
|
|
|
} // lib.optionalAttrs (position != null) {
|
2022-02-10 20:34:41 +00:00
|
|
|
# to indicate where derivation originates, similar to make-derivation.nix's mkDerivation
|
|
|
|
position = "${position.file}:${toString position.line}";
|
|
|
|
};
|
2021-12-06 16:07:01 +00:00
|
|
|
passthruAttrs = removeAttrs args [ "owner" "repo" "rev" "fetchSubmodules" "forceFetchGit" "private" "githubBase" "varPrefix" ];
|
2023-08-04 22:07:22 +00:00
|
|
|
varBase = "NIX${lib.optionalString (varPrefix != null) "_${varPrefix}"}_GITHUB_PRIVATE_";
|
2023-07-15 17:15:38 +00:00
|
|
|
useFetchGit = fetchSubmodules || (leaveDotGit == true) || deepClone || forceFetchGit || (sparseCheckout != []);
|
2020-04-24 23:36:52 +00:00
|
|
|
# We prefer fetchzip in cases we don't need submodules as the hash
|
|
|
|
# is more stable in that case.
|
2024-02-07 01:22:34 +00:00
|
|
|
fetcher =
|
|
|
|
if useFetchGit then fetchgit
|
|
|
|
# fetchzip may not be overridable when using external tools, for example nix-prefetch
|
|
|
|
else if fetchzip ? override then fetchzip.override { withUnzip = false; }
|
|
|
|
else fetchzip;
|
2020-04-24 23:36:52 +00:00
|
|
|
privateAttrs = lib.optionalAttrs private {
|
|
|
|
netrcPhase = ''
|
|
|
|
if [ -z "''$${varBase}USERNAME" -o -z "''$${varBase}PASSWORD" ]; then
|
|
|
|
echo "Error: Private fetchFromGitHub requires the nix building process (nix-daemon in multi user mode) to have the ${varBase}USERNAME and ${varBase}PASSWORD env vars set." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
cat > netrc <<EOF
|
|
|
|
machine ${githubBase}
|
|
|
|
login ''$${varBase}USERNAME
|
|
|
|
password ''$${varBase}PASSWORD
|
|
|
|
EOF
|
|
|
|
'';
|
|
|
|
netrcImpureEnvVars = [ "${varBase}USERNAME" "${varBase}PASSWORD" ];
|
|
|
|
};
|
2022-06-16 17:23:12 +00:00
|
|
|
|
|
|
|
gitRepoUrl = "${baseUrl}.git";
|
|
|
|
|
2021-02-22 21:28:39 +00:00
|
|
|
fetcherArgs = (if useFetchGit
|
|
|
|
then {
|
2022-06-16 17:23:12 +00:00
|
|
|
inherit rev deepClone fetchSubmodules sparseCheckout; url = gitRepoUrl;
|
2021-02-22 21:28:39 +00:00
|
|
|
} // lib.optionalAttrs (leaveDotGit != null) { inherit leaveDotGit; }
|
2022-06-16 17:23:12 +00:00
|
|
|
else {
|
|
|
|
url = "${baseUrl}/archive/${rev}.tar.gz";
|
|
|
|
|
|
|
|
passthru = {
|
|
|
|
inherit gitRepoUrl;
|
|
|
|
};
|
|
|
|
}
|
2021-10-04 12:37:57 +00:00
|
|
|
) // privateAttrs // passthruAttrs // { inherit name; };
|
2021-02-22 21:28:39 +00:00
|
|
|
in
|
|
|
|
|
2022-12-17 10:02:37 +00:00
|
|
|
fetcher fetcherArgs // { meta = newMeta; inherit rev owner repo; }
|
2023-05-24 13:37:59 +00:00
|
|
|
)
|