add builder pattern for tvix_eval::Evaluation, allow passing in config to tvix_serde

#262
Opened by flokli at 2023-03-20T12·29+00

Currently, tvix_serde creates an evaluation object with the passed source code and immediately starts to evaluate:

    let eval = tvix_eval::Evaluation::new(src, None);
    let source = eval.source_map();
    let result = eval.evaluate();

    if !result.errors.is_empty() {
        return Err(Error::NixErrors {
            errors: result.errors,
            source,
        });
    }

This means, it's not possible to pass in custom builtins etc.

It might make more sense to have tvix_eval provide a Builder Pattern to assemble EvalConfig, containing all this config, and consume such a config in tvix_serde.

We could also expose a "pure eval, no IO, no build/store related builtins" default config (by implementing Default.

That way, WASM or serde clients could just say EvalConfig::default(), and the CLI could compose another one.

  1. After playing with these thoughts a bit, I think we want to have a longer-living struct Evaluator struct, that is constructed from a EvalBuilder::build(). config in Evaluator should be considered immutable after creation.

    We could then move compile_only and evaluate to Evaluator::{compile_only,evaluate}, and add the attribute path/source code to the function arguments - probably together with optional observer(s) - but these could be called without consuming self.

    This should open up possibilities to longer-running evaluators, some simple LSP workloads etc - and we could pass such an Evaluator into tvix-serde - and derive Default, if we want to.

    flokli at 2023-05-27T17·23+00

  2. This should probably allow setting NIX_PATH elements. We also might want to support some feature for bundling stuff like lib, as that can be very useful without the wider nixpkgs context.

    tazjin at 2023-06-16T11·18+00

  3. Fixed by cl/8808

    tazjin at 2023-06-16T11·53+00

  4. It's more of a workaround. I'd still think the builder pattern would be a bit more ergonomic.

    flokli at 2023-06-16T12·04+00

  5. This should probably live in tvix-glue, and we should have some functions that make the whole known paths thing less annoying.

    flokli at 2023-11-03T12·12+00