summaryrefslogtreecommitdiffstats
path: root/npinsToPlugins.nix
blob: 85c504a17fee9a40737cb9bb8740b64ed0948b07 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
{
  lib,
  fetchurl,
  fetchgit,
  fetchzip,
}:
func: pathToNpins:
func
  (
    name: spec:
    let
      mayOverride =
        name: path:
        let
          envVarName = "NPINS_OVERRIDE_${saneName}";
          saneName = builtins.concatStringsSep "_" (
            builtins.concatLists (
              builtins.filter (x: builtins.isList x && x != [ "" ]) (builtins.split "([a-zA-Z0-9]*)" name)
            )
          );
          ersatz = builtins.getEnv envVarName;
        in
        if ersatz == "" then
          path
        else
          # this turns the string into an actual Nix path (for both absolute and
          # relative paths)
          builtins.trace "Overriding path of \"${name}\" with \"${ersatz}\" due to set \"${envVarName}\"" (
            if builtins.substring 0 1 ersatz == "/" then
              /. + ersatz
            else
              /. + builtins.getEnv "PWD" + "/${ersatz}"
          );

      path =
        rec {
          GitRelease = Git;
          Channel = Tarball;

          Git =
            if spec.url != null && !spec.submodules then
              Tarball
            else
              fetchgit (
                let
                  repo = spec.repository;
                  url =
                    {
                      Git = repo.url;
                      GitHub = "https://github.com/${repo.owner}/${repo.repo}.git";
                      GitLab = "${repo.server}/${repo.repo_path}.git";
                      Forgejo = "${repo.server}/${repo.owner}/${repo.repo}.git";
                    }
                    .${repo.type} or (throw "Unrecognized repository type ${repo.type}");
                in
                {
                  name =
                    let
                      matched = builtins.match "^.*/([^/]*)(\\.git)?$" url;
                      appendShort =
                        if (builtins.match "[a-f0-9]*" spec.revision) != null then
                          "-${builtins.substring 0 7 spec.revision}"
                        else
                          "";
                    in
                    "${if matched == null then "source" else builtins.head matched}${appendShort}";
                  inherit url;

                  rev = spec.revision;
                  inherit (spec) hash;
                  fetchSubmodules = spec.submodules;
                }
              );

          PyPi = fetchurl {
            inherit (spec) url hash;
          };

          Tarball = fetchzip {
            inherit (spec) url hash;
            extension = "tar";
          };

        }
        .${spec.type} or (builtins.throw "Unknown source type ${spec.type}");

      version = if spec ? revision then builtins.substring 0 8 spec.revision else "0";
    in
    spec
    // {
      name = "${name}-${version}";
      pname = name;
      inherit version;
      vimPlugin = true;
      outPath =
        (
          # Override logic won't do anything if we're in pure eval
          if builtins ? currentSystem then mayOverride name path else path
        ).overrideAttrs
          {
            pname = name;
            name = "${name}-${version}";
            inherit version;
          };
    }
  )
  (
    let
      json = lib.importJSON pathToNpins;
    in
    assert lib.assertMsg
      (lib.elem json.version [
        7
        8
      ])
      ''
        Your npins version does not match that of mnw.lib.npinsToPlugins.
        Please run npins upgrade, if that does not work file a issue in the mnw repo
      '';
    json.pins
  )