https://tvl.fyi/ TVL blog 2024-08-09T16:02:50+00:00 tazjin Thoughts and news from The Virus Lounge Tvix Status - August '24 https://tvl.fyi/blog/tvix-update-august-24 2024-08-09T16:02:50+00:00 2024-08-09T16:02:50+00:00 <p>It's already been around half a year since <a href="https://tvl.fyi/blog/tvix-update-february-24">the last Tvix update</a>, so time for another one!</p> <p>Note: This blog post is intended for a technical audience that is already intimately familiar with Nix, and knows what things like derivations or store paths are. If you're new to Nix, this will not make a lot of sense to you!</p> <h2><a href="#builds" aria-hidden="true" class="anchor" id="builds"></a>Builds</h2> <p>A long-term goal is obviously to be able to use the expressions in nixpkgs to build things with Tvix. We made progress on many places towards that goal:</p> <h3><a href="#drive-builds-on-io" aria-hidden="true" class="anchor" id="drive-builds-on-io"></a>Drive builds on IO</h3> <p>As already explained in our <a href="https://tvl.fyi/blog/rewriting-nix">first blog post</a>, in Tvix, we want to make IFD a first-class citizen without significant perf cost.</p> <p>Nix tries hard to split Evaluation and Building into two phases, visible in the <code>nix-instantiate</code> command which produces <code>.drv</code> files in <code>/nix/store</code> and the <code>nix-build</code> command which can be invoked on such <code>.drv</code> files without evaluation. Scheduling (like in Hydra) usually happens by walking the graph of <code>.drv</code> files produced in the first phase.</p> <p>As soon as there's some IFD along the path, everything until then gets built in the Evaluator (which is why IFD is prohibited in nixpkgs).</p> <p>Tvix does not have two separate &quot;phases&quot; in a build, only a graph of unfinished Derivations/Builds and their associated store paths. This graph does not need to be written to disk, and can grow during runtime, as new Derivations with new output paths are discovered.</p> <p>Build scheduling happens continuously with that graph, for everything that's really needed, when it's needed.</p> <p>We do this by only &quot;forcing&quot; the realization of a specific store path if the user ultimately wants that specific result to be available on their system, and transitively, if something else wants it. This includes IFD in a very elegant way.</p> <p>We want to play with this approach as we continue on bringing our build infrastructure up.</p> <h3><a href="#fetchers" aria-hidden="true" class="anchor" id="fetchers"></a>Fetchers</h3> <p>There's a few Nix builtins that allow describing a fetch (be it download of a file from the internet, clone of a git repo). These needed to be implemented for completeness. We implemented pretty much all downloads of Tarballs, NARs and plain files, except git repositories, which are left for later.</p> <p>Instead of doing these fetches immediately, we added a generic <code>Fetch</code> type that allows describing such fetches <em>before actually doing them</em>, similar to being able to describe builds, and use the same &quot;Drive builds on IO&quot; machinery to delay these fetches to the point where it's needed. We also show progress bars when doing fetches.</p> <p>Very early, during bootstrapping, nixpkgs relies on some <code>builtin:fetchurl</code> &quot;fake&quot; Derivation, which has some special handling logic in Nix. We implemented these quirks, by converting it to instances of our <code>Fetch</code> type and dealing with it there in a consistent fashion.</p> <h3><a href="#more-fixes-refscan" aria-hidden="true" class="anchor" id="more-fixes-refscan"></a>More fixes, Refscan</h3> <p>With the above work done, and after fixing some small bugs <sup class="footnote-ref"><a href="#fn1" id="fnref1">1</a></sup>, we were already able to build some first few store paths with Tvix and our <code>runc</code>-based builder 🎉!</p> <p>We didn't get too far though, as we still need to implement reference scanning, so that's next on our TODO list for here. Stay tuned for further updates there!</p> <h2><a href="#eval-correctness--performance" aria-hidden="true" class="anchor" id="eval-correctness--performance"></a>Eval correctness &amp; Performance</h2> <p>As already written in the previous update, we've been evaluating parts of <code>nixpkgs</code> and ensuring we produce the same derivations. We managed to find and fix some correctness issues there.</p> <p>Even though we don't want to focus too much on performance improvements until all features of Nix are properly understood and representable with our architecture, there's been some work on removing some obvious and low-risk performance bottlenecks. Expect a detailed blog post around that soon after this one!</p> <h2><a href="#tracing--o11y-support" aria-hidden="true" class="anchor" id="tracing--o11y-support"></a>Tracing / O11Y Support</h2> <p>Tvix got support for Tracing, and is able to emit spans in <a href="https://opentelemetry.io/">OpenTelemetry</a>-compatible format.</p> <p>This means, if the necessary tooling is set up to collect such spans <sup class="footnote-ref"><a href="#fn2" id="fnref2">2</a></sup>, it's possible to see what's happening inside the different components of Tvix across process (and machine) boundaries.</p> <p>Tvix now also propagates trace IDs via gRPC and HTTP requests <sup class="footnote-ref"><a href="#fn3" id="fnref3">3</a></sup>, and continues them if receiving such ones.</p> <p>As an example, this allows us to get &quot;callgraphs&quot; on how a tvix-store operation is processed through a multi-node deployment, and find bottlenecks and places to optimize performance for.</p> <p>Currently, this is compiled in by default, trying to send traces to an endpoint at <code>localhost</code> (as per the official <a href="https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/">SDK defaults</a>). It can be disabled by building without the <code>otlp</code> feature, or running with the <code>--otlp=false</code> CLI flag.</p> <p>This piggy-backs on the excellent <a href="https://tracing.rs/">tracing</a> crate, which we already use for structured logging, so while at it, we improved some log messages and fields to make it easier to filter for certain types of events.</p> <p>We also added support for sending out <a href="https://github.com/wolfpld/tracy">Tracy</a> traces, though these are disabled by default.</p> <p>Additionally, some CLI entrypoints can now report progress to the user! For example, when we're fetching something during evaluation (via <code>builtins.fetchurl</code>), or uploading store path contents, we can report on this. See <a href="https://asciinema.org/a/Fs4gKTFFpPGYVSna0xjTPGaNp">here</a> for an example.</p> <p>We're still considering these outputs as early prototypes, and will refine them as we go.</p> <h2><a href="#tvix-castore-ingestion-generalization" aria-hidden="true" class="anchor" id="tvix-castore-ingestion-generalization"></a>tvix-castore ingestion generalization</h2> <p>We spent some time refactoring and generalizing tvix-castore importer code.</p> <p>It's now generalized on a stream of &quot;ingestion entries&quot; produced in a certain order, and there's various producers of this stream (reading through the local filesystem, reading through a NAR, reading through a tarball, soon: traversing contents of a git repo, …).</p> <p>This prevented a lot of code duplication for these various formats, and allows pulling out helper code for concurrent blob uploading.</p> <h2><a href="#more-tvix-castore-backends" aria-hidden="true" class="anchor" id="more-tvix-castore-backends"></a>More tvix-[ca]store backends</h2> <p>We added some more store backends to Tvix:</p> <ul> <li>There's a <a href="https://github.com/cberner/redb">redb</a> <code>PathInfoService</code> and <code>DirectoryService</code>, which also replaced the previous <code>sled</code> default backend.</li> <li>There's a <a href="https://cloud.google.com/bigtable">bigtable</a> <code>PathInfoService</code> and <code>DirectoryService</code> backend.</li> <li>The &quot;simplefs&quot; <code>BlobService</code> has been removed, as it can be expressed using the &quot;objectstore&quot; backend with a <code>file://</code> URI.</li> <li>There's been some work on feature-flagging certain backends.</li> </ul> <h2><a href="#documentation-reconcilation" aria-hidden="true" class="anchor" id="documentation-reconcilation"></a>Documentation reconcilation</h2> <p>Various bits and pieces of documentation have previously been scattered throughout the Tvix codebase, which wasn't very accessible and quite confusing.</p> <p>These have been consolidated into a mdbook (at <code>//tvix/docs</code>).</p> <p>We plan to properly host these as a website, hopefully providing a better introduction and overview of Tvix, while adding more content over time.</p> <h2><a href="#nar-bridge-riir" aria-hidden="true" class="anchor" id="nar-bridge-riir"></a><code>nar-bridge</code> RIIR</h2> <p>While the golang implementation of <code>nar-bridge</code> did serve us well for a while, it being the only remaining non-Rust part was a bit annoying.</p> <p>Adding some features there meant they would not be accessible in the rest of Tvix - and the other way round. Also, we could not open data stores directly from there, but always had to start a separate <code>tvix-store daemon</code>.</p> <p>The initial plans for the Rust rewrite were already made quite a while ago, but we finally managed to finish implementing the remaining bits. <code>nar-bridge</code> is now fully written in Rust, providing the same CLI experience features and store backends as the rest of Tvix.</p> <h2><a href="#crate2nix-and-overall-rust-nix-improvements" aria-hidden="true" class="anchor" id="crate2nix-and-overall-rust-nix-improvements"></a><code>crate2nix</code> and overall rust Nix improvements</h2> <p>We landed some fixes in <a href="https://github.com/nix-community/crate2nix">crate2nix</a>, the tool we're using to for per-crate incremental builds of Tvix.</p> <p>It now supports the corner cases needed to build WASM - so now <a href="https://bolt.tvix.dev/">Tvixbolt</a> is built with it, too.</p> <p>We also fixed some bugs in how test directories are prepared, which unlocked running some more tests for filesystem related builtins such as <code>readDir</code> in our test suite.</p> <p>Additionally, there has been some general improvements around ensuring various combinations of Tvix feature flags build (now continuously checked by CI), and reducing the amount of unnecessary rebuilds, by filtering non-sourcecode files before building.</p> <p>These should all improve DX while working on Tvix.</p> <h2><a href="#store-composition" aria-hidden="true" class="anchor" id="store-composition"></a>Store Composition</h2> <p>Another big missing feature that landed was Store Composition. We briefly spoke about the Tvix Store Model in the last update, but we didn't go into too much detail on how that'd work in case there's multiple potential sources for a store path or some more granular contents (which is pretty much always the case normally, think about using things from your local store OR then falling back to a remote place).</p> <p>Nix has the default model of using <code>/nix/store</code> with a sqlite database for metadata as a local store, and one or multiple &quot;subsituters&quot; using the Nix HTTP Binary Cache protocol.</p> <p>In Tvix, things need to be a bit more flexible:</p> <ul> <li>You might be in a setting where you don't have a local <code>/nix/store</code> at all.</li> <li>You might want to have a view of different substituters/binary caches for different users.</li> <li>You might want to explicitly specify caches in between some of these layers, and control their config.</li> </ul> <p>The idea in Tvix is that you'll be able to combine &quot;hierarchies of stores&quot; through runtime configuration to express all this.</p> <p>It's currently behind a <code>xp-store-composition</code> feature flag, which adds the optional <code>--experimental-store-composition</code> CLI arg, pointing to a TOML file specifying the composition configuration. If set, this has priority over the old CLI args for the three (single) stores.</p> <p>We're still not 100% sure how to best expose this functionality, in terms of the appropriate level of granularity, in a user-friendly format.</p> <p>There's also some more combinators and refactors missing, but please let us know your thoughts!</p> <h2><a href="#contributors" aria-hidden="true" class="anchor" id="contributors"></a>Contributors</h2> <p>There's been a lot of progress, which would not have been possible without our contributors! Be it a small drive-by contributions, or large efforts, thank you all!</p> <ul> <li>Adam Joseph</li> <li>Alice Carroll</li> <li>Aspen Smith</li> <li>Ben Webb</li> <li>binarycat</li> <li>Brian Olsen</li> <li>Connor Brewster</li> <li>Daniel Mendler</li> <li>edef</li> <li>Edwin Mackenzie-Owen</li> <li>espes</li> <li>Farid Zakaria</li> <li>Florian Klink</li> <li>Ilan Joselevich</li> <li>Luke Granger-Brown</li> <li>Markus Rudy</li> <li>Matthew Tromp</li> <li>Moritz Sanft</li> <li>Padraic-O-Mhuiris</li> <li>Peter Kolloch</li> <li>Picnoir</li> <li>Profpatsch</li> <li>Ryan Lahfa</li> <li>Simon Hauser</li> <li>sinavir</li> <li>sterni</li> <li>Steven Allen</li> <li>tcmal</li> <li>toastal</li> <li>Vincent Ambo</li> <li>Yureka</li> </ul> <hr /> <p>That's it again, try out Tvix and hit us up on IRC or on our mailing list if you run into any snags, or have any questions.</p> <section class="footnotes"> <ol> <li id="fn1"> <p>like <code>builtins.toFile</code> not adding files yet, or <code>inputSources</code> being missed initially, duh!) <a href="#fnref1" class="footnote-backref">↩</a></p> </li> <li id="fn2"> <p>Essentially, deploying a collecting agent on your machines, accepting these traces. <a href="#fnref2" class="footnote-backref">↩</a></p> </li> <li id="fn3"> <p>Using the <code>traceparent</code> header field from <a href="https://www.w3.org/TR/trace-context/#trace-context-http-headers-format">https://www.w3.org/TR/trace-context/#trace-context-http-headers-format</a> <a href="#fnref3" class="footnote-backref">↩</a></p> </li> </ol> </section> Tvix Status - February '24 https://tvl.fyi/blog/tvix-update-february-24 2024-02-09T09:48:52+00:00 2024-02-09T09:48:52+00:00 <p>We've now been working on our rewrite of Nix, <a href="https://tvix.dev">Tvix</a>, for a little more than two years.</p> <p>Our last written update was in September 2023, and although we did publish a couple of things in the meantime (flokli's talk on Tvix at <a href="https://www.youtube.com/watch?v=j67prAPYScY">NixCon 2023</a>, our interview at the <a href="https://www.youtube.com/watch?v=ZYG3T4l8RU8">Nix Developer Dialogues</a>, or tazjin's <a href="https://tazj.in/blog/tvix-eval-talk-2023">talk on tvix-eval</a> (in Russian)), we never found the time to write something down.</p> <p>In the meantime a lot of stuff has happened though, so it's time to change that :-)</p> <p>Note: This blog post is intended for a technical audience that is already intimately familiar with Nix, and knows what things like derivations or store paths are. If you're new to Nix, this will not make a lot of sense to you!</p> <h2><a href="#evaluation-regression-testing" aria-hidden="true" class="anchor" id="evaluation-regression-testing"></a>Evaluation regression testing</h2> <p>Most of the evaluator work has been driven by evaluating <code>nixpkgs</code>, and ensuring that we produce the same derivations, and that their build results end up in the same store paths.</p> <p>Builds are not hooked up all the way to the evaluator yet, but for Nix code without IFD (such as <code>nixpkgs</code>!) we can verify this property without building. An evaluated Nix derivation's <code>outPath</code> (and <code>drvPath</code>) can be compared with what C++ Nix produces for the same code, to determine whether we evaluated the package (and all of its dependencies!) correctly <sup class="footnote-ref"><a href="#fn1" id="fnref1">1</a></sup>.</p> <p>We added integration tests in CI that ensure that the paths we calculate match C++ Nix, and are successfully evaluating fairly complicated expressions in them. For example, we test against the Firefox derivation, which exercises some of the more hairy bits in <code>nixpkgs</code> (like WASM cross-compilation infrastructure). Yay!</p> <p>Although we're avoiding fine-grained optimization until we're sure Tvix evaluates all of <code>nixpkgs</code> correctly, we still want to have an idea about evaluation performance and how our work affects it over time.</p> <p>For this we extended our benchmark suite and integrated it with <a href="https://staging.windtunnel.ci/tvl/tvix">Windtunnel</a>, which now regularly runs benchmarks and provides a view into how the timings change from commit to commit.</p> <p>In the future, we plan to run this as a part of code review, before changes are applied to our canonical branch, to provide this as an additional signal to authors and reviewers without having to run the benchmarks manually.</p> <h2><a href="#aterms-output-path-calculation-and-builtinsderivation" aria-hidden="true" class="anchor" id="aterms-output-path-calculation-and-builtinsderivation"></a>ATerms, output path calculation, and <code>builtins.derivation</code></h2> <p>We've implemented all of these features, which comprise the components needed to construct derivations in the Nix language, and to allow us to perform the path comparisons we mentioned before.</p> <p>As an interesting side note, in C++ Nix <code>builtins.derivation</code> is not actually a builtin! It is a piece of <a href="https://github.com/NixOS/nix/blob/49cf090cb2f51d6935756a6cf94d568cab063f81/src/libexpr/primops/derivation.nix#L4">bundled Nix code</a>, that massages some parameters and then calls the <em>actual</em> builtin: <code>derivationStrict</code>. We've decided to keep this setup, and implemented support in Tvix to have builtins defined in <code>.nix</code> source code.</p> <p>These builtins return attribute sets with the previously mentioned <code>outPath</code> and <code>drvPath</code> fields. Implementing them correctly meant that we needed to implement output path calculation <em>exactly</em> the same way as Nix does (bit-by-bit).</p> <p>Very little of how this output path calculation works is documented anywhere in C++ Nix. It uses a subset of <a href="http://program-transformation.org/Tools/ATermFormat.html">ATerm</a> internally, produces &quot;fingerprints&quot; containing hashes of these ATerms, which are then hashed again. The intermediate hashes are not printed out anywhere (except if you <a href="https://github.com/adisbladis/nix/tree/hash-tracing">patch Nix</a> to do so).</p> <p>We already did parts of this correctly while starting this work on <a href="https://github.com/nix-community/go-nix/blob/93cb24a868562714f1691840e94d54ef57bc0a5a/pkg/derivation/hashes.go#L52">go-nix</a> some while ago, but found some more edge cases and ultimately came up with a nicer interface for Tvix.</p> <p>All the Derivation internal data model, ATerm serialization and output path calculation have been sliced out into a more general-purpose <a href="https://docs.tvix.dev/rust/nix_compat/derivation/struct.Derivation.html">nix-compat</a> crate, alongside with more documentation unit tests and a Derivation ATerm parser, so hopefully this will now be more accessible for everyone now.</p> <p>Note our builtin does <em>not</em> yet persist the Derivation anywhere &quot;on disk&quot; (though we have a debug CL that does write it to a temporary directory, in case we want to track down differences).</p> <h2><a href="#tvix-castore" aria-hidden="true" class="anchor" id="tvix-castore"></a><code>tvix-[ca]store</code></h2> <p>Tvix now has a store implementation!</p> <h3><a href="#the-nix-model" aria-hidden="true" class="anchor" id="the-nix-model"></a>The Nix model</h3> <p>Inside Nix, store path contents are normally hashed and communicated in NAR format, which is very coarse and often wasteful - a single bit of change in one file in a large store path causes a new NAR file to be uploaded to the binary cache, which then needs to be downloaded.</p> <p>Additionally, identifying everything by the SHA256 digest of its NAR representation makes Nix store paths very incompatible with other content-addressed systems, as it's a very Nix-specific format.</p> <h3><a href="#the-more-granular-tvix-model" aria-hidden="true" class="anchor" id="the-more-granular-tvix-model"></a>The more granular Tvix model</h3> <p>After experimenting with some concepts and ideas in Golang, mostly around how to improve binary cache performance<sup class="footnote-ref"><a href="#fn2" id="fnref2">2</a></sup>, both on-disk as well as over the network, we settled on a more granular, content-addressed and general-purpose format.</p> <p>Internally, it behaves very similar to how git handles tree objects, except blobs are identified by their raw BLAKE3 digests rather than some custom encoding, and similarly, tree/directory objects use the BLAKE3 digest of its canonical protobuf serialization as identifiers.</p> <p>This provides some immediate benefits:</p> <ul> <li>We only need to keep the same data once, even if it's used across different store paths.</li> <li>Transfers can be more granular and only need to fetch the data that's needed. Due to everything being content-addressed, it can be fetched from anything supporting BLAKE3 digests, immediately making it compatible with other P2P systems (IPFS blake3 blobs, …), or general-purpose content-addressed caches (<a href="https://github.com/buchgr/bazel-remote/pull/715">bazel-remote</a>).</li> </ul> <p>There's a lot more details about the data model, certain decisions etc. in <a href="https://code.tvl.fyi/tree/tvix/docs/src/castore">the docs</a>.</p> <h3><a href="#compatibility" aria-hidden="true" class="anchor" id="compatibility"></a>Compatibility</h3> <p>We however still want to stay compatible with Nix, as in calculating &quot;NAR-addressed&quot; store paths the same, support substituting from regular Nix binary caches, as well as storing all the other additional metadata about store paths.</p> <p>We accomplished this by splitting the two different concerns into two separate <code>tvix-store</code> and <code>tvix-castore</code> crates, with the former one holding all Nix-specific metadata and functionality, and the latter being a general-purpose content-addressed blob and filesystem tree storage system, which is usable in a lot of contexts outside of Tvix too. For example, if you want to use tvix-castore to write your own git alternative, or provide granular and authenticated access into large scientific datasets, you could!</p> <h3><a href="#backends" aria-hidden="true" class="anchor" id="backends"></a>Backends</h3> <p>In addition to a gRPC API and client bindings, there's support for local filesystem-based backends, as well as for sled, an embedded K/V database.</p> <p>We're also currently working on a backend supporting most common object storages, as well as on more granular seeking and content-defined chunking for blobs.</p> <h3><a href="#fusevirtiofs" aria-hidden="true" class="anchor" id="fusevirtiofs"></a>FUSE/virtiofs</h3> <p>A tvix-store can be mounted via FUSE, or exposed through virtiofs<sup class="footnote-ref"><a href="#fn3" id="fnref3">3</a></sup>. While doing the obvious thing - allowing mounting and browsing the contents of the store, this will allow lazy substitution of builds on remote builders, be in containerized or virtualized workloads.</p> <p>We have an <a href="https://code.tvl.fyi/about/tvix/boot/README.md">example</a> in the repository seeding gnu hello into a throwaway store, then booting a MicroVM and executing it.</p> <h3><a href="#nar-bridge-bridging-binary-caches" aria-hidden="true" class="anchor" id="nar-bridge-bridging-binary-caches"></a>nar-bridge, bridging binary caches</h3> <p><code>nar-bridge</code> and the <code>NixHTTPPathInfoService</code> bridge <code>tvix-[ca]store</code> with existing Nix binary caches and Nix.</p> <p>The former exposes a <code>tvix-[ca]store</code> over the common Nix HTTP Binary Cache interface (both read and write).</p> <p>The latter allows Tvix to substitute from regular Nix HTTP Binary caches, unpacking NARs and ingesting them on-the-fly into the castore model. The necessary parsers for NARInfo, signatures etc are also available in the <a href="https://docs.tvix.dev/rust/nix_compat/narinfo/index.html">nix-compat crate</a>.</p> <h2><a href="#evalio--builtins-interacting-with-the-store-more-closely" aria-hidden="true" class="anchor" id="evalio--builtins-interacting-with-the-store-more-closely"></a>EvalIO / builtins interacting with the store more closely</h2> <p>tvix-eval itself is designed to be quite pure when it comes to IO - it doesn't do any IO directly on its own, but for the very little IO functionality it does as part of &quot;basic interaction with paths&quot; (like importing other <code>.nix</code> files), it goes through an <code>EvalIO</code> interface, which is provided to the Evaluator struct on instantiation.</p> <p>This allows us to be a bit more flexible with how IO looks like in practice, which becomes interesting for specific store implementations that might not expose a POSIX filesystem directly, or targets where we don't have a filesystem at all (like WASM).</p> <p>Using the <code>EvalIO</code> trait also lets <code>tvix-eval</code> avoid becoming too strongly coupled to a specific store implementation, hashing scheme etc<sup class="footnote-ref"><a href="#fn4" id="fnref4">4</a></sup>. As we can extend the set of builtins available to the evaluator with &quot;foreign builtins&quot;, these can live in other crates.</p> <p>Following this pattern, we started implementing some of the &quot;basic&quot; builtins that deal with path access in <code>tvix-eval</code>, like:</p> <ul> <li><code>builtins.pathExists</code></li> <li><code>builtins.readFile</code></li> </ul> <p>We also recently started working on more complicated builtins like <code>builtins.filterSource</code> and <code>builtins.path</code>, which are also used in <code>nixpkgs</code>.</p> <p>Both import a path into the store, and allow passing a Nix expression that's used as a filter function for each path. <code>builtins.path</code> can also ensuring the imported contents match a certain hash.</p> <p>This required the builtin to interact with the store and evaluator in a very tight fashion, as the filter function (written in Nix) needs to be repeatedly executed for each path, and its return value is able to cause the store to skip over certain paths (which it previously couldn't).</p> <p>Getting the abstractions right there required some back-and-forth, but the remaining changes should land quite soon.</p> <h2><a href="#catchables--tryeval" aria-hidden="true" class="anchor" id="catchables--tryeval"></a>Catchables / tryEval</h2> <p>Nix has a limited exception system for dealing with user-generated errors: <code>builtins.tryEval</code> can be used to detect if an expression fails (if <code>builtins.throw</code> or <code>assert</code> are used to generate it). This feature requires extra support in any Nix implementation, as errors may not necessarily cause the Nix program to abort.</p> <p>The C++ Nix implementation reuses the C++ language-provided Exception system for <code>builtins.tryEval</code> which Tvix can't (even if Rust had an equivalent system):</p> <p>In C++ Nix the runtime representation of the program in execution corresponds to the Nix expression tree of the relevant source files. This means that an exception raised in C++ code will automatically bubble up correctly since the C++ and Nix call stacks are equivalent to each other.</p> <p>Tvix compiles the Nix expressions to a byte code program which may be mutated by extra optimization rules (for example, we hope to eliminate as many thunks as possible in the future). This means that such a correspondence between the state of the runtime and the original Nix code is not guaranteed.</p> <p>Previously, <code>builtins.tryEval</code> (which is implemented in Rust and can access VM internals) just allowed the VM to recover from certain kinds of errors. This proved to be insufficient as it <a href="https://b.tvl.fyi/issues/281">blew up as soon as a <code>builtins.tryEval</code>-ed thunk is forced again</a> – extra bookkeeping was needed. As a solution, we now store recoverable errors as a separate runtime value type.</p> <p>As you can imagine, storing evaluation failures as &quot;normal&quot; values quickly leads to all sorts of bugs because most VM/builtins code is written with only ordinary values like attribute sets, strings etc. in mind.</p> <p>While ironing those out, we made sure to supplement those fixes with as many test cases for <code>builtins.tryEval</code> as possible. This will hopefully prevent any regressions if or rather when we touch this system again. We already have some ideas for replacing the <code>Catchable</code> value type with a cleaner representation, but first we want to pin down all the unspoken behaviour.</p> <h2><a href="#string-contexts" aria-hidden="true" class="anchor" id="string-contexts"></a>String contexts</h2> <p>For a long time, we had the <a href="https://inbox.tvl.su/depot/20230316120039.j4fkp3puzrtbjcpi@tp/T/#t">working theory</a> that we could get away with not implementing string contexts, and instead do reference scanning on a set of &quot;known paths&quot; (and not implement <code>builtins.unsafeDiscardStringContext</code>).</p> <p>Unfortunately, we discovered that while this is <em>conceptually</em> true, due to a <a href="https://github.com/NixOS/nix/issues/4629">bug in Nix</a> that's worked around in the <code>stdenv.mkDerivation</code> implementation, we can't currently do this and calculate the same hashes.</p> <p>Because hash compatibility is important for us at this point, we bit the bullet and added support for string contexts into our <code>NixString</code> implementation, implemented the context-related builtins, and added more unit tests that verify string context behaviour of various builtins.</p> <h2><a href="#strings-as-byte-strings" aria-hidden="true" class="anchor" id="strings-as-byte-strings"></a>Strings as byte strings</h2> <p>C++ Nix uses C-style zero-terminated strings internally - however, until recently, Tvix has used standard Rust strings for string values. Since those are required to be valid UTF-8, we haven't been able to properly represent all the string values that Nix supports.</p> <p>We recently converted our internal representation to byte strings, which allows us to treat a <code>Vec&lt;u8&gt;</code> as a &quot;string-like&quot; value.</p> <h2><a href="#jsontomlxml" aria-hidden="true" class="anchor" id="jsontomlxml"></a>JSON/TOML/XML</h2> <p>We added support for the <code>toJSON</code>, <code>toXML</code>, <code>fromJSON</code> and <code>fromTOML</code> builtins.</p> <p><code>toXML</code> is particularly exciting, as it's the only format that allows expressing (partially applied) functions. It's also used in some of Nix' own test suite, so we can now include these in our unit test suite (and pass, yay!).</p> <h2><a href="#builder-protocol-drv-builder" aria-hidden="true" class="anchor" id="builder-protocol-drv-builder"></a>Builder protocol, drv-&gt;builder</h2> <p>We've been working on the builder protocol, and Tvix's internal build representation.</p> <p>Nix uses derivations (encoded in ATerm) as nodes in its build graph, but it refers to other store paths used in that build by these store paths <em>only</em>. As mentioned before, store paths only address the inputs - and not the content.</p> <p>This poses a big problem in Nix as soon as builds are scheduled on remote builders: There is no guarantee that files at the same store path on the remote builder actually have the same contents as on the machine orchestrating the build. If a package is not binary reproducible, this can lead to so-called <a href="https://blog.layus.be/posts/2021-06-25-frankenbuilds.html">frankenbuilds</a>.</p> <p>This also introduces a dependency on the state that's present on the remote builder machine: Whatever is in its store and matches the paths will be used, even if it was maliciously placed there.</p> <p>To eliminate this hermiticity problem and increase the integrity of builds, we've decided to use content-addressing in the builder protocol.</p> <p>We're currently hacking on this at <a href="https://thaigersprint.org/">Thaigersprint</a> and might have some more news to share soon!</p> <hr /> <p>That's it for now, try out Tvix and hit us up on IRC or on our mailing list if you run into any snags, or have any questions.</p> <p>เจอกันนะ :)</p> <section class="footnotes"> <ol> <li id="fn1"> <p>We know that we calculated all dependencies correctly because of how their hashes are included in the hashes of their dependents, and so on. More on path calculation and input-addressed paths in the next section! <a href="#fnref1" class="footnote-backref">↩</a></p> </li> <li id="fn2"> <p>See <a href="https://discourse.nixos.org/t/nix-casync-a-more-efficient-way-to-store-and-substitute-nix-store-paths/16539">nix-casync</a> for one example - investing content-defined chunking (while still keeping the NAR format) <a href="#fnref2" class="footnote-backref">↩</a></p> </li> <li id="fn3"> <p>Strictly speaking, not limited to tvix-store - literally anything providing a listing into tvix-castore nodes. <a href="#fnref3" class="footnote-backref">↩</a></p> </li> <li id="fn4"> <p>That's the same reason why <code>builtins.derivation[Strict]</code> also lives in <code>tvix-glue</code>, not in <code>tvix-eval</code>. <a href="#fnref4" class="footnote-backref">↩</a></p> </li> </ol> </section> Tvix Status - September '22 https://tvl.fyi/blog/tvix-status-september-22 2022-09-12T15:12:14+00:00 2022-09-12T15:12:14+00:00 <p>We've now been working on our rewrite of Nix, <a href="https://tvl.fyi/blog/rewriting-nix">Tvix</a>, for over a year.</p> <p>As you can imagine, this past year has been turbulent, to say the least, given the regions where many of us live. As a result we haven't had as much time to work on fun things (like open-source software projects!) as we'd like.</p> <p>We've all been fortunate enough to continue making progress, but we just haven't had the bandwidth to communicate with you and keep you up to speed on what's going on. That's what this blog post is for.</p> <h2><a href="#nix-language-evaluator" aria-hidden="true" class="anchor" id="nix-language-evaluator"></a>Nix language evaluator</h2> <p>The most significant progress in the past six months has been on our Nix language evaluator. To answer the most important question: yes, you can play with it right now – in <a href="https://bolt.tvix.dev">Tvixbolt</a>!</p> <p>We got the evaluator into its current state by first listing all the problems we were likely to encounter, then solving them independently, and finally assembling all those small-scale solutions into a coherent whole. As a result, we briefly had an impractically large private source tree, which we have since <a href="https://cl.tvl.fyi/q/status:merged+%2522tvix/eval%2522+mergedbefore:2022-09-09">integrated</a> into our monorepo.</p> <p>This process was much slower than we would have liked, due to code review bandwidth... which is to say, we're all volunteers. People have lives, bottlenecks happen.</p> <p>Most of this code was either written or reviewed by <a href="https://gws.fyi/">grfn</a>, <a href="https://github.com/sternenseemann">sterni</a> and <a href="https://tazj.in">tazjin</a> (that's me!).</p> <h3><a href="#how-much-of-eval-is-working" aria-hidden="true" class="anchor" id="how-much-of-eval-is-working"></a>How much of eval is working?</h3> <p><em>Most of it</em>! You can enter most (but not <em>all</em>, sorry! Not yet, anyway.) Nix language expressions in <a href="https://bolt.tvix.dev">Tvixbolt</a> and observe how they are evaluated.</p> <p>There's a lot of interesting stuff going on under the hood, such as:</p> <ul> <li> <p>The Tvix compiler can emit warnings and errors without failing early, and retains as much source information as possible. This will enable you to use Tvix as the basis for developer tooling, such as language servers.</p> </li> <li> <p>The Tvix compiler performs in-depth scope analysis, so it can both generate efficient bytecode for accessing identifiers, and alert you about problems in your code before runtime.</p> </li> <li> <p>The runtime supports tail-call optimisation in many (but – again – not yet all) cases, so you can evaluate recursive expressions in constant stack space.</p> </li> <li> <p>The runtime can give you different backing representations for the same Nix type. For example, an attribute set is represented differently depending on whether you've constructed an empty one, a <code>name/value</code> pair, or a larger set. This lets us optimise frequent, well-known use-cases without impacting the general case much.</p> </li> </ul> <p>We've run some initial benchmarks against C++ Nix (using the features that are ready), and in most cases Tvix evaluation is an order of magnitude faster. To be fair, though, these benchmarks are in no way indicative of real-life performance for things like <code>nixpkgs</code>. More information is coming... eventually.</p> <h3><a href="#how-does-it-all-work" aria-hidden="true" class="anchor" id="how-does-it-all-work"></a>How does it all work?</h3> <p>Tvix's evaluator uses a custom abstract machine with a Nix-specific instruction set, and a compiler that traverses a parsed Nix AST to emit this bytecode and perform a set of optimisations and other analysis. The most important benefit of this is that we can plan and lay out the execution of a program in a way that is better suited to an efficient runtime than directly traversing the AST.</p> <p class="cheddar-callout cheddar-tip"> TIP: You can see the generated bytecode in <a href="https://bolt.tvix.dev">Tvixbolt</a>! </p> <p>This is all written in about 4000 lines of Rust (naturally), some of which – especially around scope-handling – are deceptively simple.</p> <p>As part of our CI suite, we run the evaluator against some tests we wrote ourselves, as well as against the upstream Nix test suite (which we don't <em>quite</em> pass yet. We're working on it!).</p> <h3><a href="#whats-next-for-tvix-eval" aria-hidden="true" class="anchor" id="whats-next-for-tvix-eval"></a>What's next for tvix-eval?</h3> <p>Despite all our progress, there are still some unfinished feature areas, and some of them are pretty important:</p> <ol> <li> <p>The majority of Nix's builtins – including fundamental ones like <code>import</code> and <code>derivation</code> – aren't implemented yet.</p> </li> <li> <p>Neither are recursive attribute sets (<code>rec</code>). This isn't because of a problem with the recursion itself, but because of the handling of nested keys (such as <code>a.b</code>). We have a lackluster solution already, but are designing a more efficient one.</p> </li> </ol> <p>In both cases, we've mostly figured out what to do; now it's just a matter of finding the time to do it. Our progress is steady, and can be tracked <a href="https://code.tvl.fyi/tree/tvix/eval">in the source</a> (viewer without Javascript <a href="https://code.tvl.fyi/tree/tvix/eval">here</a>).</p> <p>Apart from that, the next steps are:</p> <ul> <li> <p>Comprehensive benchmarking. We're standing up an infrastructure for continuous benchmarking to measure the impact of changes. It'll also let us identify and optimise hotspots</p> </li> <li> <p>Implementing known optimisations. There are some areas of the code that have the potential for significant speed gains, but we're holding off implementing those until the evaluator is feature complete and passes the Nix test suite.</p> </li> <li> <p>Finishing our language specification. Based on what we've learned, we're writing a specification of the Nix language that captures its various behaviours in all their tricky subtlety and subtle trickery.</p> </li> </ul> <p>Once we can evaluate <code>nixpkgs</code>, we're likely to shift our focus towards the other areas of Tvix.</p> <h2><a href="#the-other-areas-of-tvix" aria-hidden="true" class="anchor" id="the-other-areas-of-tvix"></a>The Other Areas of Tvix</h2> <p>Speaking of these other areas (most importantly, the builder and store implementation), we've made some nice progress there also.</p> <p>While we've yet to start assembling the actual pieces, <a href="https://flokli.de/">flokli</a> and <a href="https://github.com/adisbladis">adisbladis</a> have been hard at work on <a href="https://github.com/nix-community/go-nix">go-nix</a>, which aims to implement many of the low-level primitives required for the Nix store and builder (hashing and encoding schemes, archive formats, reference scanning ...).</p> <p>We're looking forward to telling you more in the next Tvix status update!</p> <h2><a href="#outro-" aria-hidden="true" class="anchor" id="outro-"></a>Outro ...</h2> <p>We'd be delighted to onboard new contributors to Tvix! Please take a look at the main <a href="https://tvl.fyi">TVL page</a> to find out how to get in touch with us if you'd like to join!</p> <p>Thanks also, of course, to <a href="https://nlnet.nl/">NLNet</a> for sponsoring some of this work!</p> <p>And finally, we would like to thank and pay our respects to jD91mZM2 – the original author of <a href="https://github.com/nix-community/rnix-parser">rnix-parser</a> – who has sadly passed away. Please, tell people how important they are to you.</p> <p>We use <code>rnix-parser</code> in our compiler, and its well-designed internals (also thanks to its new maintainers!) have saved us a lot of time.</p> <p>That's it for this update. Go play with <a href="https://bolt.tvix.dev">Tvixbolt</a>, have fun figuring out weird ways to break it – and if you do, let us know.</p> <p>We'll see you around!</p> Tvix: We are rewriting Nix https://tvl.fyi/blog/rewriting-nix 2021-12-01T17:56:27+00:00 2021-12-01T17:56:27+00:00 <p>Evaluating the Nix programming language, used by the Nix package manager, is currently very slow. This becomes apparent in all projects written in Nix that are not just simple package definitions, for example:</p> <ul> <li>the NixOS module system</li> <li>TVL projects like <a href="https://at.tvl.fyi/?q=%2F%2Fnix%2Fyants"><code>//nix/yants</code></a> and <a href="https://at.tvl.fyi/?q=%2F%2Fweb%2Fbubblegum"><code>//web/bubblegum</code></a>.</li> <li>the code that <a href="https://at.tvl.fyi/?q=%2F%2Fops%2Fpipelines">generates build instructions</a> for TVL's <a href="https://tvl.fyi/builds">CI setup</a></li> </ul> <p>Whichever project you pick, they all suffer from issues with the language implementation. At TVL, it takes us close to a minute to create the CI instructions for our monorepo at the moment - despite it being a plain Nix evaluation. Running our Nix-native build systems for <a href="https://code.tvl.fyi/about/nix/buildGo">Go</a> and <a href="https://code.tvl.fyi/about/nix/buildLisp">Common Lisp</a> takes much more time than we would like.</p> <p>Some time last year a few of us got together and started investigating ways to modernise the current architecture of Nix and figure out how to improve the speed of some of the components. We created over <a href="https://cl.tvl.fyi/q/topic:tvix">250 commits</a> in our fork of the Nix 2.3 codebase at the time, tried <a href="https://cl.tvl.fyi/c/depot/+/1123/">performance experiments</a> aimed at improving the current evaluator and fought <a href="https://cl.tvl.fyi/c/depot/+/1504">gnarly bugs</a>.</p> <p>After a while we realised that we were treading water: Some of our ideas are too architecturally divergent from Nix to be done on top of the existing codebase, and the memory model of Nix causes significant headaches when trying to do any kind of larger change.</p> <p>We needed an alternative approach and started brainstorming on a bent whiteboard in a small flat in Hurghada, Egypt.</p> <p><img src="https://static.tvl.fyi/latest/files/flokli_tazjin_tvix.webp" alt="flokli &amp; tazjin brainstorming" /></p> <p>Half a year later we are now ready to announce our new project: <strong>Tvix</strong>, a re-imagined Nix with full nixpkgs compatibility. Tvix is generously funded <a href="https://nlnet.nl/project/Tvix/">by NLNet</a> (thanks!) and we are ready to start implementing it.</p> <p>The <a href="https://code.tvl.fyi/about/tvix/docs/components.md">Tvix architecture</a> is designed to be modular: It should be possible to write an evaluator that plugs in the Guile language (for compatibility with GNU Guix), to use arbitrary builders, and to replace the store implementation.</p> <p>Tvix has these high-level goals:</p> <ul> <li> <p>Creating an alternative implementation of Nix that is <strong>fully compatible with nixpkgs</strong>.</p> <p>The package collection is an enormous effort with hundreds of thousands of commits, encoding expert knowledge about lots of different software and ways of building and managing it. It is a very valuable piece of software and we must be able to reuse it.</p> </li> <li> <p>More efficient Nix language evaluation, leading to greatly increased performance.</p> </li> <li> <p>No more strict separation of evaluation and build phases: Generating Nix data structures from build artefacts (&quot;IFD&quot;) should be supported first-class and not incur significant performance cost.</p> </li> <li> <p>Well-defined interaction protocols for how the three different components (evaluator, builder, store) interact.</p> </li> <li> <p>A builder implementation using OCI instead of custom sandboxing code.</p> </li> </ul> <p><img src="https://static.tvl.fyi/latest/files/adisbladis_tazjin_tvix.webp" alt="adisbladis &amp; tazjin brainstorming" /></p> <p>Tvix is not intended to <em>replace</em> Nix, instead we want to improve the ecosystem by offering an alternative, fast and reliable implementation for Nix features that are in use today.</p> <p>As things ramp up we will be posting more information on this blog, for now you can keep an eye on <a href="https://code.tvl.fyi/tree/tvix"><code>//tvix</code></a> in the TVL monorepo and subscribe to <a href="https://tvl.fyi/feed.atom">our feed</a>.</p> <p>Stay tuned!</p> <p><span style="font-size: small;">PS: TVL is international, but a lot of the development will take place in our office in Moscow. Say hi if you're around and interested!</span></p>