├── .envrc
├── .github
├── dependabot.yml
└── workflows
│ └── update-flake-lock.yml
├── .gitignore
├── .gitmodules
├── Cargo.lock
├── Cargo.toml
├── LICENSE
├── README.md
├── TODO.md
├── bors.toml
├── contrib
├── evaluation
│ ├── sandboxed
│ │ ├── iterate_over_pypi_dump.sh
│ │ └── pack_from_pypi.sh
│ └── unsandboxed
│ │ ├── functions.sh
│ │ ├── iterate_over_pypi_dump.sh
│ │ ├── iterate_over_rubygems_dump.sh
│ │ ├── pack_from_pypi.sh
│ │ └── pack_from_rubygems.sh
├── iterate_over_pypi_dump.sh
└── pack_from_pypi.sh
├── data
├── autotools
│ └── m4.toml
├── glibc
│ ├── endian.toml
│ └── xlocale.toml
└── python
│ ├── cffi.toml
│ ├── cryptography.toml
│ └── cython.toml
├── default.nix
├── flake.lock
├── flake.nix
├── garnix.yaml
├── src
├── cache
│ ├── database.rs
│ ├── files.rs
│ ├── frcode.rs
│ ├── mod.rs
│ └── package.rs
├── design.md
├── fs.rs
├── interactive.rs
├── main.rs
├── nix.rs
├── popcount.rs
├── resolution.rs
└── runner.rs
├── tests
├── flake-module.nix
├── lib.nix
└── nixos-test.nix
└── treefmt
└── flake-module.nix
/.envrc:
--------------------------------------------------------------------------------
1 | use flake
2 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: "github-actions"
4 | directory: "/"
5 | schedule:
6 | interval: "weekly"
7 | - package-ecosystem: "cargo"
8 | directory: "/"
9 | schedule:
10 | interval: "weekly"
11 |
--------------------------------------------------------------------------------
/.github/workflows/update-flake-lock.yml:
--------------------------------------------------------------------------------
1 | name: update-flake-lock
2 | on:
3 | workflow_dispatch: # allows manual triggering
4 | schedule:
5 | - cron: '0 0 * * 1,4' # Run twice a week
6 |
7 | jobs:
8 | lockfile:
9 | runs-on: ubuntu-latest
10 | steps:
11 | - name: Checkout repository
12 | uses: actions/checkout@v3
13 | - name: Install Nix
14 | uses: cachix/install-nix-action@v20
15 | with:
16 | github_access_token: ${{ secrets.GITHUB_TOKEN }}
17 | - name: Update flake.lock
18 | uses: DeterminateSystems/update-flake-lock@v19
19 | with:
20 | pr-body: |
21 | Automated changes by the update-flake-lock
22 | ```
23 | {{ env.GIT_COMMIT_MESSAGE }}
24 | ```
25 | bors merge
26 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Generated by Cargo
2 | # will have compiled files and executables
3 | /target/
4 |
5 | # These are backup files generated by rustfmt
6 | **/*.rs.bk
7 |
8 |
9 | # Added by cargo
10 |
11 | /target
12 |
13 | # nix
14 | /result
15 | .direnv
16 | test-venv
17 | popcount-graph.json
18 | nix-index-files
19 |
--------------------------------------------------------------------------------
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "examples"]
2 | path = examples
3 | url = https://github.com/RaitoBezarius/buildxyz-examples
4 |
--------------------------------------------------------------------------------
/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "buildxyz"
3 | version = "0.1.0"
4 | edition = "2021"
5 |
6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7 |
8 | [dependencies]
9 | fuser = { version = "0.12", features = [ "serializable" ] }
10 | nix = "0.26.2"
11 | log = "0.4.17"
12 | stderrlog = "0.5.4"
13 | ctrlc = "3.2.5"
14 | clap = { version = "4.1.8", features = [ "derive" ] }
15 | crossbeam-channel = "0.5.7"
16 | xdg = "2.4.1"
17 | tui = "0.19.0"
18 | crossterm = "0.26"
19 | signal-hook = "0.3.15"
20 | # nix-index dependencies
21 | regex = "1.7.1"
22 | error-chain = "0.12.4"
23 | memchr = "2.5.0"
24 | zstd = { version = "0.12.3", features = [ "zstdmt" ] }
25 | serde_json = "1.0.94"
26 | byteorder = "1.4.3"
27 | regex-syntax = "0.7.1"
28 | grep = "0.2.11"
29 | serde = "1.0.163"
30 | num_cpus = "1.15.0"
31 | serde_bytes = "0.11.9"
32 | tempfile = "3.4.0"
33 | lazy_static = "1.4.0"
34 | toml = "0.7.3"
35 | thiserror = "1.0.40"
36 | walkdir = "2.3.3"
37 | include_dir = { version = "0.7.3", features = [ "glob" ] }
38 |
39 | [profile.release]
40 | debug = true
41 |
42 | [profile.dev]
43 | opt-level = 1 # Otherwise queries takes 10s (~500ms for opt-level=1).
44 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # `buildxyz`
2 |
3 | Build your (Nix) package automatically.
4 |
5 | ## Introduction
6 |
7 | `buildxyz` is a Rust program running your build system invocation and injecting into many commonly used environment variables extra search directories to trap
8 | any filesystem access that cannot be provided by your existing environment.
9 |
10 | By doing so, `buildxyz` can know what your build system needs and will provide it dynamically using the Nix store and a nixpkgs index database.
11 |
12 | Finally, once your build system built, `buildxyz` remembers what it actually had to provide and can rematerialize the set of dependencies provided for any usage, e.g. providing this data to `nix-init` to automatically write a Nix derivation, fix implicit dependencies, etc.
13 |
14 | ## Design
15 |
16 | `buildxyz` relies on [the FUSE technology](https://en.wikipedia.org/wiki/Filesystem_in_Userspace) to instantiate an instrumented filesystem which does not require any higher privilege.
17 |
18 | Here is a sequence example of `buildxyz` operating:
19 |
20 | ```mermaid
21 | sequenceDiagram
22 | Build system->>Operating System: open(/usr/bin/pip)
23 | Operating System-)Build system: OK
24 | Build system->>Operating System: open(/usr/lib/cuda/...)
25 | Operating System->>Build system: ENOENT
26 | Build system->>Operating System: open(/tmp/buildxyz/lib/cuda/...)
27 | Operating System->>BuildXYZ: lookup(/tmp/buildxyz/lib/cuda/...)
28 | BuildXYZ->>Database: search this path
29 | Database-)BuildXYZ: /nix/store/eeeeeeeeee-cuda/
30 | BuildXYZ->>User: Do you want to use this dependency or provide your own?
31 | User-)BuildXYZ: Use the preferred candidate
32 | BuildXYZ->>Build system: This is a symlink to /nix/store/eeeeeeeeee-cuda/
33 | Build system->>Operating System: readlink(/nix/store/eeee..-cuda)
34 | Operating System->>Build system: OK
35 | ```
36 |
37 | ## Actually implemented
38 |
39 | BuildXYZ can already provide dependencies to your build system based on a precise revision of nixpkgs, pinned in the `default.nix`.
40 |
41 | It is known to work on Python's packages (through `pip install --no-binary :all:`) and sometimes on certain autotools project depending on their complexity.
42 |
43 | ## Resolutions
44 |
45 | When BuildXYZ receives a new filesystem access, it means that the existing environment failed to provide it.
46 |
47 | If the filesystem access has a match in the nixpkgs index database, two options are possible:
48 |
49 | - provide it
50 | - do not provide it
51 |
52 | We call resolution the information composed of a filesystem access identified by a canonical path and a decision: provide it or not.
53 |
54 | Not all filesystem accesses should be provided even if we have matches for them, that's why we enable custom resolutions which can be managed through policies: user interaction, language-specific resolutions, etc.
55 |
56 | The resolution data for a project is very interesting as it is exactly the "implicit dependencies" data that is required to build a project, which is often described through instructions.
57 |
58 | ## Goals & TODO
59 |
60 | Current objective: get Nix to compile without any manually provided dependency using BuildXYZ.
61 |
62 | - Proper restart & program lifecycle (Ctrl-C, SIGTERM)
63 | - Proper discovery of existing resolutions databases
64 | - Proper flags to record new resolutions or merge them in an existing file
65 | - Human/machine-readable format for resolutions
66 | - Extend graphs of dependencies with implicit dependencies
67 | - `nix-init` integration
68 |
69 | # Usage
70 |
71 | Run the project:
72 |
73 | ``` nix
74 | nix run github:RaitoBezarius/buildxyz
75 | ```
76 |
77 | Build the project:
78 |
79 | ``` nix
80 | nix build
81 | ```
82 |
83 | Run all tests:
84 |
85 | ``` nix
86 | nix flake check -L
87 | ```
88 |
89 | Run formatters:
90 |
91 | ``` nix
92 | nix fmt
93 | ```
94 |
--------------------------------------------------------------------------------
/TODO.md:
--------------------------------------------------------------------------------
1 | # Long term
2 |
3 | - [ ] Analyze drvs to understand popularity of dependency graph
4 |
5 | # Mid term
6 |
7 | - [ ] Integrate nix-index computations of popcount
8 | - [ ] Investigate how to understand hooks from nixpkgs model
9 | - [ ] Benchmark popularity counts using header-only libraries as an example (collect list of header-only libraries)
10 | - [ ] Interactive sandboxing
11 | - [ ] Record resolutions
12 |
13 | # Short term
14 |
15 | - [ ] Visualization of nixpkgs graph
16 |
--------------------------------------------------------------------------------
/bors.toml:
--------------------------------------------------------------------------------
1 | cut_body_after = "" # don't include text from the PR body in the merge commit message
2 | status = [
3 | "Evaluate flake.nix",
4 | "check clippy [x86_64-linux]",
5 | "check nixos-test [x86_64-linux]",
6 | "check treefmt [x86_64-linux]",
7 | "package buildxyz [aarch64-darwin]",
8 | "package buildxyz [x86_64-linux]",
9 | "package default [aarch64-darwin]",
10 | "package default [x86_64-linux]"
11 | ]
12 |
--------------------------------------------------------------------------------
/contrib/evaluation/sandboxed/iterate_over_pypi_dump.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env nix-shell
2 | #!nix-shell --pure -i bash -p jq git nix bubblewrap which cacert parallel tmux --keep BUILDXYZ_NIXPKGS --keep RUST_BACKTRACE
3 | # shellcheck shell=sh
4 |
5 | JOB="${1:-pypi-job}"
6 |
7 | generic_buildxyz() {
8 | builder="$1"
9 | package="$2"
10 | echo "buildxyz $package"
11 | # This is needed for the new tmpfs
12 | export TMPDIR="/buildxyz"
13 | # CAP_SYS_ADMIN is for the fusermount
14 | # /dev bind is for /dev/fuse
15 | # --share-net is necessary for network interactions.
16 | # share also the DNS resolver.
17 | bwrap \
18 | --ro-bind /etc/resolv.conf /etc/resolv.conf \
19 | --share-net \
20 | --bind /nix /nix \
21 | --dev-bind /dev /dev \
22 | --ro-bind $(which git) $(which git) \
23 | --ro-bind $(pwd)/target $(pwd)/target \
24 | --bind $(pwd)/examples $(pwd)/examples \
25 | --tmpfs /buildxyz \
26 | --proc /proc \
27 | --unshare-pid \
28 | --cap-add CAP_SYS_ADMIN \
29 | --new-session \
30 | ./target/debug/buildxyz --automatic --record-to "examples/python/$package.toml" "$builder $package"
31 | }
32 |
33 | pip_install() {
34 | package="$1"
35 | pip install "$package" --prefix /tmp --no-binary :all
36 | }
37 |
38 | pypi_buildxyz() {
39 | generic_buildxyz pip_install "$@"
40 | }
41 |
42 | export -f pypi_buildxyz
43 |
44 | readarray -t PYPI_PACKAGES < <(jq -rc '.rows | .[] | .project' top-pypi.json)
45 | parallel --joblog $JOB --progress --bar --delay 2.5 --jobs 50% --tmux pypi_buildxyz ::: "${PYPI_PACKAGES[@]}"
46 |
--------------------------------------------------------------------------------
/contrib/evaluation/sandboxed/pack_from_pypi.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env nix-shell
2 | #!nix-shell --pure -i bash -p gcc jq git nix bubblewrap which cacert strace --keep BUILDXYZ_NIXPKGS --keep RUST_BACKTRACE --keep MANUAL --keep ENABLE_STRACE --keep NIX_DEBUG
3 | # shellcheck shell=sh
4 |
5 | buildxyz_global_flags=()
6 | STRACE=""
7 |
8 | if [[ -v ENABLE_STRACE ]]; then
9 | STRACE="strace -yy -e file -f"
10 | fi
11 |
12 | if [[ ! -v MANUAL ]]; then
13 | buildxyz_global_flags+=(--automatic)
14 | fi
15 |
16 | pypi_buildxyz() {
17 | package="$1"
18 | echo "buildxyz $package"
19 | export TMPDIR="/buildxyz"
20 | bwrap \
21 | --ro-bind /etc/resolv.conf /etc/resolv.conf \
22 | --share-net \
23 | --bind /nix /nix \
24 | --dev-bind /dev /dev \
25 | --ro-bind $(which git) $(which git) \
26 | --ro-bind $(pwd)/target $(pwd)/target \
27 | --bind $(pwd)/examples $(pwd)/examples \
28 | --tmpfs /buildxyz \
29 | --proc /proc \
30 | --unshare-pid \
31 | --cap-add CAP_SYS_ADMIN \
32 | --new-session \
33 | $STRACE ./target/debug/buildxyz "${buildxyz_global_flags[@]}" --record-to "examples/python/$package.toml" "pip install --verbose $package --prefix /tmp --no-binary :all:"
34 | }
35 |
36 | pypi_buildxyz "$1"
37 |
--------------------------------------------------------------------------------
/contrib/evaluation/unsandboxed/functions.sh:
--------------------------------------------------------------------------------
1 | set -euxo pipefail
2 | export BUILDXYZ_RELEASE_VARIANT="release"
3 | export BUILDXYZ_BINARY="./target/$BUILDXYZ_RELEASE_VARIANT/buildxyz"
4 | # Improve the performance of the evaluation
5 | # because some packages believe it's fine to adopt nightly features
6 | # in Python releases…
7 | export RUSTC_BOOTSTRAP=1
8 |
9 | export buildxyz_global_flags=()
10 |
11 | if [[ -v AUTOMATIC ]]; then
12 | buildxyz_global_flags+=(--automatic)
13 | fi
14 |
15 | # Debugging infrastructure
16 | export STRACE=""
17 | export STRACE_EXTRA_FLAGS=""
18 |
19 | if [[ -v ENABLE_STRACE ]]; then
20 | STRACE="strace -yy -f $STRACE_EXTRA_FLAGS"
21 | fi
22 |
23 | # Manual interaction
24 | if [[ ! -v MANUAL ]]; then
25 | buildxyz_global_flags+=(--automatic)
26 | fi
27 |
28 | pypi_buildxyz() {
29 | package="$1"
30 | PREFIX_DIR=$(mktemp -d)
31 | echo "buildxyz $package in pip prefix $PREFIX_DIR"
32 | $STRACE $BUILDXYZ_BINARY "${buildxyz_global_flags[@]}" --record-to "examples/python/$package.toml" "pip install --use-feature=no-binary-enable-wheel-cache --prefix $PREFIX_DIR --no-binary :all: --no-cache-dir $package"
33 | }
34 |
35 | rubygems_buildxyz() {
36 | package="$1"
37 | PREFIX_DIR=$(mktemp -d)
38 | export GEM_HOME="$PREFIX_DIR"
39 | echo "buildxyz $package in gem prefix $PREFIX_DIR"
40 | $STRACE $BUILDXYZ_BINARY "${buildxyz_global_flags[@]}" --record-to "examples/ruby/$package.toml" "gem install --bindir $(mktemp -d) --install-dir $(mktemp -d) --no-user-install $package"
41 | }
42 |
--------------------------------------------------------------------------------
/contrib/evaluation/unsandboxed/iterate_over_pypi_dump.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env nix-shell
2 | #!nix-shell --pure -i bash -p jq git nix bubblewrap which parallel tmux --keep BUILDXYZ_NIXPKGS --keep RUST_BACKTRACE --keep ENABLE_STRACE --keep NIX_DEBUG --keep MANUAL
3 | # shellcheck shell=sh
4 |
5 | SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
6 | . "$SCRIPT_DIR/functions.sh"
7 |
8 | JOB="${1:-pypi-job}"
9 | export -f pypi_buildxyz
10 |
11 | readarray -t PYPI_PACKAGES < <(jq -rc '.rows | .[] | .project' top-pypi.json)
12 | mkdir -p "$TMPDIR/job-logs/$JOB"
13 | parallel --output-as-files --results "$TMPDIR/job-logs/$JOB" --resume-failed --joblog $JOB --progress --bar --delay 2.5 --jobs 25% --tmuxpane pypi_buildxyz ::: "${PYPI_PACKAGES[@]}"
14 |
--------------------------------------------------------------------------------
/contrib/evaluation/unsandboxed/iterate_over_rubygems_dump.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env nix-shell
2 | #!nix-shell --pure -i bash -p jq git nix bubblewrap which parallel tmux --keep BUILDXYZ_NIXPKGS --keep RUST_BACKTRACE --keep ENABLE_STRACE --keep NIX_DEBUG --keep MANUAL
3 | # shellcheck shell=sh
4 |
5 | SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
6 | . "$SCRIPT_DIR/functions.sh"
7 |
8 | JOB="${1:-ruby-job}"
9 | export -f rubygems_buildxyz
10 |
11 | while IFS=',' read -ra TOP_RUBY_PACKAGES; do
12 | RUBY_PACKAGES+=("${TOP_RUBY_PACKAGES[0]}")
13 | done < top-rubygems.csv
14 |
15 |
16 | mkdir -p "$TMPDIR/job-logs/$JOB"
17 | parallel --output-as-files --results "$TMPDIR/job-logs/$JOB" --resume-failed --joblog $JOB --progress --bar --delay 2.5 --jobs 25% --tmuxpane rubygems_buildxyz ::: "${RUBY_PACKAGES[@]}"
18 |
--------------------------------------------------------------------------------
/contrib/evaluation/unsandboxed/pack_from_pypi.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env nix-shell
2 | #!nix-shell --pure -i bash -p jq git nix which cacert strace --keep BUILDXYZ_NIXPKGS --keep RUST_BACKTRACE --keep MANUAL --keep ENABLE_STRACE --keep NIX_DEBUG --keep RUSTC_BOOTSTRAP
3 | # shellcheck shell=sh
4 |
5 | SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
6 | . "$SCRIPT_DIR/functions.sh"
7 |
8 | pypi_buildxyz "$1"
9 |
--------------------------------------------------------------------------------
/contrib/evaluation/unsandboxed/pack_from_rubygems.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env nix-shell
2 | #!nix-shell --pure -i bash -p jq git nix which cacert strace --keep BUILDXYZ_NIXPKGS --keep RUST_BACKTRACE --keep MANUAL --keep ENABLE_STRACE --keep NIX_DEBUG --keep RUSTC_BOOTSTRAP
3 | # shellcheck shell=sh
4 |
5 | SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
6 | . "$SCRIPT_DIR/functions.sh"
7 |
8 | rubygems_buildxyz "$1"
9 |
--------------------------------------------------------------------------------
/contrib/iterate_over_pypi_dump.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env nix-shell
2 | #!nix-shell --pure -i bash -p jq git nix bubblewrap which cacert parallel tmux --keep BUILDXYZ_NIXPKGS --keep RUST_BACKTRACE
3 | # shellcheck shell=sh
4 |
5 | JOB="${1:-pypi-job}"
6 |
7 | generic_buildxyz() {
8 | builder="$1"
9 | package="$2"
10 | echo "buildxyz $package"
11 | # This is needed for the new tmpfs
12 | export TMPDIR="/buildxyz"
13 | # CAP_SYS_ADMIN is for the fusermount
14 | # /dev bind is for /dev/fuse
15 | # --share-net is necessary for network interactions.
16 | # share also the DNS resolver.
17 | bwrap \
18 | --ro-bind /etc/resolv.conf /etc/resolv.conf \
19 | --share-net \
20 | --bind /nix /nix \
21 | --dev-bind /dev /dev \
22 | --ro-bind $(which git) $(which git) \
23 | --ro-bind $(pwd)/target $(pwd)/target \
24 | --bind $(pwd)/examples $(pwd)/examples \
25 | --tmpfs /buildxyz \
26 | --proc /proc \
27 | --unshare-pid \
28 | --cap-add CAP_SYS_ADMIN \
29 | --new-session \
30 | ./target/debug/buildxyz --automatic --record-to "examples/python/$package.toml" "$builder $package"
31 | }
32 |
33 | pip_install() {
34 | package="$1"
35 | pip install "$package" --prefix /tmp --no-binary :all
36 | }
37 |
38 | pypi_buildxyz() {
39 | generic_buildxyz pip_install "$@"
40 | }
41 |
42 | export -f pypi_buildxyz
43 |
44 | readarray -t PYPI_PACKAGES < <(jq -rc '.rows | .[] | .project' top-pypi.json)
45 | parallel --joblog $JOB --progress --bar --delay 2.5 --jobs 50% --tmux pypi_buildxyz ::: "${PYPI_PACKAGES[@]}"
46 |
--------------------------------------------------------------------------------
/contrib/pack_from_pypi.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env nix-shell
2 | #!nix-shell --pure -i bash -p gcc jq git nix bubblewrap which cacert strace --keep BUILDXYZ_NIXPKGS --keep RUST_BACKTRACE --keep MANUAL --keep ENABLE_STRACE --keep NIX_DEBUG
3 | # shellcheck shell=sh
4 |
5 | buildxyz_global_flags=()
6 | STRACE=""
7 |
8 | if [[ -v ENABLE_STRACE ]]; then
9 | STRACE="strace -yy -e file -f"
10 | fi
11 |
12 | if [[ ! -v MANUAL ]]; then
13 | buildxyz_global_flags+=(--automatic)
14 | fi
15 |
16 | pypi_buildxyz() {
17 | package="$1"
18 | echo "buildxyz $package"
19 | export TMPDIR="/buildxyz"
20 | bwrap \
21 | --ro-bind /etc/resolv.conf /etc/resolv.conf \
22 | --share-net \
23 | --bind /nix /nix \
24 | --dev-bind /dev /dev \
25 | --ro-bind $(which git) $(which git) \
26 | --ro-bind $(pwd)/target $(pwd)/target \
27 | --bind $(pwd)/examples $(pwd)/examples \
28 | --tmpfs /buildxyz \
29 | --proc /proc \
30 | --unshare-pid \
31 | --cap-add CAP_SYS_ADMIN \
32 | --new-session \
33 | $STRACE ./target/debug/buildxyz "${buildxyz_global_flags[@]}" --record-to "examples/python/$package.toml" "pip install --verbose $package --prefix /tmp --no-binary :all:"
34 | }
35 |
36 | pypi_buildxyz "$1"
37 |
--------------------------------------------------------------------------------
/data/autotools/m4.toml:
--------------------------------------------------------------------------------
1 | ["share/aclocal/ax_zoneinfo.m4"]
2 | decision = "provide"
3 | file_entry_name = "/share/aclocal/ax_zoneinfo.m4"
4 | kind = "symlink"
5 | resolution = "constant"
6 |
7 | ["share/aclocal/ax_zoneinfo.m4".store_path]
8 | hash = "0znwn1lw0268i7pj0rylhz9dnzbwijjn"
9 | name = "autoconf-archive-2022.09.03"
10 | store_dir = "/nix/store"
11 |
12 | ["share/aclocal/ax_zoneinfo.m4".store_path.origin]
13 | attr = "autoconf-archive"
14 | output = "out"
15 | system = "x86_64-linux"
16 | toplevel = true
17 |
--------------------------------------------------------------------------------
/data/glibc/endian.toml:
--------------------------------------------------------------------------------
1 | ["include/sys/endian.h"]
2 | decision = "ignore"
3 | resolution = "constant"
4 |
--------------------------------------------------------------------------------
/data/glibc/xlocale.toml:
--------------------------------------------------------------------------------
1 | ["include/xlocale.h"]
2 | decision = "ignore"
3 | resolution = "constant"
4 |
--------------------------------------------------------------------------------
/data/python/cffi.toml:
--------------------------------------------------------------------------------
1 | ["bin/lsb_release"]
2 | decision = "provide"
3 | file_entry_name = "/bin/lsb_release"
4 | kind = "symlink"
5 | resolution = "constant"
6 |
7 | ["bin/lsb_release".store_path]
8 | hash = "ji55v58cch94h30inpnrd5h5jra9fwd8"
9 | name = "lsb_release"
10 | store_dir = "/nix/store"
11 |
12 | ["bin/lsb_release".store_path.origin]
13 | attr = "lsb-release"
14 | output = "out"
15 | system = "x86_64-linux"
16 | toplevel = true
17 |
18 | ["bin/pip"]
19 | decision = "provide"
20 | file_entry_name = "/bin/pip"
21 | kind = "symlink"
22 | resolution = "constant"
23 |
24 | ["bin/pip".store_path]
25 | hash = "8gzj48mkr8i824a4ali025hk85gmfdzy"
26 | name = "python3.10-pip-22.3.1"
27 | store_dir = "/nix/store"
28 |
29 | ["bin/pip".store_path.origin]
30 | attr = "python310Packages.pip"
31 | output = "out"
32 | system = "x86_64-linux"
33 | toplevel = true
34 |
35 | ["bin/pkg-config"]
36 | decision = "provide"
37 | file_entry_name = "/bin/pkg-config"
38 | kind = "symlink"
39 | resolution = "constant"
40 |
41 | ["bin/pkg-config".store_path]
42 | hash = "prm4x2r6997idyjxajapk1177jczvjfj"
43 | name = "pkg-config-0.29.2"
44 | store_dir = "/nix/store"
45 |
46 | ["bin/pkg-config".store_path.origin]
47 | attr = "pkg-config-unwrapped"
48 | output = "out"
49 | system = "x86_64-linux"
50 | toplevel = true
51 |
52 | ["lib/pkgconfig/libffi.pc"]
53 | decision = "provide"
54 | file_entry_name = "/lib/pkgconfig/libffi.pc"
55 | kind = "symlink"
56 | resolution = "constant"
57 |
58 | ["lib/pkgconfig/libffi.pc".store_path]
59 | hash = "4j08mgygxhi9y3957hbwqn1bg02va18y"
60 | name = "libffi-3.4.4-dev"
61 | store_dir = "/nix/store"
62 |
63 | ["lib/pkgconfig/libffi.pc".store_path.origin]
64 | attr = "libffi"
65 | output = "dev"
66 | system = "x86_64-linux"
67 | toplevel = true
68 |
--------------------------------------------------------------------------------
/data/python/cryptography.toml:
--------------------------------------------------------------------------------
1 | ["bin/cargo"]
2 | decision = "provide"
3 | file_entry_name = "/bin/cargo"
4 | kind = "symlink"
5 | resolution = "constant"
6 |
7 | ["bin/cargo".store_path]
8 | hash = "qiklc9rr5jg1zck0rb7vj4wn1n7yxx67"
9 | name = "cargo-1.67.1"
10 | store_dir = "/nix/store"
11 |
12 | ["bin/cargo".store_path.origin]
13 | attr = "cargo"
14 | output = "out"
15 | system = "x86_64-linux"
16 | toplevel = true
17 |
18 | ["bin/lsb_release"]
19 | decision = "provide"
20 | file_entry_name = "/bin/lsb_release"
21 | kind = "symlink"
22 | resolution = "constant"
23 |
24 | ["bin/lsb_release".store_path]
25 | hash = "ji55v58cch94h30inpnrd5h5jra9fwd8"
26 | name = "lsb_release"
27 | store_dir = "/nix/store"
28 |
29 | ["bin/lsb_release".store_path.origin]
30 | attr = "lsb-release"
31 | output = "out"
32 | system = "x86_64-linux"
33 | toplevel = true
34 |
35 | ["bin/pip"]
36 | decision = "provide"
37 | file_entry_name = "/bin/pip"
38 | kind = "symlink"
39 | resolution = "constant"
40 |
41 | ["bin/pip".store_path]
42 | hash = "8gzj48mkr8i824a4ali025hk85gmfdzy"
43 | name = "python3.10-pip-22.3.1"
44 | store_dir = "/nix/store"
45 |
46 | ["bin/pip".store_path.origin]
47 | attr = "python310Packages.pip"
48 | output = "out"
49 | system = "x86_64-linux"
50 | toplevel = true
51 |
52 | ["bin/pkg-config"]
53 | decision = "provide"
54 | file_entry_name = "/bin/pkg-config"
55 | kind = "symlink"
56 | resolution = "constant"
57 |
58 | ["bin/pkg-config".store_path]
59 | hash = "prm4x2r6997idyjxajapk1177jczvjfj"
60 | name = "pkg-config-0.29.2"
61 | store_dir = "/nix/store"
62 |
63 | ["bin/pkg-config".store_path.origin]
64 | attr = "pkg-config-unwrapped"
65 | output = "out"
66 | system = "x86_64-linux"
67 | toplevel = true
68 |
69 | ["bin/rustc"]
70 | decision = "provide"
71 | file_entry_name = "/bin/rustc"
72 | kind = "symlink"
73 | resolution = "constant"
74 |
75 | ["bin/rustc".store_path]
76 | hash = "bxm02qp0lnm4sin3x78qrxp0gsbby1jb"
77 | name = "rustc-1.67.1"
78 | store_dir = "/nix/store"
79 |
80 | ["bin/rustc".store_path.origin]
81 | attr = "rustc"
82 | output = "out"
83 | system = "x86_64-linux"
84 | toplevel = true
85 |
86 | ["lib/pkgconfig/libffi.pc"]
87 | decision = "provide"
88 | file_entry_name = "/lib/pkgconfig/libffi.pc"
89 | kind = "symlink"
90 | resolution = "constant"
91 |
92 | ["lib/pkgconfig/libffi.pc".store_path]
93 | hash = "4j08mgygxhi9y3957hbwqn1bg02va18y"
94 | name = "libffi-3.4.4-dev"
95 | store_dir = "/nix/store"
96 |
97 | ["lib/pkgconfig/libffi.pc".store_path.origin]
98 | attr = "libffi"
99 | output = "dev"
100 | system = "x86_64-linux"
101 | toplevel = true
102 |
103 | ["lib/pkgconfig/openssl.pc"]
104 | decision = "provide"
105 | file_entry_name = "/lib/pkgconfig/openssl.pc"
106 | kind = "symlink"
107 | resolution = "constant"
108 |
109 | ["lib/pkgconfig/openssl.pc".store_path]
110 | hash = "fyiwac98bi86nd5qzz9bhvs1bvmmwgad"
111 | name = "openssl-3.0.8-dev"
112 | store_dir = "/nix/store"
113 |
114 | ["lib/pkgconfig/openssl.pc".store_path.origin]
115 | attr = "openssl"
116 | output = "dev"
117 | system = "x86_64-linux"
118 | toplevel = true
119 |
--------------------------------------------------------------------------------
/data/python/cython.toml:
--------------------------------------------------------------------------------
1 | ["bin/lsb_release"]
2 | decision = "provide"
3 | file_entry_name = "/bin/lsb_release"
4 | kind = "symlink"
5 | resolution = "constant"
6 |
7 | ["bin/lsb_release".store_path]
8 | hash = "ji55v58cch94h30inpnrd5h5jra9fwd8"
9 | name = "lsb_release"
10 | store_dir = "/nix/store"
11 |
12 | ["bin/lsb_release".store_path.origin]
13 | attr = "lsb-release"
14 | output = "out"
15 | system = "x86_64-linux"
16 | toplevel = true
17 |
18 | ["bin/pgen"]
19 | decision = "ignore"
20 | resolution = "constant"
21 |
22 | ["bin/pip"]
23 | decision = "provide"
24 | file_entry_name = "/bin/pip"
25 | kind = "symlink"
26 | resolution = "constant"
27 |
28 | ["bin/pip".store_path]
29 | hash = "8gzj48mkr8i824a4ali025hk85gmfdzy"
30 | name = "python3.10-pip-22.3.1"
31 | store_dir = "/nix/store"
32 |
33 | ["bin/pip".store_path.origin]
34 | attr = "python310Packages.pip"
35 | output = "out"
36 | system = "x86_64-linux"
37 | toplevel = true
38 |
--------------------------------------------------------------------------------
/default.nix:
--------------------------------------------------------------------------------
1 | { fuse3
2 | , macfuse-stubs
3 | , stdenv
4 | , pkg-config
5 | , openssl
6 | , zstd
7 | , cargo-flamegraph
8 | , rustPlatform
9 | , lib
10 | , runCommand
11 | , fetchurl
12 | , clippy
13 | , path
14 | , enableLint ? false
15 | }:
16 | let
17 | fuse = if stdenv.isDarwin then macfuse-stubs else fuse3;
18 | popcount-graph = builtins.fetchurl {
19 | url = "https://github.com/RaitoBezarius/buildxyz/releases/download/assets-0.1.0/popcount-graph.json";
20 | sha256 = "1xbhlcmb2laa9cp5qh9vsmmvzdifaqb7x7817ppjk1wx6gf2p02a";
21 | };
22 | nix-index-db = builtins.fetchurl {
23 | url = "https://github.com/RaitoBezarius/buildxyz/releases/download/assets-0.1.0/files";
24 | sha256 = "02igi3vkqg8hqwa9p03gyr6x2h99sz1gv2w4mzfw646qlckfh32p";
25 | };
26 | in
27 | rustPlatform.buildRustPackage
28 | {
29 | pname = "buildxyz";
30 | version = "0.0.1";
31 | src = runCommand "src" { } ''
32 | install -D ${./Cargo.toml} $out/Cargo.toml
33 | install -D ${./Cargo.lock} $out/Cargo.lock
34 | cp -r ${./src} $out/src
35 | ln -sf ${popcount-graph} $out/popcount-graph.json
36 | ln -sf ${nix-index-db} $out/nix-index-files
37 | '';
38 | # Use provided zstd rather than vendored one.
39 | ZSTD_SYS_USE_PKG_CONFIG = true;
40 | BUILDXYZ_NIXPKGS = path;
41 | BUILDXYZ_CORE_RESOLUTIONS = ./data;
42 |
43 | buildInputs = [ zstd fuse ];
44 | nativeBuildInputs = [ openssl cargo-flamegraph pkg-config ] ++ lib.optional enableLint clippy;
45 |
46 | shellHook = ''
47 | ln -s ${popcount-graph} popcount-graph.json
48 | ln -s ${nix-index-db} nix-index-files
49 | '';
50 |
51 | cargoLock = {
52 | lockFile = ./Cargo.lock;
53 | };
54 | meta = with lib; {
55 | description = "Provides build shell that can automatically figure out dependencies";
56 | homepage = "https://github.com/RaitoBezarius/buildxyz";
57 | license = licenses.mit;
58 | };
59 | } // lib.optionalAttrs enableLint {
60 | buildPhase = ''
61 | cargo clippy --all-targets --all-features -- -D warnings
62 | if grep -R 'dbg!' ./src; then
63 | echo "use of dbg macro found in code!"
64 | false
65 | fi
66 | '';
67 |
68 | installPhase = ''
69 | touch $out
70 | '';
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/flake.lock:
--------------------------------------------------------------------------------
1 | {
2 | "nodes": {
3 | "flake-parts": {
4 | "inputs": {
5 | "nixpkgs-lib": [
6 | "nixpkgs"
7 | ]
8 | },
9 | "locked": {
10 | "lastModified": 1677714448,
11 | "narHash": "sha256-Hq8qLs8xFu28aDjytfxjdC96bZ6pds21Yy09mSC156I=",
12 | "owner": "hercules-ci",
13 | "repo": "flake-parts",
14 | "rev": "dc531e3a9ce757041e1afaff8ee932725ca60002",
15 | "type": "github"
16 | },
17 | "original": {
18 | "owner": "hercules-ci",
19 | "repo": "flake-parts",
20 | "type": "github"
21 | }
22 | },
23 | "nixpkgs": {
24 | "locked": {
25 | "lastModified": 1677995890,
26 | "narHash": "sha256-eOnCn0o3I6LP48fAi8xWFcn49V2rL7oX5jCtJTeN1LI=",
27 | "owner": "NixOS",
28 | "repo": "nixpkgs",
29 | "rev": "a1240f6b4a0bcc84fc48008b396a140d9f3638f6",
30 | "type": "github"
31 | },
32 | "original": {
33 | "owner": "NixOS",
34 | "ref": "nixpkgs-unstable",
35 | "repo": "nixpkgs",
36 | "type": "github"
37 | }
38 | },
39 | "root": {
40 | "inputs": {
41 | "flake-parts": "flake-parts",
42 | "nixpkgs": "nixpkgs",
43 | "treefmt-nix": "treefmt-nix"
44 | }
45 | },
46 | "treefmt-nix": {
47 | "inputs": {
48 | "nixpkgs": [
49 | "nixpkgs"
50 | ]
51 | },
52 | "locked": {
53 | "lastModified": 1677433127,
54 | "narHash": "sha256-vafj2WbhrlnwkU20yRDqtHFTUJIEygPfxJVswB3dJ9U=",
55 | "owner": "numtide",
56 | "repo": "treefmt-nix",
57 | "rev": "f7fcf3770c6cec6fd5f995ba94e6e6376019b9ff",
58 | "type": "github"
59 | },
60 | "original": {
61 | "owner": "numtide",
62 | "repo": "treefmt-nix",
63 | "type": "github"
64 | }
65 | }
66 | },
67 | "root": "root",
68 | "version": 7
69 | }
70 |
--------------------------------------------------------------------------------
/flake.nix:
--------------------------------------------------------------------------------
1 | {
2 | description = "Development environment for this project";
3 |
4 | inputs = {
5 | nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
6 | treefmt-nix.url = "github:numtide/treefmt-nix";
7 | treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";
8 |
9 | flake-parts.url = "github:hercules-ci/flake-parts";
10 | flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs";
11 | };
12 |
13 | outputs = inputs@{ flake-parts, ... }:
14 | flake-parts.lib.mkFlake { inherit inputs; } ({ ... }: {
15 | systems = [
16 | "x86_64-linux"
17 | "aarch64-linux"
18 | # TODO: fix eval...
19 | #"riscv64-linux"
20 | "x86_64-darwin"
21 | "aarch64-darwin"
22 | ];
23 | imports = [
24 | ./treefmt/flake-module.nix
25 | ./tests/flake-module.nix
26 | ];
27 |
28 | perSystem = { self', pkgs, ... }: {
29 | packages.buildxyz = pkgs.callPackage ./default.nix { };
30 | packages.default = self'.packages.buildxyz;
31 | checks.clippy = self'.packages.buildxyz.override {
32 | enableLint = true;
33 | };
34 | };
35 | });
36 | }
37 |
--------------------------------------------------------------------------------
/garnix.yaml:
--------------------------------------------------------------------------------
1 | builds:
2 | exclude: []
3 | include:
4 | - "*.x86_64-linux.*"
5 | - "nixosConfigurations.*"
6 | - "packages.aarch64-darwin.*"
7 | - "devShells.aarch64-darwin.*"
8 |
--------------------------------------------------------------------------------
/src/cache/database.rs:
--------------------------------------------------------------------------------
1 | use std::fs::File;
2 | use std::io::Cursor;
3 | /// Creating and searching file databases.
4 | ///
5 | /// This module implements an abstraction for creating an index of files with meta information
6 | /// and searching that index for paths matching a specific pattern.
7 | use std::io::{self, BufReader, BufWriter, Read, Seek, SeekFrom, Write};
8 | use std::path::Path;
9 |
10 | use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
11 | use error_chain::error_chain;
12 | use grep::matcher::{LineMatchKind, Match, Matcher, NoError};
13 | use grep::{self};
14 | use memchr::{memchr, memrchr};
15 | use regex::bytes::Regex;
16 | use regex_syntax::ast::{
17 | Alternation, Assertion, AssertionKind, Ast, Concat, Group, Literal, Repetition,
18 | };
19 | use serde_json;
20 | use zstd;
21 |
22 | use crate::cache::files::{FileTree, FileTreeEntry};
23 | use crate::cache::frcode;
24 | use crate::cache::package::StorePath;
25 |
26 | /// The version of the database format supported by this nix-index version.
27 | ///
28 | /// This should be updated whenever you make an incompatible change to the database format.
29 | const FORMAT_VERSION: u64 = 1;
30 |
31 | /// The magic for nix-index database files, used to ensure that the file we're passed is
32 | /// actually a file generated by nix-index.
33 | const FILE_MAGIC: &'static [u8] = b"NIXI";
34 |
35 | /// A writer for creating a new file database.
36 | pub struct Writer {
37 | /// The encoder used to compress the database. Will be set to `None` when the value
38 | /// is dropped.
39 | writer: Option>>,
40 | }
41 |
42 | // We need to make sure that the encoder is `finish`ed in all cases, so we need
43 | // a custom Drop.
44 | impl Drop for Writer {
45 | fn drop(&mut self) {
46 | if self.writer.is_some() {
47 | self.finish_encoder().unwrap();
48 | }
49 | }
50 | }
51 |
52 | impl Writer {
53 | /// Creates a new database at the given path with the specified zstd compression level
54 | /// (currently, supported values range from 0 to 22).
55 | pub fn create>(path: P, level: i32) -> io::Result {
56 | let mut file = File::create(path)?;
57 | file.write_all(FILE_MAGIC)?;
58 | file.write_u64::(FORMAT_VERSION)?;
59 | let mut encoder = zstd::Encoder::new(file, level)?;
60 | encoder.multithread(num_cpus::get() as u32)?;
61 |
62 | Ok(Writer {
63 | writer: Some(BufWriter::new(encoder)),
64 | })
65 | }
66 |
67 | /// Add a new package to the database for the given store path with its corresponding
68 | /// file tree. Entries are only added if they match `filter_prefix`.
69 | pub fn add(
70 | &mut self,
71 | path: StorePath,
72 | files: FileTree,
73 | filter_prefix: &[u8],
74 | ) -> io::Result<()> {
75 | let writer = self.writer.as_mut().expect("not dropped yet");
76 | let mut encoder =
77 | frcode::Encoder::new(writer, b"p".to_vec(), serde_json::to_vec(&path).unwrap());
78 | for entry in files.to_list(filter_prefix) {
79 | entry.encode(&mut encoder)?;
80 | }
81 | Ok(())
82 | }
83 |
84 | /// Finishes encoding. After calling this function, `add` may no longer be called, since this function
85 | /// closes the stream.
86 | ///
87 | /// The return value is the underlying File.
88 | fn finish_encoder(&mut self) -> io::Result {
89 | let writer = self.writer.take().expect("not dropped yet");
90 | let encoder = writer.into_inner()?;
91 | encoder.finish()
92 | }
93 |
94 | /// Finish the encoding and return the size in bytes of the compressed file that was created.
95 | pub fn finish(mut self) -> io::Result {
96 | let mut file = self.finish_encoder()?;
97 | file.seek(SeekFrom::Current(0))
98 | }
99 | }
100 |
101 | error_chain! {
102 | errors {
103 | UnsupportedFileType(found: Vec) {
104 | description("unsupported file type")
105 | display("expected file to start with nix-index file magic 'NIXI', but found '{}' (is this a valid nix-index database file?)", String::from_utf8_lossy(found))
106 | }
107 | UnsupportedVersion(found: u64) {
108 | description("unsupported file version")
109 | display("this executable only supports the nix-index database version {}, but found a database with version {}", FORMAT_VERSION, found)
110 | }
111 | MissingPackageEntry {
112 | description("missing package entry for path")
113 | display("database corrupt, found a file entry without a matching package entry")
114 | }
115 | Frcode(err: frcode::Error) {
116 | description("frcode error")
117 | display("database corrupt, frcode error: {}", err)
118 | }
119 | EntryParse(entry: Vec) {
120 | description("entry parse failure")
121 | display("database corrupt, could not parse entry: {:?}", String::from_utf8_lossy(entry))
122 | }
123 | StorePathParse(path: Vec) {
124 | description("store path parse failure")
125 | display("database corrupt, could not parse store path: {:?}", String::from_utf8_lossy(path))
126 | }
127 | }
128 |
129 | foreign_links {
130 | Io(io::Error);
131 | Grep(grep::regex::Error);
132 | }
133 | }
134 |
135 | impl From for Error {
136 | fn from(err: frcode::Error) -> Error {
137 | ErrorKind::Frcode(err).into()
138 | }
139 | }
140 |
141 | /// A Reader allows fast querying of a nix-index database.
142 | pub struct Reader {
143 | decoder: frcode::Decoder>>, // BufReader>>>,
144 | }
145 |
146 | pub fn read_from_path>(path: P) -> Result> {
147 | read_raw_buffer(File::open(path)?)
148 | }
149 |
150 | pub fn read_raw_buffer(mut reader: Reader) -> Result> {
151 | let mut magic = [0u8; 4];
152 | reader.read_exact(&mut magic)?;
153 |
154 | if magic != FILE_MAGIC {
155 | return Err(ErrorKind::UnsupportedFileType(magic.to_vec()).into());
156 | }
157 |
158 | let version = reader.read_u64::()?;
159 | if version != FORMAT_VERSION {
160 | return Err(ErrorKind::UnsupportedVersion(version).into());
161 | }
162 |
163 | let mut decoder = zstd::Decoder::new(reader)?;
164 | let mut buffer: Vec = Vec::new();
165 | decoder.read_to_end(&mut buffer)?;
166 |
167 | Ok(buffer)
168 | }
169 |
170 | impl Reader {
171 | /// Opens a nix-index database located at the given path.
172 | ///
173 | /// If the path does not exist or is not a valid database, an error is returned.
174 | pub fn open>(path: P) -> Result {
175 | Reader::from_buffer(read_from_path(path)?)
176 | }
177 |
178 | pub fn from_buffer(buffer: Vec) -> Result {
179 | Ok(Reader {
180 | decoder: frcode::Decoder::new(Cursor::new(buffer)),
181 | })
182 | }
183 |
184 | /// Builds a query to find all entries in the database that have a filename matching the given pattern.
185 | ///
186 | /// Afterwards, use `Query::into_iter` to iterate over the items.
187 | pub fn query(self, exact_regex: &Regex) -> Query {
188 | Query {
189 | reader: self,
190 | exact_regex,
191 | hash: None,
192 | package_pattern: None,
193 | }
194 | }
195 |
196 | /// Dumps the contents of the database to stdout, for debugging.
197 | #[allow(clippy::print_stdout)]
198 | pub fn dump(&mut self) -> Result<()> {
199 | loop {
200 | let block = self.decoder.decode()?;
201 | if block.is_empty() {
202 | break;
203 | }
204 | for line in block.split(|c| *c == b'\n') {
205 | println!("{:?}", String::from_utf8_lossy(line));
206 | }
207 | println!("-- block boundary");
208 | }
209 | Ok(())
210 | }
211 | }
212 |
213 | /// A builder for a `ReaderIter` to iterate over entries in the database matching a given pattern.
214 | pub struct Query<'a, 'b> {
215 | /// The underlying reader from which we read input.
216 | reader: Reader,
217 |
218 | /// The pattern that file paths have to match.
219 | exact_regex: &'a Regex,
220 |
221 | /// Only include the package with the given hash.
222 | hash: Option,
223 |
224 | /// Only include packages whose name matches the given pattern.
225 | package_pattern: Option<&'b Regex>,
226 | }
227 |
228 | impl<'a, 'b> Query<'a, 'b> {
229 | /// Limit results to entries from the package with the specified hash if `Some`.
230 | pub fn hash(self, hash: Option) -> Query<'a, 'b> {
231 | Query { hash, ..self }
232 | }
233 |
234 | /// Limit results to entries from packages whose name matches the given regex if `Some`.
235 | pub fn package_pattern(self, package_pattern: Option<&'b Regex>) -> Query<'a, 'b> {
236 | Query {
237 | package_pattern,
238 | ..self
239 | }
240 | }
241 |
242 | /// Runs the query, returning an Iterator that will yield all entries matching the conditions.
243 | ///
244 | /// There is no guarantee about the order of the returned matches.
245 | pub fn run(self) -> Result> {
246 | let mut expr = regex_syntax::ast::parse::Parser::new()
247 | .parse(self.exact_regex.as_str())
248 | .expect("regex cannot be invalid");
249 | // replace the ^ anchor by a NUL byte, since each entry is of the form `METADATA\0PATH`
250 | // (so the NUL byte marks the start of the path).
251 | {
252 | let mut stack = vec![&mut expr];
253 | while let Some(e) = stack.pop() {
254 | match *e {
255 | Ast::Assertion(Assertion {
256 | kind: AssertionKind::StartLine,
257 | span,
258 | }) => {
259 | *e = Ast::Literal(Literal {
260 | span,
261 | c: '\0',
262 | kind: regex_syntax::ast::LiteralKind::Verbatim,
263 | })
264 | }
265 | Ast::Group(Group { ref mut ast, .. }) => stack.push(ast),
266 | Ast::Repetition(Repetition { ref mut ast, .. }) => stack.push(ast),
267 | Ast::Concat(Concat { ref mut asts, .. })
268 | | Ast::Alternation(Alternation { ref mut asts, .. }) => stack.extend(asts),
269 | _ => {}
270 | }
271 | }
272 | }
273 | let mut regex_builder = grep::regex::RegexMatcherBuilder::new();
274 | regex_builder.line_terminator(Some(b'\n')).multi_line(true);
275 |
276 | let grep = regex_builder.build(&format!("{}", expr))?;
277 | Ok(ReaderIter {
278 | reader: self.reader,
279 | found: Vec::new(),
280 | found_without_package: Vec::new(),
281 | pattern: grep,
282 | exact_pattern: self.exact_regex,
283 | package_entry_pattern: regex_builder.build("^p\0").expect("valid regex"),
284 | package_name_pattern: self.package_pattern,
285 | package_hash: self.hash,
286 | })
287 | }
288 | }
289 |
290 | /// An iterator for entries in a database matching a given pattern.
291 | pub struct ReaderIter<'a, 'b> {
292 | /// The underlying reader from which we read input.
293 | reader: Reader,
294 | /// Entries that matched the pattern but have not been returned by `next` yet.
295 | found: Vec<(StorePath, FileTreeEntry)>,
296 | /// Entries that matched the pattern but for which we don't know yet what package they belong to.
297 | /// This may happen if the entry we matched was at the end of the search buffer, so that the entry
298 | /// for the package did not fit into the buffer anymore (since the package is stored after the entries
299 | /// of the package). In this case, we need to look for the package entry in the next iteration when
300 | /// we read the next block of input.
301 | found_without_package: Vec,
302 | /// The pattern for which to search package paths.
303 | ///
304 | /// This pattern should work on the raw bytes of file entries. In particular, the file path is not the
305 | /// first data in a file entry, so the regex `^` anchor will not work correctly.
306 | ///
307 | /// The pattern here may produce false positives (for example, if it matches inside the metadata of a file
308 | /// entry). This is not a problem, as matches are later checked against `exact_pattern`.
309 | pattern: grep::regex::RegexMatcher,
310 | /// The raw pattern, as supplied to `find_iter`. This is used to verify matches, since `pattern` itself
311 | /// may produce false positives.
312 | exact_pattern: &'a Regex,
313 | /// Pattern that matches only package entries.
314 | package_entry_pattern: grep::regex::RegexMatcher,
315 | /// Pattern that the package name should match.
316 | package_name_pattern: Option<&'b Regex>,
317 | /// Only search the package with the given hash.
318 | package_hash: Option,
319 | }
320 |
321 | fn consume_no_error(e: NoError) -> T {
322 | panic!("impossible: {}", e)
323 | }
324 |
325 | fn next_matching_line>(
326 | matcher: M,
327 | buf: &[u8],
328 | mut start: usize,
329 | ) -> Option {
330 | while let Some(candidate) = matcher
331 | .find_candidate_line(&buf[start..])
332 | .unwrap_or_else(consume_no_error)
333 | {
334 | // the buffer may end with a newline character, so we may get a match
335 | // for an empty "line" at the end of the buffer
336 | // since this is not a line match, return None
337 | if start == buf.len() {
338 | return None;
339 | };
340 |
341 | let (pos, confirmed) = match candidate {
342 | LineMatchKind::Confirmed(pos) => (start + pos, true),
343 | LineMatchKind::Candidate(pos) => (start + pos, false),
344 | };
345 |
346 | let line_start = memrchr(b'\n', &buf[..pos]).map(|x| x + 1).unwrap_or(0);
347 | let line_end = memchr(b'\n', &buf[pos..])
348 | .map(|x| x + pos + 1)
349 | .unwrap_or(buf.len());
350 |
351 | if !confirmed
352 | && !matcher
353 | .is_match(&buf[line_start..line_end])
354 | .unwrap_or_else(consume_no_error)
355 | {
356 | start = line_end;
357 | continue;
358 | }
359 |
360 | return Some(Match::new(line_start, line_end));
361 | }
362 | None
363 | }
364 |
365 | impl<'a, 'b> ReaderIter<'a, 'b> {
366 | /// Reads input until `self.found` contains at least one entry or the end of the input has been reached.
367 | #[allow(unused_assignments)] // because of https://github.com/rust-lang/rust/issues/22630
368 | fn fill_buf(&mut self) -> Result<()> {
369 | // the input is processed in blocks until we've found at least a single entry
370 | while self.found.is_empty() {
371 | let &mut ReaderIter {
372 | ref mut reader,
373 | ref package_entry_pattern,
374 | ref package_name_pattern,
375 | ref package_hash,
376 | ..
377 | } = self;
378 | let block = reader.decoder.decode()?;
379 |
380 | // if the block is empty, the end of input has been reached
381 | if block.is_empty() {
382 | return Ok(());
383 | }
384 |
385 | // when we find a match, we need to know the package that this match belongs to.
386 | // the `find_package` function will skip forward until a package entry is found
387 | // (the package entry comes after all file entries for a package).
388 | //
389 | // to be more efficient if there are many matches, we cache the current package here.
390 | // this package is valid for all positions up to the second element of the tuple
391 | // (after that, a new package begins).
392 | let mut cached_package: Option<(StorePath, usize)> = None;
393 | let mut no_more_package = false;
394 | let mut find_package = |item_end| -> Result<_> {
395 | if let Some((ref pkg, end)) = cached_package {
396 | if item_end < end {
397 | return Ok(Some((pkg.clone(), end)));
398 | }
399 | }
400 |
401 | if no_more_package {
402 | return Ok(None);
403 | }
404 |
405 | let mat = match next_matching_line(&package_entry_pattern, &block, item_end) {
406 | Some(v) => v,
407 | None => {
408 | no_more_package = true;
409 | return Ok(None);
410 | }
411 | };
412 |
413 | let json = &block[mat.start() + 2..mat.end() - 1];
414 | let pkg: StorePath = serde_json::from_slice(json)
415 | .chain_err(|| ErrorKind::StorePathParse(json.to_vec()))?;
416 | cached_package = Some((pkg.clone(), mat.end()));
417 | Ok(Some((pkg, mat.end())))
418 | };
419 |
420 | // Tests if a store path matches the `package_name_pattern` and `package_hash` constraints.
421 | let should_search_package = |pkg: &StorePath| -> bool {
422 | package_name_pattern.map_or(true, |r| r.is_match(pkg.name().as_bytes()))
423 | && package_hash.as_ref().map_or(true, |h| h == &pkg.hash())
424 | };
425 |
426 | let mut pos = 0;
427 | // if there are any entries without a package left over from the previous iteration, see
428 | // if this block contains the package entry.
429 | if !self.found_without_package.is_empty() {
430 | if let Some((pkg, end)) = find_package(0)? {
431 | if !should_search_package(&pkg) {
432 | // all entries before end will have the same package
433 | pos = end;
434 | self.found_without_package.truncate(0);
435 | } else {
436 | for entry in self.found_without_package.split_off(0) {
437 | self.found.push((pkg.clone(), entry));
438 | }
439 | }
440 | }
441 | }
442 |
443 | // process all matches in this block
444 | while let Some(mat) = next_matching_line(&self.pattern, &block, pos) {
445 | pos = mat.end();
446 | let entry = &block[mat.start()..mat.end() - 1];
447 | // skip entries that aren't describing file paths
448 | if self
449 | .package_entry_pattern
450 | .is_match(entry)
451 | .unwrap_or_else(consume_no_error)
452 | {
453 | continue;
454 | }
455 |
456 | // skip if package name or hash doesn't match
457 | // we can only skip if we know the package
458 | if let Some((pkg, end)) = find_package(mat.end())? {
459 | if !should_search_package(&pkg) {
460 | // all entries before end will have the same package
461 | pos = end;
462 | continue;
463 | }
464 | }
465 |
466 | let entry = FileTreeEntry::decode(entry)
467 | .ok_or_else(|| Error::from(ErrorKind::EntryParse(entry.to_vec())))?;
468 |
469 | // check for false positives
470 | if !self.exact_pattern.is_match(&entry.path) {
471 | continue;
472 | }
473 |
474 | match find_package(mat.end())? {
475 | None => self.found_without_package.push(entry),
476 | Some((pkg, _)) => self.found.push((pkg, entry)),
477 | }
478 | }
479 | }
480 | Ok(())
481 | }
482 |
483 | /// Returns the next match in the database.
484 | fn next_match(&mut self) -> Result