2019-01-26 16:06:25 +01:00
|
|
|
{ lib, fetchgit, fetchzip }:
|
|
|
|
|
|
|
|
{ owner, repo, rev, name ? "source"
|
2020-12-21 13:17:01 +01:00
|
|
|
, fetchSubmodules ? false, leaveDotGit ? null
|
2021-09-26 15:41:34 +02:00
|
|
|
, deepClone ? false, private ? false, forceFetchGit ? false
|
2019-01-26 16:06:25 +01:00
|
|
|
, githubBase ? "github.com", varPrefix ? null
|
|
|
|
, ... # For hash agility
|
2020-12-21 13:17:01 +01:00
|
|
|
}@args:
|
2019-01-26 16:06:25 +01:00
|
|
|
let
|
|
|
|
baseUrl = "https://${githubBase}/${owner}/${repo}";
|
|
|
|
passthruAttrs = removeAttrs args [ "owner" "repo" "rev" "fetchSubmodules" "private" "githubBase" "varPrefix" ];
|
|
|
|
varBase = "NIX${if varPrefix == null then "" else "_${varPrefix}"}_GITHUB_PRIVATE_";
|
2021-09-26 15:41:34 +02:00
|
|
|
useFetchGit = fetchSubmodules || (leaveDotGit == true) || deepClone || forceFetchGit;
|
2019-01-26 16:06:25 +01:00
|
|
|
# We prefer fetchzip in cases we don't need submodules as the hash
|
|
|
|
# is more stable in that case.
|
2020-12-21 13:17:01 +01:00
|
|
|
fetcher = if useFetchGit then fetchgit else fetchzip;
|
2019-01-26 16:06:25 +01: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" ];
|
|
|
|
};
|
2020-12-21 13:17:01 +01:00
|
|
|
fetcherArgs = (if useFetchGit
|
|
|
|
then {
|
|
|
|
inherit rev deepClone fetchSubmodules; url = "${baseUrl}.git";
|
|
|
|
} // lib.optionalAttrs (leaveDotGit != null) { inherit leaveDotGit; }
|
2021-09-15 16:17:30 +02:00
|
|
|
else { url = "${baseUrl}/archive/${rev}.tar.gz"; }
|
|
|
|
) // privateAttrs // passthruAttrs // { inherit name; };
|
2020-12-21 13:17:01 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
fetcher fetcherArgs // { meta.homepage = baseUrl; inherit rev; }
|