···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
111111+ builtins.throw "Unknown source type ${spec.type}";
112112+ in
113113+ spec // { outPath = mayOverride name path; };
114114+115115+ mkGitSource =
116116+ { fetchTarball, fetchGit, ... }:
117117+ {
118118+ repository,
119119+ revision,
120120+ url ? null,
121121+ submodules,
122122+ hash,
123123+ ...
124124+ }:
125125+ assert repository ? type;
126126+ # At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository
127127+ # In the latter case, there we will always be an url to the tarball
128128+ if url != null && !submodules then
129129+ fetchTarball {
130130+ inherit url;
131131+ sha256 = hash;
132132+ }
133133+ else
134134+ let
135135+ url =
136136+ if repository.type == "Git" then
137137+ repository.url
138138+ else if repository.type == "GitHub" then
139139+ "https://github.com/${repository.owner}/${repository.repo}.git"
140140+ else if repository.type == "GitLab" then
141141+ "${repository.server}/${repository.repo_path}.git"
142142+ else
143143+ throw "Unrecognized repository type ${repository.type}";
144144+ urlToName =
145145+ url: rev:
146146+ let
147147+ matched = builtins.match "^.*/([^/]*)(\\.git)?$" url;
148148+149149+ short = builtins.substring 0 7 rev;
150150+151151+ appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else "";
152152+ in
153153+ "${if matched == null then "source" else builtins.head matched}${appendShort}";
154154+ name = urlToName url revision;
155155+ in
156156+ fetchGit {
157157+ rev = revision;
158158+ narHash = hash;
159159+160160+ inherit name submodules url;
161161+ };
162162+163163+ mkPyPiSource =
164164+ { fetchurl, ... }:
165165+ { url, hash, ... }:
166166+ fetchurl {
167167+ inherit url;
168168+ sha256 = hash;
169169+ };
170170+171171+ mkChannelSource =
172172+ { fetchTarball, ... }:
173173+ { url, hash, ... }:
174174+ fetchTarball {
175175+ inherit url;
176176+ sha256 = hash;
177177+ };
178178+179179+ mkTarballSource =
180180+ { fetchTarball, ... }:
181181+ {
182182+ url,
183183+ locked_url ? url,
184184+ hash,
185185+ ...
186186+ }:
187187+ fetchTarball {
188188+ url = locked_url;
189189+ sha256 = hash;
190190+ };
191191+in
192192+mkFunctor (
193193+ {
194194+ input ? ./sources.json,
195195+ }:
196196+ let
197197+ data =
198198+ if builtins.isPath input then
199199+ # while `readFile` will throw an error anyways if the path doesn't exist,
200200+ # we still need to check beforehand because *our* error can be caught but not the one from the builtin
201201+ # *piegames sighs*
202202+ if builtins.pathExists input then
203203+ builtins.fromJSON (builtins.readFile input)
204204+ else
205205+ throw "Input path ${toString input} does not exist"
206206+ else if builtins.isAttrs input then
207207+ input
208208+ else
209209+ throw "Unsupported input type ${builtins.typeOf input}, must be a path or an attrset";
210210+ version = data.version;
211211+ in
212212+ if version == 6 then
213213+ builtins.mapAttrs (name: spec: mkFunctor (mkSource name spec)) data.pins
214214+ else
215215+ throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`"
216216+)