···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+ # Frustratingly, due to flakes and `fetchTree`, `fetchGit`
6969+ # has a different signature than the other builtin
7070+ # fetchers
7171+ fetchGit = args: (builtins.fetchGit args).outPath;
7272+ }
7373+ else
7474+ {
7575+ fetchTarball =
7676+ {
7777+ url,
7878+ sha256,
7979+ }:
8080+ pkgs.fetchzip {
8181+ inherit url sha256;
8282+ extension = "tar";
8383+ };
8484+ inherit (pkgs) fetchurl;
8585+ fetchGit =
8686+ {
8787+ url,
8888+ submodules,
8989+ rev,
9090+ name,
9191+ narHash,
9292+ }:
9393+ pkgs.fetchgit {
9494+ inherit url rev name;
9595+ fetchSubmodules = submodules;
9696+ hash = narHash;
9797+ };
9898+ };
9999+100100+ path =
101101+ if spec.type == "Git" then
102102+ mkGitSource fetchers spec
103103+ else if spec.type == "GitRelease" then
104104+ mkGitSource fetchers spec
105105+ else if spec.type == "PyPi" then
106106+ mkPyPiSource fetchers spec
107107+ else if spec.type == "Channel" then
108108+ mkChannelSource fetchers spec
109109+ else if spec.type == "Url" || spec.type == "MutableUrl" then
110110+ mkUrlSource fetchers spec
111111+ else if spec.type == "Container" then
112112+ mkContainerSource pkgs spec
113113+ else
114114+ builtins.throw "Unknown source type ${spec.type}";
115115+ in
116116+ spec // { outPath = mayOverride name path; };
117117+118118+ mkGitSource =
119119+ {
120120+ fetchTarball,
121121+ fetchGit,
122122+ ...
123123+ }:
124124+ {
125125+ repository,
126126+ revision,
127127+ url ? null,
128128+ submodules,
129129+ hash,
130130+ ...
131131+ }:
132132+ assert repository ? type;
133133+ # At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository
134134+ # In the latter case, there we will always be an url to the tarball
135135+ if url != null && !submodules then
136136+ fetchTarball {
137137+ inherit url;
138138+ sha256 = hash;
139139+ }
140140+ else
141141+ let
142142+ url =
143143+ if repository.type == "Git" then
144144+ repository.url
145145+ else if repository.type == "GitHub" then
146146+ "https://github.com/${repository.owner}/${repository.repo}.git"
147147+ else if repository.type == "GitLab" then
148148+ "${repository.server}/${repository.repo_path}.git"
149149+ else if repository.type == "Forgejo" then
150150+ "${repository.server}/${repository.owner}/${repository.repo}.git"
151151+ else
152152+ throw "Unrecognized repository type ${repository.type}";
153153+ urlToName =
154154+ url: rev:
155155+ let
156156+ matched = builtins.match "^.*/([^/]*)(\\.git)?$" url;
157157+158158+ short = builtins.substring 0 7 rev;
159159+160160+ appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else "";
161161+ in
162162+ "${if matched == null then "source" else builtins.head matched}${appendShort}";
163163+ name = urlToName url revision;
164164+ in
165165+ fetchGit {
166166+ rev = revision;
167167+ narHash = hash;
168168+169169+ inherit name submodules url;
170170+ };
171171+172172+ mkPyPiSource =
173173+ { fetchurl, ... }:
174174+ {
175175+ url,
176176+ hash,
177177+ ...
178178+ }:
179179+ fetchurl {
180180+ inherit url;
181181+ sha256 = hash;
182182+ };
183183+184184+ mkChannelSource =
185185+ { fetchTarball, ... }:
186186+ {
187187+ url,
188188+ hash,
189189+ ...
190190+ }:
191191+ fetchTarball {
192192+ inherit url;
193193+ sha256 = hash;
194194+ };
195195+196196+ mkUrlSource =
197197+ {
198198+ fetchTarball,
199199+ fetchurl,
200200+ ...
201201+ }:
202202+ {
203203+ url,
204204+ hash,
205205+ unpack,
206206+ ...
207207+ }:
208208+ (if unpack then fetchTarball else fetchurl) {
209209+ inherit url;
210210+ sha256 = hash;
211211+ };
212212+213213+ mkContainerSource =
214214+ pkgs:
215215+ {
216216+ image_name,
217217+ image_tag,
218218+ image_digest,
219219+ hash,
220220+ ...
221221+ }@args:
222222+ if pkgs == null then
223223+ builtins.throw "container sources require passing in a Nixpkgs value: https://github.com/andir/npins/blob/master/README.md#using-the-nixpkgs-fetchers"
224224+ else
225225+ pkgs.dockerTools.pullImage (
226226+ {
227227+ imageName = image_name;
228228+ imageDigest = image_digest;
229229+ finalImageTag = image_tag;
230230+ hash = hash;
231231+ }
232232+ // (if args.arch or null != null then { arch = args.arch; } else { })
233233+ );
234234+235235+in
236236+mkFunctor (
237237+ {
238238+ input ? ./sources.json,
239239+ }:
240240+ let
241241+ data =
242242+ if builtins.isPath input then
243243+ # while `readFile` will throw an error anyways if the path doesn't exist,
244244+ # we still need to check beforehand because *our* error can be caught but not the one from the builtin
245245+ # See: <https://git.lix.systems/lix-project/lix/issues/1098>
246246+ if builtins.pathExists input then
247247+ builtins.fromJSON (builtins.readFile input)
248248+ else
249249+ throw "Input path ${toString input} does not exist"
250250+ else if builtins.isAttrs input then
251251+ input
252252+ else
253253+ throw "Unsupported input type ${builtins.typeOf input}, must be a path or an attrset";
254254+ version = data.version;
255255+ in
256256+ if version == 8 then
257257+ builtins.mapAttrs (name: spec: mkFunctor (mkSource name spec)) data.pins
258258+ else
259259+ throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`"
260260+)