1{ lib, fetchgit, fetchzip }:
2
3lib.makeOverridable (
4{ owner, repo, rev, name ? "source"
5, fetchSubmodules ? false, leaveDotGit ? null
6, deepClone ? false, private ? false, forceFetchGit ? false
7, sparseCheckout ? []
8, githubBase ? "github.com", varPrefix ? null
9, meta ? { }
10, ... # For hash agility
11}@args:
12
13let
14
15 position = (if args.meta.description or null != null
16 then builtins.unsafeGetAttrPos "description" args.meta
17 else builtins.unsafeGetAttrPos "rev" args
18 );
19 baseUrl = "https://${githubBase}/${owner}/${repo}";
20 newMeta = meta // {
21 homepage = meta.homepage or baseUrl;
22 } // lib.optionalAttrs (position != null) {
23 # to indicate where derivation originates, similar to make-derivation.nix's mkDerivation
24 position = "${position.file}:${toString position.line}";
25 };
26 passthruAttrs = removeAttrs args [ "owner" "repo" "rev" "fetchSubmodules" "forceFetchGit" "private" "githubBase" "varPrefix" ];
27 varBase = "NIX${lib.optionalString (varPrefix != null) "_${varPrefix}"}_GITHUB_PRIVATE_";
28 useFetchGit = fetchSubmodules || (leaveDotGit == true) || deepClone || forceFetchGit || (sparseCheckout != []);
29 # We prefer fetchzip in cases we don't need submodules as the hash
30 # is more stable in that case.
31 fetcher =
32 if useFetchGit then fetchgit
33 # fetchzip may not be overridable when using external tools, for example nix-prefetch
34 else if fetchzip ? override then fetchzip.override { withUnzip = false; }
35 else fetchzip;
36 privateAttrs = lib.optionalAttrs private {
37 netrcPhase = ''
38 if [ -z "''$${varBase}USERNAME" -o -z "''$${varBase}PASSWORD" ]; then
39 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
40 exit 1
41 fi
42 cat > netrc <<EOF
43 machine ${githubBase}
44 login ''$${varBase}USERNAME
45 password ''$${varBase}PASSWORD
46 EOF
47 '';
48 netrcImpureEnvVars = [ "${varBase}USERNAME" "${varBase}PASSWORD" ];
49 };
50
51 gitRepoUrl = "${baseUrl}.git";
52
53 fetcherArgs = (if useFetchGit
54 then {
55 inherit rev deepClone fetchSubmodules sparseCheckout; url = gitRepoUrl;
56 } // lib.optionalAttrs (leaveDotGit != null) { inherit leaveDotGit; }
57 else {
58 url = "${baseUrl}/archive/${rev}.tar.gz";
59
60 passthru = {
61 inherit gitRepoUrl;
62 };
63 }
64 ) // privateAttrs // passthruAttrs // { inherit name; };
65in
66
67fetcher fetcherArgs // { meta = newMeta; inherit rev owner repo; }
68)