···11+/*
22+ This file is provided under the MIT licence:
33+44+ 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:
55+66+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
77+88+ 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.
99+*/
1010+# Generated by npins. Do not modify; will be overwritten regularly
1111+let
1212+ # Backwards-compatibly make something that previously didn't take any arguments take some
1313+ # The function must return an attrset, and will unfortunately be eagerly evaluated
1414+ # Same thing, but it catches eval errors on the default argument so that one may still call it with other arguments
1515+ mkFunctor =
1616+ fn:
1717+ let
1818+ e = builtins.tryEval (fn { });
1919+ in
2020+ (if e.success then e.value else { error = fn { }; }) // { __functor = _self: fn; };
2121+2222+ # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
2323+ range =
2424+ first: last: if first > last then [ ] else builtins.genList (n: first + n) (last - first + 1);
2525+2626+ # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
2727+ stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
2828+2929+ # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
3030+ stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
3131+ concatStrings = builtins.concatStringsSep "";
3232+3333+ # If the environment variable NPINS_OVERRIDE_${name} is set, then use
3434+ # the path directly as opposed to the fetched source.
3535+ # (Taken from Niv for compatibility)
3636+ mayOverride =
3737+ name: path:
3838+ let
3939+ envVarName = "NPINS_OVERRIDE_${saneName}";
4040+ saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name;
4141+ ersatz = builtins.getEnv envVarName;
4242+ in
4343+ if ersatz == "" then
4444+ path
4545+ else
4646+ # this turns the string into an actual Nix path (for both absolute and
4747+ # relative paths)
4848+ builtins.trace "Overriding path of \"${name}\" with \"${ersatz}\" due to set \"${envVarName}\"" (
4949+ if builtins.substring 0 1 ersatz == "/" then
5050+ /. + ersatz
5151+ else
5252+ /. + builtins.getEnv "PWD" + "/${ersatz}"
5353+ );
5454+5555+ mkSource =
5656+ name: spec:
5757+ {
5858+ pkgs ? null,
5959+ }:
6060+ assert spec ? type;
6161+ let
6262+ # Unify across builtin and pkgs fetchers.
6363+ # `fetchGit` requires a wrapper because of slight API differences.
6464+ fetchers =
6565+ if pkgs == null then
6666+ {
6767+ inherit (builtins) fetchTarball fetchurl;
6868+ # For some fucking reason, fetchGit has a different signature than the other builtin fetchers …
6969+ fetchGit = args: (builtins.fetchGit args).outPath;
7070+ }
7171+ else
7272+ {
7373+ fetchTarball =
7474+ {
7575+ url,
7676+ sha256,
7777+ }:
7878+ pkgs.fetchzip {
7979+ inherit url sha256;
8080+ extension = "tar";
8181+ };
8282+ inherit (pkgs) fetchurl;
8383+ fetchGit =
8484+ {
8585+ url,
8686+ submodules,
8787+ rev,
8888+ name,
8989+ narHash,
9090+ }:
9191+ pkgs.fetchgit {
9292+ inherit url rev name;
9393+ fetchSubmodules = submodules;
9494+ hash = narHash;
9595+ };
9696+ };
9797+9898+ # Dispatch to the correct code path based on the type
9999+ path =
100100+ if spec.type == "Git" then
101101+ mkGitSource fetchers spec
102102+ else if spec.type == "GitRelease" then
103103+ mkGitSource fetchers spec
104104+ else if spec.type == "PyPi" then
105105+ mkPyPiSource fetchers spec
106106+ else if spec.type == "Channel" then
107107+ mkChannelSource fetchers spec
108108+ else if spec.type == "Tarball" then
109109+ mkTarballSource fetchers spec
110110+ else if spec.type == "Container" then
111111+ mkContainerSource pkgs spec
112112+ else
113113+ builtins.throw "Unknown source type ${spec.type}";
114114+ in
115115+ spec // { outPath = mayOverride name path; };
116116+117117+ mkGitSource =
118118+ {
119119+ fetchTarball,
120120+ fetchGit,
121121+ ...
122122+ }:
123123+ {
124124+ repository,
125125+ revision,
126126+ url ? null,
127127+ submodules,
128128+ hash,
129129+ ...
130130+ }:
131131+ assert repository ? type;
132132+ # At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository
133133+ # In the latter case, there we will always be an url to the tarball
134134+ if url != null && !submodules then
135135+ fetchTarball {
136136+ inherit url;
137137+ sha256 = hash;
138138+ }
139139+ else
140140+ let
141141+ url =
142142+ if repository.type == "Git" then
143143+ repository.url
144144+ else if repository.type == "GitHub" then
145145+ "https://github.com/${repository.owner}/${repository.repo}.git"
146146+ else if repository.type == "GitLab" then
147147+ "${repository.server}/${repository.repo_path}.git"
148148+ else
149149+ throw "Unrecognized repository type ${repository.type}";
150150+ urlToName =
151151+ url: rev:
152152+ let
153153+ matched = builtins.match "^.*/([^/]*)(\\.git)?$" url;
154154+155155+ short = builtins.substring 0 7 rev;
156156+157157+ appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else "";
158158+ in
159159+ "${if matched == null then "source" else builtins.head matched}${appendShort}";
160160+ name = urlToName url revision;
161161+ in
162162+ fetchGit {
163163+ rev = revision;
164164+ narHash = hash;
165165+166166+ inherit name submodules url;
167167+ };
168168+169169+ mkPyPiSource =
170170+ { fetchurl, ... }:
171171+ {
172172+ url,
173173+ hash,
174174+ ...
175175+ }:
176176+ fetchurl {
177177+ inherit url;
178178+ sha256 = hash;
179179+ };
180180+181181+ mkChannelSource =
182182+ { fetchTarball, ... }:
183183+ {
184184+ url,
185185+ hash,
186186+ ...
187187+ }:
188188+ fetchTarball {
189189+ inherit url;
190190+ sha256 = hash;
191191+ };
192192+193193+ mkTarballSource =
194194+ { fetchTarball, ... }:
195195+ {
196196+ url,
197197+ locked_url ? url,
198198+ hash,
199199+ ...
200200+ }:
201201+ fetchTarball {
202202+ url = locked_url;
203203+ sha256 = hash;
204204+ };
205205+206206+ mkContainerSource =
207207+ pkgs:
208208+ {
209209+ image_name,
210210+ image_tag,
211211+ image_digest,
212212+ ...
213213+ }:
214214+ if pkgs == null then
215215+ builtins.throw "container sources require passing in a Nixpkgs value: https://github.com/andir/npins/blob/master/README.md#using-the-nixpkgs-fetchers"
216216+ else
217217+ pkgs.dockerTools.pullImage {
218218+ imageName = image_name;
219219+ imageDigest = image_digest;
220220+ finalImageTag = image_tag;
221221+ };
222222+in
223223+mkFunctor (
224224+ {
225225+ input ? ./sources.json,
226226+ }:
227227+ let
228228+ data =
229229+ if builtins.isPath input then
230230+ # while `readFile` will throw an error anyways if the path doesn't exist,
231231+ # we still need to check beforehand because *our* error can be caught but not the one from the builtin
232232+ # *piegames sighs*
233233+ if builtins.pathExists input then
234234+ builtins.fromJSON (builtins.readFile input)
235235+ else
236236+ throw "Input path ${toString input} does not exist"
237237+ else if builtins.isAttrs input then
238238+ input
239239+ else
240240+ throw "Unsupported input type ${builtins.typeOf input}, must be a path or an attrset";
241241+ version = data.version;
242242+ in
243243+ if version == 7 then
244244+ builtins.mapAttrs (name: spec: mkFunctor (mkSource name spec)) data.pins
245245+ else
246246+ throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`"
247247+)