about summary refs log tree commit diff
path: root/tvix/glue/src/builtins
diff options
context:
space:
mode:
authorRyan Lahfa <tvl@lahfa.xyz>2024-01-16T04·31+0100
committerclbot <clbot@tvl.fyi>2024-01-18T14·28+0000
commit12ae96cff2e925f502cee8afb4f8dcf54aba27d8 (patch)
tree661f8bf7401b524d9ac3dc6770eddd10b3bdb4d7 /tvix/glue/src/builtins
parent43b9e25025eef302369ff27074bfa5bbfb1c7115 (diff)
feat(tvix/glue): use TvixStoreIO as derivation builtin state r/7410
We propagate a `TvixStoreIO` as the `state` of our derivation-specific
builtins in the glue crate.

The evaluators `io_handle` itself is using a Rc<dyn EvalIO>.

An earlier version of TvixStoreIO was also introducing generics over the
different internal services themselves, but we opted for instead
hardcoding this to Arc<dyn …> for the sake of less macro voodoo.

Change-Id: I535c476f06b840858fa3070c4a237ece47f7a15b
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10636
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
Autosubmit: raitobezarius <tvl@lahfa.xyz>
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/glue/src/builtins')
-rw-r--r--tvix/glue/src/builtins/derivation.rs9
-rw-r--r--tvix/glue/src/builtins/mod.rs46
2 files changed, 35 insertions, 20 deletions
diff --git a/tvix/glue/src/builtins/derivation.rs b/tvix/glue/src/builtins/derivation.rs
index c5ed8ec2fffe..180567b8c4a8 100644
--- a/tvix/glue/src/builtins/derivation.rs
+++ b/tvix/glue/src/builtins/derivation.rs
@@ -1,10 +1,9 @@
 //! Implements `builtins.derivation`, the core of what makes Nix build packages.
 use crate::builtins::DerivationError;
-use crate::known_paths::KnownPaths;
+use crate::tvix_store_io::TvixStoreIO;
 use bstr::BString;
 use nix_compat::derivation::{Derivation, Output};
 use nix_compat::nixhash;
-use std::cell::RefCell;
 use std::collections::{btree_map, BTreeSet};
 use std::rc::Rc;
 use tvix_eval::builtin_macros::builtins;
@@ -121,7 +120,7 @@ fn handle_fixed_output(
     Ok(None)
 }
 
-#[builtins(state = "Rc<RefCell<KnownPaths>>")]
+#[builtins(state = "Rc<TvixStoreIO>")]
 pub(crate) mod derivation_builtins {
     use std::collections::BTreeMap;
 
@@ -152,7 +151,7 @@ pub(crate) mod derivation_builtins {
     /// use the higher-level `builtins.derivation` instead.
     #[builtin("derivationStrict")]
     async fn builtin_derivation_strict(
-        state: Rc<RefCell<KnownPaths>>,
+        state: Rc<TvixStoreIO>,
         co: GenCo,
         input: Value,
     ) -> Result<Value, ErrorKind> {
@@ -425,7 +424,7 @@ pub(crate) mod derivation_builtins {
         }
 
         populate_inputs(&mut drv, input_context);
-        let mut known_paths = state.borrow_mut();
+        let mut known_paths = state.as_ref().known_paths.borrow_mut();
 
         // At this point, derivation fields are fully populated from
         // eval data structures.
diff --git a/tvix/glue/src/builtins/mod.rs b/tvix/glue/src/builtins/mod.rs
index 22166055d067..d2378779319b 100644
--- a/tvix/glue/src/builtins/mod.rs
+++ b/tvix/glue/src/builtins/mod.rs
@@ -1,12 +1,14 @@
 //! Contains builtins that deal with the store or builder.
-use std::{cell::RefCell, rc::Rc};
 
-use crate::known_paths::KnownPaths;
+use std::rc::Rc;
+
+use crate::tvix_store_io::TvixStoreIO;
 
 mod derivation;
 mod derivation_error;
 
 pub use derivation_error::Error as DerivationError;
+use tvix_eval::EvalIO;
 
 /// Adds derivation-related builtins to the passed [tvix_eval::Evaluation].
 ///
@@ -14,12 +16,12 @@ pub use derivation_error::Error as DerivationError;
 ///
 /// 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>,
-    known_paths: Rc<RefCell<KnownPaths>>,
-) {
+pub fn add_derivation_builtins<IO>(eval: &mut tvix_eval::Evaluation<IO>, io: Rc<TvixStoreIO>)
+where
+    IO: AsRef<dyn EvalIO>,
+{
     eval.builtins
-        .extend(derivation::derivation_builtins::builtins(known_paths));
+        .extend(derivation::derivation_builtins::builtins(io));
 
     // Add the actual `builtins.derivation` from compiled Nix code
     eval.src_builtins
@@ -28,22 +30,36 @@ pub fn add_derivation_builtins<IO>(
 
 #[cfg(test)]
 mod tests {
+    use std::rc::Rc;
+
+    use crate::tvix_store_io::TvixStoreIO;
+
     use super::add_derivation_builtins;
-    use crate::known_paths::KnownPaths;
     use nix_compat::store_path::hash_placeholder;
-    use std::{cell::RefCell, rc::Rc};
     use test_case::test_case;
-    use tvix_eval::EvaluationResult;
+    use tvix_eval::{EvalIO, EvaluationResult};
+    use tvix_store::utils::construct_services;
 
     /// evaluates a given nix expression and returns the result.
     /// Takes care of setting up the evaluator so it knows about the
     // `derivation` builtin.
     fn eval(str: &str) -> EvaluationResult {
-        let mut eval = tvix_eval::Evaluation::new_impure();
-
-        let known_paths: Rc<RefCell<KnownPaths>> = Default::default();
-
-        add_derivation_builtins(&mut eval, known_paths.clone());
+        // We assemble a complete store in memory.
+        let runtime = tokio::runtime::Runtime::new().expect("Failed to build a Tokio runtime");
+        let (blob_service, directory_service, path_info_service) = runtime
+            .block_on(async { construct_services("memory://", "memory://", "memory://").await })
+            .expect("Failed to construct store services in memory");
+
+        let io = Rc::new(TvixStoreIO::new(
+            blob_service,
+            directory_service,
+            path_info_service.into(),
+            runtime.handle().clone(),
+        ));
+
+        let mut eval = tvix_eval::Evaluation::new(io.clone() as Rc<dyn EvalIO>, false);
+
+        add_derivation_builtins(&mut eval, io);
 
         // run the evaluation itself.
         eval.evaluate(str, None)