@@ -203,11 +203,38 @@ with pkgs;
203203
204204 fetchCrate = callPackage ../build-support/rust/fetchcrate.nix { };
205205
206− fetchFromGitHub = { owner, repo, rev, sha256 }: fetchzip {
207− name = "source";
208− url = "https://github.com/${owner}/${repo}/archive/${rev}.zip";
209− inherit sha256;
210− };
206+ fetchFromGitHub = {
207+ owner, repo, rev, name ? "source",
208+ fetchSubmodules ? false, private ? false,
209+ githubBase ? "github.com", varPrefix ? null,
210+ ... # For hash agility
211+ }@args: assert private -> !fetchSubmodules;
212+ let
213+ baseUrl = "https://${githubBase}/${owner}/${repo}";
214+ passthruAttrs = removeAttrs args [ "owner" "repo" "rev" "fetchSubmodules" "private" "githubBase" "varPrefix" ];
215+ varBase = "NIX${if varPrefix == null then "" else "_${varPrefix}"}_GITHUB_PRIVATE_";
216+ # We prefer fetchzip in cases we don't need submodules as the hash
217+ # is more stable in that case.
218+ fetcher = if fetchSubmodules then fetchgit else fetchzip;
219+ privateAttrs = lib.optionalAttrs private {
220+ netrcPhase = ''
221+ if [ -z "''$${varBase}USERNAME" -o -z "''$${varBase}PASSWORD" ]; then
222+ 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
223+ exit 1
224+ fi
225+ cat > netrc <<EOF
226+ machine ${githubBase}
227+ login ''$${varBase}USERNAME
228+ password ''$${varBase}PASSWORD
229+ EOF
230+ '';
231+ netrcImpureEnvVars = [ "${varBase}USERNAME" "${varBase}PASSWORD" ];
232+ };
233+ fetcherArgs = (if fetchSubmodules
234+ then { inherit rev fetchSubmodules; url = "${baseUrl}.git"; }
235+ else ({ url = "${baseUrl}/archive/${rev}.tar.gz"; } // privateAttrs)
236+ ) // passthruAttrs // { inherit name; };
237+ in fetcher fetcherArgs // { meta.homepage = baseUrl; inherit rev; };
211238
212239 fetchFromBitbucket = {
213240 owner, repo, rev, name ? "source",