From 2149c2f7533b44b75151e5744644523aed295015 Mon Sep 17 00:00:00 2001 From: isabel Date: Mon, 18 Aug 2025 12:45:58 +0100 Subject: [PATCH] fix: flakey url resolution sometimes i could get it to work othertimes not so much; now it should just always work --- fetcher.nix | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 112 insertions(+), 2 deletions(-) diff --git a/fetcher.nix b/fetcher.nix index 66d4360..12294ff 100644 --- a/fetcher.nix +++ b/fetcher.nix @@ -1,10 +1,120 @@ -{ lib, fetchFromGitHub }: +# this is really just a fork of fetchFromGitHub, but with a few changes due to +# incompatibilities, for example `tag` is removed and there is no such concept +# of private in atproto, so all the private stuff has been removed. +# +# +# as of the time of writing, `.git` suffixed urls do not resolve correctly +# either +# +# as of the time of writing too +# `https://tangled.sh/@rockorager.dev/lsr/archive/v1.0.0.tar.gz` resolves to +# `https://knot1.tangled.sh/did:plc:vk2bemsr3uxaw4fdzno3chsh/lsr/archive/v1.0.0.tar.gz.tar.gz` +# which is an invalid url. + +{ + lib, + repoRevToNameMaybe, + fetchgit, + fetchzip, +}: lib.makeOverridable ( { domain ? "tangled.sh", + owner, + repo, + rev ? null, + name ? repoRevToNameMaybe repo rev "tangled", + + # fetchgit stuff + fetchSubmodules ? false, + leaveDotGit ? false, + deepClone ? false, + forceFetchGit ? false, + fetchLFS ? false, + sparseCheckout ? [ ], + + meta ? { }, ... }@args: - fetchFromGitHub ((removeAttrs args [ "domain" ]) // { githubBase = domain; }) + let + + position = ( + if args.meta.description or null != null then + builtins.unsafeGetAttrPos "description" args.meta + else + builtins.unsafeGetAttrPos "rev" args + ); + + baseUrl = "https://${domain}/${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 [ + "domain" + "owner" + "repo" + "rev" + "fetchSubmodules" + "forceFetchGit" + ]; + + useFetchGit = + fetchSubmodules || leaveDotGit || deepClone || forceFetchGit || fetchLFS || (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; + + fetcherArgs = + ( + if useFetchGit then + { + inherit + rev + deepClone + fetchSubmodules + sparseCheckout + fetchLFS + leaveDotGit + ; + url = baseUrl; + } + else + { + url = "${baseUrl}/archive/${rev}"; + extension = "tar.gz"; + + passthru = { + gitRepoUrl = baseUrl; + }; + } + ) + // passthruAttrs + // { + inherit name; + }; + in + + fetcher fetcherArgs + // { + meta = newMeta; + inherit owner repo rev; + } ) -- 2.51.2