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