default.nix

  1/*
  2  This file is provided under the MIT licence:
  3
  4  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  5
  6  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  7
  8  THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  9*/
 10# Generated by npins. Do not modify; will be overwritten regularly
 11let
 12  # Backwards-compatibly make something that previously didn't take any arguments take some
 13  # The function must return an attrset, and will unfortunately be eagerly evaluated
 14  # Same thing, but it catches eval errors on the default argument so that one may still call it with other arguments
 15  mkFunctor =
 16    fn:
 17    let
 18      e = builtins.tryEval (fn { });
 19    in
 20    (if e.success then e.value else { error = fn { }; }) // { __functor = _self: fn; };
 21
 22  # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
 23  range =
 24    first: last: if first > last then [ ] else builtins.genList (n: first + n) (last - first + 1);
 25
 26  # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
 27  stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
 28
 29  # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
 30  stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
 31  concatStrings = builtins.concatStringsSep "";
 32
 33  # If the environment variable NPINS_OVERRIDE_${name} is set, then use
 34  # the path directly as opposed to the fetched source.
 35  # (Taken from Niv for compatibility)
 36  mayOverride =
 37    name: path:
 38    let
 39      envVarName = "NPINS_OVERRIDE_${saneName}";
 40      saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name;
 41      ersatz = builtins.getEnv envVarName;
 42    in
 43    if ersatz == "" then
 44      path
 45    else
 46      # this turns the string into an actual Nix path (for both absolute and
 47      # relative paths)
 48      builtins.trace "Overriding path of \"${name}\" with \"${ersatz}\" due to set \"${envVarName}\"" (
 49        if builtins.substring 0 1 ersatz == "/" then
 50          /. + ersatz
 51        else
 52          /. + builtins.getEnv "PWD" + "/${ersatz}"
 53      );
 54
 55  mkSource =
 56    name: spec:
 57    {
 58      pkgs ? null,
 59    }:
 60    assert spec ? type;
 61    let
 62      # Unify across builtin and pkgs fetchers.
 63      # `fetchGit` requires a wrapper because of slight API differences.
 64      fetchers =
 65        if pkgs == null then
 66          {
 67            inherit (builtins) fetchTarball fetchurl;
 68            # For some fucking reason, fetchGit has a different signature than the other builtin fetchers …
 69            fetchGit = args: (builtins.fetchGit args).outPath;
 70          }
 71        else
 72          {
 73            fetchTarball =
 74              {
 75                url,
 76                sha256,
 77              }:
 78              pkgs.fetchzip {
 79                inherit url sha256;
 80                extension = "tar";
 81              };
 82            inherit (pkgs) fetchurl;
 83            fetchGit =
 84              {
 85                url,
 86                submodules,
 87                rev,
 88                name,
 89                narHash,
 90              }:
 91              pkgs.fetchgit {
 92                inherit url rev name;
 93                fetchSubmodules = submodules;
 94                hash = narHash;
 95              };
 96          };
 97
 98      # Dispatch to the correct code path based on the type
 99      path =
100        if spec.type == "Git" then
101          mkGitSource fetchers spec
102        else if spec.type == "GitRelease" then
103          mkGitSource fetchers spec
104        else if spec.type == "PyPi" then
105          mkPyPiSource fetchers spec
106        else if spec.type == "Channel" then
107          mkChannelSource fetchers spec
108        else if spec.type == "Tarball" then
109          mkTarballSource fetchers spec
110        else if spec.type == "Container" then
111          mkContainerSource pkgs spec
112        else
113          builtins.throw "Unknown source type ${spec.type}";
114    in
115    spec // { outPath = mayOverride name path; };
116
117  mkGitSource =
118    {
119      fetchTarball,
120      fetchGit,
121      ...
122    }:
123    {
124      repository,
125      revision,
126      url ? null,
127      submodules,
128      hash,
129      ...
130    }:
131    assert repository ? type;
132    # At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository
133    # In the latter case, there we will always be an url to the tarball
134    if url != null && !submodules then
135      fetchTarball {
136        inherit url;
137        sha256 = hash;
138      }
139    else
140      let
141        url =
142          if repository.type == "Git" then
143            repository.url
144          else if repository.type == "GitHub" then
145            "https://github.com/${repository.owner}/${repository.repo}.git"
146          else if repository.type == "GitLab" then
147            "${repository.server}/${repository.repo_path}.git"
148          else if repository.type == "Forgejo" then
149            "${repository.server}/${repository.owner}/${repository.repo}.git"
150          else
151            throw "Unrecognized repository type ${repository.type}";
152        urlToName =
153          url: rev:
154          let
155            matched = builtins.match "^.*/([^/]*)(\\.git)?$" url;
156
157            short = builtins.substring 0 7 rev;
158
159            appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else "";
160          in
161          "${if matched == null then "source" else builtins.head matched}${appendShort}";
162        name = urlToName url revision;
163      in
164      fetchGit {
165        rev = revision;
166        narHash = hash;
167
168        inherit name submodules url;
169      };
170
171  mkPyPiSource =
172    { fetchurl, ... }:
173    {
174      url,
175      hash,
176      ...
177    }:
178    fetchurl {
179      inherit url;
180      sha256 = hash;
181    };
182
183  mkChannelSource =
184    { fetchTarball, ... }:
185    {
186      url,
187      hash,
188      ...
189    }:
190    fetchTarball {
191      inherit url;
192      sha256 = hash;
193    };
194
195  mkTarballSource =
196    { fetchTarball, ... }:
197    {
198      url,
199      locked_url ? url,
200      hash,
201      ...
202    }:
203    fetchTarball {
204      url = locked_url;
205      sha256 = hash;
206    };
207
208  mkContainerSource =
209    pkgs:
210    {
211      image_name,
212      image_tag,
213      image_digest,
214      ...
215    }:
216    if pkgs == null then
217      builtins.throw "container sources require passing in a Nixpkgs value: https://github.com/andir/npins/blob/master/README.md#using-the-nixpkgs-fetchers"
218    else
219      pkgs.dockerTools.pullImage {
220        imageName = image_name;
221        imageDigest = image_digest;
222        finalImageTag = image_tag;
223      };
224in
225mkFunctor (
226  {
227    input ? ./sources.json,
228  }:
229  let
230    data =
231      if builtins.isPath input then
232        # while `readFile` will throw an error anyways if the path doesn't exist,
233        # we still need to check beforehand because *our* error can be caught but not the one from the builtin
234        # *piegames sighs*
235        if builtins.pathExists input then
236          builtins.fromJSON (builtins.readFile input)
237        else
238          throw "Input path ${toString input} does not exist"
239      else if builtins.isAttrs input then
240        input
241      else
242        throw "Unsupported input type ${builtins.typeOf input}, must be a path or an attrset";
243    version = data.version;
244  in
245  if version == 7 then
246    builtins.mapAttrs (name: spec: mkFunctor (mkSource name spec)) data.pins
247  else
248    throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`"
249)