1 { lib, fetchgit, fetchzip }:
4 { owner, repo, rev, name ? "source"
5 , fetchSubmodules ? false, leaveDotGit ? null
6 , deepClone ? false, private ? false, forceFetchGit ? false
9 , githubBase ? "github.com", varPrefix ? null
11 , ... # For hash agility
16 position = (if args.meta.description or null != null
17 then builtins.unsafeGetAttrPos "description" args.meta
18 else builtins.unsafeGetAttrPos "rev" args
20 baseUrl = "https://${githubBase}/${owner}/${repo}";
22 homepage = meta.homepage or baseUrl;
23 } // lib.optionalAttrs (position != null) {
24 # to indicate where derivation originates, similar to make-derivation.nix's mkDerivation
25 position = "${position.file}:${toString position.line}";
27 passthruAttrs = removeAttrs args [ "owner" "repo" "rev" "fetchSubmodules" "forceFetchGit" "private" "githubBase" "varPrefix" ];
28 varBase = "NIX${lib.optionalString (varPrefix != null) "_${varPrefix}"}_GITHUB_PRIVATE_";
29 useFetchGit = fetchSubmodules || (leaveDotGit == true) || deepClone || forceFetchGit || fetchLFS || (sparseCheckout != []);
30 # We prefer fetchzip in cases we don't need submodules as the hash
31 # is more stable in that case.
33 if useFetchGit then fetchgit
34 # fetchzip may not be overridable when using external tools, for example nix-prefetch
35 else if fetchzip ? override then fetchzip.override { withUnzip = false; }
37 privateAttrs = lib.optionalAttrs private {
39 if [ -z "''$${varBase}USERNAME" -o -z "''$${varBase}PASSWORD" ]; then
40 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
45 login ''$${varBase}USERNAME
46 password ''$${varBase}PASSWORD
49 netrcImpureEnvVars = [ "${varBase}USERNAME" "${varBase}PASSWORD" ];
52 gitRepoUrl = "${baseUrl}.git";
54 fetcherArgs = (if useFetchGit
56 inherit rev deepClone fetchSubmodules sparseCheckout fetchLFS; url = gitRepoUrl;
57 } // lib.optionalAttrs (leaveDotGit != null) { inherit leaveDotGit; }
59 url = "${baseUrl}/archive/${rev}.tar.gz";
65 ) // privateAttrs // passthruAttrs // { inherit name; };
68 fetcher fetcherArgs // { meta = newMeta; inherit rev owner repo; }