From afb8fbbbb618068bd2de097127a0a1e049c04abe Mon Sep 17 00:00:00 2001 From: "Julie B." Date: Tue, 21 May 2024 10:52:45 +0200 Subject: [PATCH] fetchFromGitLab: align with fetchFromGitHub --- pkgs/build-support/fetchgitlab/default.nix | 69 ++++++++++++++++------ 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/pkgs/build-support/fetchgitlab/default.nix b/pkgs/build-support/fetchgitlab/default.nix index 749883f..b26e440 100644 --- a/pkgs/build-support/fetchgitlab/default.nix +++ b/pkgs/build-support/fetchgitlab/default.nix @@ -1,36 +1,67 @@ { lib, fetchgit, fetchzip }: lib.makeOverridable ( -# gitlab example -{ owner, repo, rev, protocol ? "https", domain ? "gitlab.com", name ? "source", group ? null -, fetchSubmodules ? false, leaveDotGit ? false +{ owner, repo, rev +, name ? null # Override with null to use the default value +, pname ? lib.concatStringsSep "-" ([ "source" domain ] ++ (lib.optional (group != null) group) ++ [ owner repo ]) +, fetchSubmodules ? false, leaveDotGit ? null , deepClone ? false, forceFetchGit ? false , sparseCheckout ? [] +, protocol ? "https", domain ? "gitlab.com", group ? null +, passthru ? { } +, meta ? { } , ... # For hash agility -} @ args: +}@args: let + name = if args.name or null != null then args.name + else "${pname}-${rev}"; + + position = (if args.meta.description or null != null + then builtins.unsafeGetAttrPos "description" args.meta + else builtins.unsafeGetAttrPos "rev" args + ); + baseUrl = "${protocol}://${domain}/${slug}"; + newPassthru = passthru // { + inherit rev owner repo; + }; + newMeta = meta // { + homepage = meta.homepage or baseUrl; + } // lib.optionalAttrs (position != null) { + # to indicate where derivation originates, similar to make-derivation.nix's mkDerivation + position = "${position.file}:${toString position.line}"; + }; + passthruAttrs = removeAttrs args [ "protocol" "domain" "owner" "group" "repo" "rev" "fetchSubmodules" "forceFetchGit" "leaveDotGit" "deepClone" ]; slug = lib.concatStringsSep "/" ((lib.optional (group != null) group) ++ [ owner repo ]); escapedSlug = lib.replaceStrings [ "." "/" ] [ "%2E" "%2F" ] slug; escapedRev = lib.replaceStrings [ "+" "%" "/" ] [ "%2B" "%25" "%2F" ] rev; - passthruAttrs = removeAttrs args [ "protocol" "domain" "owner" "group" "repo" "rev" "fetchSubmodules" "forceFetchGit" "leaveDotGit" "deepClone" ]; + useFetchGit = fetchSubmodules || (leaveDotGit == true) || deepClone || forceFetchGit || (sparseCheckout != []); + # We prefer fetchzip in cases we don't need submodules as the hash + # is more stable in that case. + fetcher = + if useFetchGit then fetchgit + # fetchzip may not be overridable when using external tools, for example nix-prefetch + else if fetchzip ? override then fetchzip.override { withUnzip = false; } + else fetchzip; - useFetchGit = fetchSubmodules || leaveDotGit || deepClone || forceFetchGit || (sparseCheckout != []); - fetcher = if useFetchGit then fetchgit else fetchzip; + gitRepoUrl = "${baseUrl}.git"; - gitRepoUrl = "${protocol}://${domain}/${slug}.git"; + fetcherArgs = (if useFetchGit + then { + inherit rev deepClone fetchSubmodules sparseCheckout; url = gitRepoUrl; + passthru = newPassthru; + } // lib.optionalAttrs (leaveDotGit != null) { inherit leaveDotGit; } + else { + url = "${protocol}://${domain}/api/v4/projects/${escapedSlug}/repository/archive.tar.gz?sha=${escapedRev}"; - fetcherArgs = (if useFetchGit then { - inherit rev deepClone fetchSubmodules sparseCheckout leaveDotGit; - url = gitRepoUrl; - } else { - url = "${protocol}://${domain}/api/v4/projects/${escapedSlug}/repository/archive.tar.gz?sha=${escapedRev}"; - - passthru = { - inherit gitRepoUrl; - }; - }) // passthruAttrs // { inherit name; }; + passthru = newPassthru // { + inherit gitRepoUrl; + }; + } + ) // passthruAttrs // { inherit name; }; in -fetcher fetcherArgs // { meta.homepage = "${protocol}://${domain}/${slug}/"; inherit rev owner repo; } +(fetcher fetcherArgs).overrideAttrs (finalAttrs: previousAttrs: { + meta = newMeta; +}) )