core/pkgs/build-support/fetchgitlab/default.nix

68 lines
2.5 KiB
Nix
Raw Normal View History

2024-05-02 00:46:19 +00:00
{ lib, fetchgit, fetchzip }:
lib.makeOverridable (
{ 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
2024-05-02 00:46:19 +00:00
, deepClone ? false, forceFetchGit ? false
, sparseCheckout ? []
, protocol ? "https", domain ? "gitlab.com", group ? null
, passthru ? { }
, meta ? { }
2024-05-02 00:46:19 +00:00
, ... # For hash agility
}@args:
2024-05-02 00:46:19 +00:00
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" ];
2024-05-02 00:46:19 +00:00
slug = lib.concatStringsSep "/" ((lib.optional (group != null) group) ++ [ owner repo ]);
escapedSlug = lib.replaceStrings [ "." "/" ] [ "%2E" "%2F" ] slug;
escapedRev = lib.replaceStrings [ "+" "%" "/" ] [ "%2B" "%25" "%2F" ] rev;
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;
2024-05-02 00:46:19 +00:00
gitRepoUrl = "${baseUrl}.git";
2024-05-02 00:46:19 +00:00
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}";
2024-05-02 00:46:19 +00:00
passthru = newPassthru // {
inherit gitRepoUrl;
};
}
) // passthruAttrs // { inherit name; };
2024-05-02 00:46:19 +00:00
in
(fetcher fetcherArgs).overrideAttrs (finalAttrs: previousAttrs: {
meta = newMeta;
})
2024-05-02 00:46:19 +00:00
)