summaryrefslogtreecommitdiff
path: root/pkgs/build-support/fetchsourcehut/default.nix
blob: f22746a64f74a34e84aebb57cce24fc7656c4c6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
{
  lib,
  repoRevToNameMaybe,
  fetchgit,
  fetchhg,
  fetchzip,
}:

let
  inherit (lib)
    assertOneOf
    makeOverridable
    optionalString
    ;
in

makeOverridable (
  {
    owner,
    repo,
    rev ? null,
    tag ? null,
    name ? repoRevToNameMaybe repo (lib.revOrTag rev tag) "sourcehut",
    domain ? "sr.ht",
    vc ? "git",
    fetchSubmodules ? false,
    ... # For hash agility
  }@args:

  assert (
    lib.assertMsg (lib.xor (tag == null) (
      rev == null
    )) "fetchFromSourcehut requires one of either `rev` or `tag` to be provided (not both)."
  );

  assert (
    assertOneOf "vc" vc [
      "hg"
      "git"
    ]
  );

  let
    urlFor = resource: "https://${resource}.${domain}/${owner}/${repo}";
    rev' = if tag != null then tag else rev;
    baseUrl = urlFor vc;
    baseArgs = {
      inherit name;
    }
    // removeAttrs args [
      "owner"
      "repo"
      "rev"
      "tag"
      "domain"
      "vc"
      "name"
      "fetchSubmodules"
    ];
    vcArgs = baseArgs // {
      rev = rev';
      url = baseUrl;
    };
    fetcher = if fetchSubmodules then vc else "zip";
    cases = {
      git = {
        fetch = fetchgit;
        arguments = vcArgs // {
          fetchSubmodules = true;
        };
      };
      hg = {
        fetch = fetchhg;
        arguments = vcArgs // {
          fetchSubrepos = true;
        };
      };
      zip = {
        fetch = fetchzip;
        arguments = baseArgs // {
          url = "${baseUrl}/archive/${rev'}.tar.gz";
          postFetch = optionalString (vc == "hg") ''
            rm -f "$out/.hg_archival.txt"
          ''; # impure file; see #12002
          passthru = (args.passthru or { }) // {
            gitRepoUrl = urlFor "git";
          };
        };
      };
    };
  in
  cases.${fetcher}.fetch cases.${fetcher}.arguments
  // {
    rev = rev';
    meta.homepage = "${baseUrl}";
  }
)