refactor(tvix/eval): Builderize Evaluation
Make constructing of a new Evaluation use the builder pattern rather than setting public mutable fields. This is currently a pure refactor (no functionality has changed) but has a few advantages: - We've encapsulated the internals of the fields in Evaluation, meaning we can change them without too much breakage of clients - We have type safety that prevents us from ever changing the fields of an Evaluation after it's built (which matters more in a world where we reuse Evaluations). More importantly, this paves the road for doing different things with the construction of an Evaluation - notably, sharing certain things like the GlobalsMap across subsequent evaluations in eg the REPL. Fixes: b/262 Change-Id: I4a27116faac14cdd144fc7c992d14ae095a1aca4 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11956 Tested-by: BuildkiteCI Autosubmit: aspen <root@gws.fyi> Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
parent
d5964c1d54
commit
dfe137786c
15 changed files with 325 additions and 154 deletions
|
|
@ -18,13 +18,14 @@ pub use errors::{DerivationError, FetcherError, ImportError};
|
|||
///
|
||||
/// As they need to interact with `known_paths`, we also need to pass in
|
||||
/// `known_paths`.
|
||||
pub fn add_derivation_builtins<IO>(eval: &mut tvix_eval::Evaluation<IO>, io: Rc<TvixStoreIO>) {
|
||||
eval.builtins
|
||||
.extend(derivation::derivation_builtins::builtins(Rc::clone(&io)));
|
||||
|
||||
// Add the actual `builtins.derivation` from compiled Nix code
|
||||
eval.src_builtins
|
||||
.push(("derivation", include_str!("derivation.nix")));
|
||||
pub fn add_derivation_builtins<'co, 'ro, 'env, IO>(
|
||||
eval_builder: tvix_eval::EvaluationBuilder<'co, 'ro, 'env, IO>,
|
||||
io: Rc<TvixStoreIO>,
|
||||
) -> tvix_eval::EvaluationBuilder<'co, 'ro, 'env, IO> {
|
||||
eval_builder
|
||||
.add_builtins(derivation::derivation_builtins::builtins(Rc::clone(&io)))
|
||||
// Add the actual `builtins.derivation` from compiled Nix code
|
||||
.add_src_builtin("derivation", include_str!("derivation.nix"))
|
||||
}
|
||||
|
||||
/// Adds fetcher builtins to the passed [tvix_eval::Evaluation]:
|
||||
|
|
@ -32,9 +33,11 @@ pub fn add_derivation_builtins<IO>(eval: &mut tvix_eval::Evaluation<IO>, io: Rc<
|
|||
/// * `fetchurl`
|
||||
/// * `fetchTarball`
|
||||
/// * `fetchGit`
|
||||
pub fn add_fetcher_builtins<IO>(eval: &mut tvix_eval::Evaluation<IO>, io: Rc<TvixStoreIO>) {
|
||||
eval.builtins
|
||||
.extend(fetchers::fetcher_builtins::builtins(Rc::clone(&io)));
|
||||
pub fn add_fetcher_builtins<'co, 'ro, 'env, IO>(
|
||||
eval_builder: tvix_eval::EvaluationBuilder<'co, 'ro, 'env, IO>,
|
||||
io: Rc<TvixStoreIO>,
|
||||
) -> tvix_eval::EvaluationBuilder<'co, 'ro, 'env, IO> {
|
||||
eval_builder.add_builtins(fetchers::fetcher_builtins::builtins(Rc::clone(&io)))
|
||||
}
|
||||
|
||||
/// Adds import-related builtins to the passed [tvix_eval::Evaluation].
|
||||
|
|
@ -42,10 +45,12 @@ pub fn add_fetcher_builtins<IO>(eval: &mut tvix_eval::Evaluation<IO>, io: Rc<Tvi
|
|||
/// These are `filterSource` and `path`
|
||||
///
|
||||
/// As they need to interact with the store implementation, we pass [`TvixStoreIO`].
|
||||
pub fn add_import_builtins<IO>(eval: &mut tvix_eval::Evaluation<IO>, io: Rc<TvixStoreIO>) {
|
||||
eval.builtins.extend(import::import_builtins(io));
|
||||
|
||||
pub fn add_import_builtins<'co, 'ro, 'env, IO>(
|
||||
eval_builder: tvix_eval::EvaluationBuilder<'co, 'ro, 'env, IO>,
|
||||
io: Rc<TvixStoreIO>,
|
||||
) -> tvix_eval::EvaluationBuilder<'co, 'ro, 'env, IO> {
|
||||
// TODO(raitobezarius): evaluate expressing filterSource as Nix code using path (b/372)
|
||||
eval_builder.add_builtins(import::import_builtins(io))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
@ -81,11 +86,11 @@ mod tests {
|
|||
runtime.handle().clone(),
|
||||
));
|
||||
|
||||
let mut eval = tvix_eval::Evaluation::new(io.clone() as Rc<dyn EvalIO>, false);
|
||||
|
||||
add_derivation_builtins(&mut eval, Rc::clone(&io));
|
||||
add_fetcher_builtins(&mut eval, Rc::clone(&io));
|
||||
add_import_builtins(&mut eval, io);
|
||||
let mut eval_builder = tvix_eval::Evaluation::builder(io.clone() as Rc<dyn EvalIO>);
|
||||
eval_builder = add_derivation_builtins(eval_builder, Rc::clone(&io));
|
||||
eval_builder = add_fetcher_builtins(eval_builder, Rc::clone(&io));
|
||||
eval_builder = add_import_builtins(eval_builder, io);
|
||||
let eval = eval_builder.build();
|
||||
|
||||
// run the evaluation itself.
|
||||
eval.evaluate(str, None)
|
||||
|
|
|
|||
|
|
@ -14,12 +14,14 @@ mod tests;
|
|||
/// Tell the Evaluator to resolve `<nix>` to the path `/__corepkgs__`,
|
||||
/// which has special handling in [tvix_io::TvixIO].
|
||||
/// This is used in nixpkgs to import `fetchurl.nix` from `<nix>`.
|
||||
pub fn configure_nix_path<IO>(
|
||||
eval: &mut tvix_eval::Evaluation<IO>,
|
||||
pub fn configure_nix_path<'co, 'ro, 'env, IO>(
|
||||
eval_builder: tvix_eval::EvaluationBuilder<'co, 'ro, 'env, IO>,
|
||||
nix_search_path: &Option<String>,
|
||||
) {
|
||||
eval.nix_path = nix_search_path
|
||||
.as_ref()
|
||||
.map(|p| format!("nix=/__corepkgs__:{}", p))
|
||||
.or_else(|| Some("nix=/__corepkgs__".to_string()));
|
||||
) -> tvix_eval::EvaluationBuilder<'co, 'ro, 'env, IO> {
|
||||
eval_builder.nix_path(
|
||||
nix_search_path
|
||||
.as_ref()
|
||||
.map(|p| format!("nix=/__corepkgs__:{}", p))
|
||||
.or_else(|| Some("nix=/__corepkgs__".to_string())),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,16 +47,18 @@ fn eval_test(code_path: PathBuf, expect_success: bool) {
|
|||
tokio_runtime.handle().clone(),
|
||||
));
|
||||
// Wrap with TvixIO, so <nix/fetchurl.nix can be imported.
|
||||
let mut eval = tvix_eval::Evaluation::new(
|
||||
Box::new(TvixIO::new(tvix_store_io.clone() as Rc<dyn EvalIO>)) as Box<dyn EvalIO>,
|
||||
true,
|
||||
);
|
||||
let mut eval_builder = tvix_eval::Evaluation::builder(Box::new(TvixIO::new(
|
||||
tvix_store_io.clone() as Rc<dyn EvalIO>,
|
||||
)) as Box<dyn EvalIO>)
|
||||
.enable_import()
|
||||
.strict();
|
||||
|
||||
eval.strict = true;
|
||||
add_derivation_builtins(&mut eval, tvix_store_io.clone());
|
||||
add_fetcher_builtins(&mut eval, tvix_store_io.clone());
|
||||
add_import_builtins(&mut eval, tvix_store_io.clone());
|
||||
configure_nix_path(&mut eval, &None);
|
||||
eval_builder = add_derivation_builtins(eval_builder, Rc::clone(&tvix_store_io));
|
||||
eval_builder = add_fetcher_builtins(eval_builder, Rc::clone(&tvix_store_io));
|
||||
eval_builder = add_import_builtins(eval_builder, tvix_store_io);
|
||||
eval_builder = configure_nix_path(eval_builder, &None);
|
||||
|
||||
let eval = eval_builder.build();
|
||||
|
||||
let result = eval.evaluate(code, Some(code_path.clone()));
|
||||
let failed = match result.value {
|
||||
|
|
|
|||
|
|
@ -653,11 +653,13 @@ mod tests {
|
|||
Arc::<DummyBuildService>::default(),
|
||||
tokio_runtime.handle().clone(),
|
||||
));
|
||||
let mut eval = tvix_eval::Evaluation::new(io.clone() as Rc<dyn EvalIO>, true);
|
||||
|
||||
add_derivation_builtins(&mut eval, io.clone());
|
||||
add_fetcher_builtins(&mut eval, io.clone());
|
||||
add_import_builtins(&mut eval, io);
|
||||
let mut eval_builder =
|
||||
tvix_eval::Evaluation::builder(io.clone() as Rc<dyn EvalIO>).enable_import();
|
||||
eval_builder = add_derivation_builtins(eval_builder, Rc::clone(&io));
|
||||
eval_builder = add_fetcher_builtins(eval_builder, Rc::clone(&io));
|
||||
eval_builder = add_import_builtins(eval_builder, io);
|
||||
let eval = eval_builder.build();
|
||||
|
||||
// run the evaluation itself.
|
||||
eval.evaluate(str, None)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue