1{ lib, fetchgit, fetchzip }:
2
3{ owner, repo, rev, name ? "source"
4, fetchSubmodules ? false, leaveDotGit ? null
5, deepClone ? false, private ? false
6, githubBase ? "github.com", varPrefix ? null
7, ... # For hash agility
8}@args:
9let
10 baseUrl = "https://${githubBase}/${owner}/${repo}";
11 passthruAttrs = removeAttrs args [ "owner" "repo" "rev" "fetchSubmodules" "private" "githubBase" "varPrefix" ];
12 varBase = "NIX${if varPrefix == null then "" else "_${varPrefix}"}_GITHUB_PRIVATE_";
13 useFetchGit = fetchSubmodules || (leaveDotGit == true) || deepClone;
14 # We prefer fetchzip in cases we don't need submodules as the hash
15 # is more stable in that case.
16 fetcher = if useFetchGit then fetchgit else fetchzip;
17 privateAttrs = lib.optionalAttrs private {
18 netrcPhase = ''
19 if [ -z "''$${varBase}USERNAME" -o -z "''$${varBase}PASSWORD" ]; then
20 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
21 exit 1
22 fi
23 cat > netrc <<EOF
24 machine ${githubBase}
25 login ''$${varBase}USERNAME
26 password ''$${varBase}PASSWORD
27 EOF
28 '';
29 netrcImpureEnvVars = [ "${varBase}USERNAME" "${varBase}PASSWORD" ];
30 };
31 fetcherArgs = (if useFetchGit
32 then {
33 inherit rev deepClone fetchSubmodules; url = "${baseUrl}.git";
34 } // lib.optionalAttrs (leaveDotGit != null) { inherit leaveDotGit; }
35 else ({ url = "${baseUrl}/archive/${rev}.tar.gz"; } // privateAttrs)
36 ) // passthruAttrs // { inherit name; };
37in
38
39assert private -> !useFetchGit;
40
41fetcher fetcherArgs // { meta.homepage = baseUrl; inherit rev; }