Advent of Code
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
149 throw "Unrecognized repository type ${repository.type}";
150 urlToName =
151 url: rev:
152 let
153 matched = builtins.match "^.*/([^/]*)(\\.git)?$" url;
154
155 short = builtins.substring 0 7 rev;
156
157 appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else "";
158 in
159 "${if matched == null then "source" else builtins.head matched}${appendShort}";
160 name = urlToName url revision;
161 in
162 fetchGit {
163 rev = revision;
164 narHash = hash;
165
166 inherit name submodules url;
167 };
168
169 mkPyPiSource =
170 { fetchurl, ... }:
171 {
172 url,
173 hash,
174 ...
175 }:
176 fetchurl {
177 inherit url;
178 sha256 = hash;
179 };
180
181 mkChannelSource =
182 { fetchTarball, ... }:
183 {
184 url,
185 hash,
186 ...
187 }:
188 fetchTarball {
189 inherit url;
190 sha256 = hash;
191 };
192
193 mkTarballSource =
194 { fetchTarball, ... }:
195 {
196 url,
197 locked_url ? url,
198 hash,
199 ...
200 }:
201 fetchTarball {
202 url = locked_url;
203 sha256 = hash;
204 };
205
206 mkContainerSource =
207 pkgs:
208 {
209 image_name,
210 image_tag,
211 image_digest,
212 ...
213 }:
214 if pkgs == null then
215 builtins.throw "container sources require passing in a Nixpkgs value: https://github.com/andir/npins/blob/master/README.md#using-the-nixpkgs-fetchers"
216 else
217 pkgs.dockerTools.pullImage {
218 imageName = image_name;
219 imageDigest = image_digest;
220 finalImageTag = image_tag;
221 };
222in
223mkFunctor (
224 {
225 input ? ./sources.json,
226 }:
227 let
228 data =
229 if builtins.isPath input then
230 # while `readFile` will throw an error anyways if the path doesn't exist,
231 # we still need to check beforehand because *our* error can be caught but not the one from the builtin
232 # *piegames sighs*
233 if builtins.pathExists input then
234 builtins.fromJSON (builtins.readFile input)
235 else
236 throw "Input path ${toString input} does not exist"
237 else if builtins.isAttrs input then
238 input
239 else
240 throw "Unsupported input type ${builtins.typeOf input}, must be a path or an attrset";
241 version = data.version;
242 in
243 if version == 7 then
244 builtins.mapAttrs (name: spec: mkFunctor (mkSource name spec)) data.pins
245 else
246 throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`"
247)