1 { lib, fetchgit, fetchzip }:
4 { owner, repo, rev, name ? "source"
5 , fetchSubmodules ? false, leaveDotGit ? null
6 , deepClone ? false, private ? false, forceFetchGit ? false
8 , githubBase ? "github.com", varPrefix ? null
10 , ... # For hash agility
15 position = (if args.meta.description or null != null
16 then builtins.unsafeGetAttrPos "description" args.meta
17 else builtins.unsafeGetAttrPos "rev" args
19 baseUrl = "https://${githubBase}/${owner}/${repo}";
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}";
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.
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; }
36 privateAttrs = lib.optionalAttrs private {
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
44 login ''$${varBase}USERNAME
45 password ''$${varBase}PASSWORD
48 netrcImpureEnvVars = [ "${varBase}USERNAME" "${varBase}PASSWORD" ];
51 gitRepoUrl = "${baseUrl}.git";
53 fetcherArgs = (if useFetchGit
55 inherit rev deepClone fetchSubmodules sparseCheckout; url = gitRepoUrl;
56 } // lib.optionalAttrs (leaveDotGit != null) { inherit leaveDotGit; }
58 url = "${baseUrl}/archive/${rev}.tar.gz";
64 ) // privateAttrs // passthruAttrs // { inherit name; };
67 fetcher fetcherArgs // { meta = newMeta; inherit rev owner repo; }