git.delta.rocks / fleet / refs/commits / 210037fd620b

difftreelog

refactor nix store api cleanup

ssxzyxlqYaroslav Bolyukin2026-06-15parent: #7bc4be6.patch.diff

7 files changed

modifiedcmds/fleet/src/main.rsdiffbeforeafterboth
--- a/cmds/fleet/src/main.rs
+++ b/cmds/fleet/src/main.rs
@@ -24,8 +24,7 @@
 #[cfg(feature = "indicatif")]
 use indicatif::{ProgressState, ProgressStyle};
 use nix_eval::{
-	add_file_to_store, gc_register_my_thread, gc_unregister_my_thread, init_libraries,
-	init_tokio_for_nix,
+	eval_store, gc_register_my_thread, gc_unregister_my_thread, init_libraries, init_tokio_for_nix,
 };
 use opentelemetry::trace::TracerProvider;
 use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
@@ -33,6 +32,7 @@
 	OtlpBaseSettings, OtlpLogsSettings, OtlpTracesSettings, ResolvedOtlpSettings,
 };
 use opentelemetry_sdk::{logs::SdkLoggerProvider, trace::SdkTracerProvider};
+use tokio::task::spawn_blocking;
 use tracing::{Instrument, error, info, info_span};
 #[cfg(feature = "indicatif")]
 use tracing_indicatif::IndicatifLayer;
@@ -59,7 +59,8 @@
 				Utf8PathBuf::try_from(entry.path()).context("prefetch path should be utf8")?;
 			let span = info_span!("prefetching", name = %name);
 			tasks.push(async move {
-				let added = tokio::task::spawn_blocking(move || add_file_to_store(&name, &path))
+				let store = eval_store();
+				let added = spawn_blocking(move || store.add_file(&name, &path))
 					.instrument(span.clone())
 					.await??;
 				let _g = span.enter();
@@ -121,9 +122,7 @@
 		Opts::Prefetch(p) => p.run(config).await?,
 		Opts::Tf(t) => t.run(config).await?,
 		// TODO: actually parse commands before starting the async runtime
-		Opts::Complete(c) => {
-			tokio::task::spawn_blocking(move || c.run(RootOpts::command())).await?
-		}
+		Opts::Complete(c) => spawn_blocking(move || c.run(RootOpts::command())).await?,
 	};
 	Ok(())
 }
modifiedcrates/fleet-base/src/host.rsdiffbeforeafterboth
--- a/crates/fleet-base/src/host.rs
+++ b/crates/fleet-base/src/host.rs
@@ -13,7 +13,7 @@
 use camino::{Utf8Path, Utf8PathBuf};
 use chrono::{DateTime, Utc};
 use fleet_shared::SecretData;
-use nix_eval::{Store, Value, nix_go, nix_go_json, util::assert_warn};
+use nix_eval::{Store, Value, eval_store, nix_go, nix_go_json, util::assert_warn};
 use remowt_client::{AgentBundle, Remowt};
 use remowt_endpoints::fs::FsClient;
 use remowt_link_shared::Address;
@@ -427,8 +427,10 @@
 		let store = self.nix_store().await?;
 		{
 			let path = path.clone();
-			spawn_blocking(move || nix_eval::copy_closure_to(&store, path.as_ref()))
-				.await?
+			let store = eval_store();
+			spawn_blocking(move || store.copy_to(&store, path.as_ref()))
+				.await
+				.expect("copy_to panicked")
 				.context("copying closure to remote store")?;
 		}
 		Ok(path)
modifiedcrates/nix-eval/src/drv.rsdiffbeforeafterboth
--- a/crates/nix-eval/src/drv.rs
+++ b/crates/nix-eval/src/drv.rs
@@ -1,41 +1,21 @@
 use std::collections::{HashMap, HashSet, VecDeque};
-use std::ffi::CString;
 
 use anyhow::{Result, bail};
+use camino::{Utf8Component, Utf8Path, Utf8PathBuf};
 use serde::Deserialize;
 
 use crate::nix_raw::{derivation_free, derivation_to_json, store_drv_from_store_path};
-use crate::{copy_nix_str, with_store_context};
-
-fn store_dir() -> Result<String> {
-	let mut out = String::new();
-	with_store_context(|c, store, _| unsafe {
-		crate::nix_raw::store_get_storedir(c, store, Some(copy_nix_str), (&raw mut out).cast())
-	})?;
-	Ok(out)
-}
-
-fn to_absolute_store_path(store_dir: &str, path: &str) -> String {
-	if path.starts_with('/') {
-		path.to_owned()
-	} else {
-		format!("{store_dir}/{path}")
-	}
-}
+use crate::{Store, copy_nix_str, with_default_context};
 
 pub struct Derivation(*mut crate::nix_raw::derivation);
 unsafe impl Send for Derivation {}
 
 impl Derivation {
-	pub fn from_path(drv_path: &str) -> Result<Self> {
-		let path_c = CString::new(drv_path)?;
-		let store_path = with_store_context(|c, store, _| unsafe {
-			crate::nix_raw::store_parse_path(c, store, path_c.as_ptr())
-		})?;
-		let drv = with_store_context(|c, store, _| unsafe {
-			store_drv_from_store_path(c, store, store_path)
+	pub fn from_path(store: &Store, drv_path: &Utf8Path) -> Result<Self> {
+		let store_path = store.parse_path(drv_path)?;
+		let drv = with_default_context(|c, _| unsafe {
+			store_drv_from_store_path(c, store.as_ptr(), store_path.as_ptr())
 		});
-		unsafe { crate::nix_raw::store_path_free(store_path) };
 		let drv = drv?;
 		if drv.is_null() {
 			bail!("failed to read derivation from {drv_path}");
@@ -45,7 +25,7 @@
 
 	pub fn to_json_string(&self) -> Result<String> {
 		let mut out = String::new();
-		with_store_context(|c, _, _| unsafe {
+		with_default_context(|c, _| unsafe {
 			derivation_to_json(c, self.0, Some(copy_nix_str), (&raw mut out).cast())
 		})?;
 		Ok(out)
@@ -78,9 +58,9 @@
 #[derive(Debug, Deserialize)]
 pub struct DrvInputs {
 	#[serde(default)]
-	pub srcs: Vec<String>,
+	pub srcs: Vec<Utf8PathBuf>,
 	#[serde(default)]
-	pub drvs: HashMap<String, DrvInputEntry>,
+	pub drvs: HashMap<Utf8PathBuf, DrvInputEntry>,
 }
 
 #[derive(Debug, Deserialize)]
@@ -90,23 +70,23 @@
 
 #[derive(Debug, Clone)]
 pub struct DrvGraph {
-	pub root: String,
-	pub nodes: HashMap<String, DrvNode>,
+	pub root: Utf8PathBuf,
+	pub nodes: HashMap<Utf8PathBuf, DrvNode>,
 }
 
 #[derive(Debug, Clone)]
 pub struct DrvNode {
 	pub name: String,
-	pub input_drvs: HashMap<String, Vec<String>>,
-	pub input_srcs: Vec<String>,
+	pub input_drvs: HashMap<Utf8PathBuf, Vec<String>>,
+	pub input_srcs: Vec<Utf8PathBuf>,
 	// TODO: CA outputs without a known paths are skipped
-	pub outputs: HashMap<String, String>,
+	pub outputs: HashMap<String, Utf8PathBuf>,
 }
 
 impl DrvGraph {
-	pub fn resolve(drv_path: &str) -> Result<Self> {
-		let sd = store_dir()?;
-		let root = to_absolute_store_path(&sd, drv_path);
+	pub fn resolve(store: &Store, drv_path: &Utf8Path) -> Result<Self> {
+		let sd = store.store_dir()?;
+		let root = sd.join(drv_path);
 
 		let mut nodes = HashMap::new();
 		let mut queue = VecDeque::new();
@@ -115,14 +95,14 @@
 		visited.insert(root.clone());
 
 		while let Some(path) = queue.pop_front() {
-			let drv = Derivation::from_path(&path)?;
+			let drv = Derivation::from_path(store, &path)?;
 			let parsed = drv.parsed()?;
 
-			let input_drvs: HashMap<String, Vec<String>> = parsed
+			let input_drvs: HashMap<Utf8PathBuf, Vec<String>> = parsed
 				.inputs
 				.drvs
 				.into_iter()
-				.map(|(k, v)| (to_absolute_store_path(&sd, &k), v.outputs))
+				.map(|(k, v)| (sd.join(&k), v.outputs))
 				.collect();
 
 			for dep_path in input_drvs.keys() {
@@ -131,10 +111,10 @@
 				}
 			}
 
-			let outputs: HashMap<String, String> = parsed
+			let outputs: HashMap<String, Utf8PathBuf> = parsed
 				.outputs
 				.into_iter()
-				.filter_map(|(name, out)| out.path.map(|p| (name, to_absolute_store_path(&sd, &p))))
+				.filter_map(|(name, out)| out.path.map(|p| (name, sd.join(&p))))
 				.collect();
 
 			nodes.insert(
@@ -151,11 +131,11 @@
 		Ok(Self { root, nodes })
 	}
 
-	pub fn wanted_outputs(&self, root_outputs: &[String]) -> HashMap<String, Vec<String>> {
-		let mut wanted: HashMap<String, HashSet<String>> = HashMap::new();
+	pub fn wanted_outputs(&self, root_outputs: &[String]) -> HashMap<Utf8PathBuf, Vec<String>> {
+		let mut wanted: HashMap<Utf8PathBuf, HashSet<String>> = HashMap::new();
 		wanted.insert(self.root.clone(), root_outputs.iter().cloned().collect());
 
-		let mut queue: VecDeque<String> = VecDeque::new();
+		let mut queue: VecDeque<Utf8PathBuf> = VecDeque::new();
 		queue.push_back(self.root.clone());
 		while let Some(path) = queue.pop_front() {
 			let Some(node) = self.nodes.get(&path) else {
@@ -186,12 +166,19 @@
 	}
 }
 
-fn extract_drv_name(drv_path: &str) -> String {
-	drv_path
-		.rsplit('/')
+pub fn extract_drv_name(drv_path: &Utf8Path) -> String {
+	let comp = drv_path
+		.components()
+		.rev()
 		.next()
-		.and_then(|f| f.strip_suffix(".drv"))
-		.and_then(|f| f.split_once('-').map(|(_, name)| name))
-		.unwrap_or(drv_path)
-		.to_owned()
+		.expect("drv path is at least one component");
+	let Utf8Component::Normal(n) = comp else {
+		panic!("drv path is normal");
+	};
+
+	let n = n.strip_suffix(".drv").unwrap_or(n);
+
+	let n = n.split_once(' ').map(|(_, n)| n).unwrap_or(n);
+
+	n.to_owned()
 }
modifiedcrates/nix-eval/src/lib.rsdiffbeforeafterboth
--- a/crates/nix-eval/src/lib.rs
+++ b/crates/nix-eval/src/lib.rs
@@ -25,7 +25,7 @@
 	PrimOpFun, Store as c_store, StorePath as c_store_path, alloc_primop, alloc_value,
 	bindings_builder_free, bindings_builder_insert, c_context, c_context_create, c_context_free,
 	clear_err, copy_value, err_NIX_ERR_KEY, err_NIX_ERR_NIX_ERROR, err_NIX_ERR_OVERFLOW,
-	err_NIX_ERR_UNKNOWN, err_code, err_info_msg, err_msg, eval_state_build,
+	err_NIX_ERR_UNKNOWN, err_NIX_OK, err_code, err_info_msg, err_msg, eval_state_build,
 	eval_state_builder_load, eval_state_builder_new, eval_state_builder_set_eval_setting,
 	expr_eval_from_string, fetchers_settings, fetchers_settings_free, fetchers_settings_new,
 	flake_lock, flake_lock_flags, flake_lock_flags_free, flake_lock_flags_new, flake_reference,
@@ -320,15 +320,16 @@
 struct GlobalState {
 	// Store should be valid as long as EvalState is valid
 	#[allow(dead_code)]
-	store: Store,
+	store: Arc<Store>,
 	state: EvalState,
 }
 impl GlobalState {
 	fn new() -> Result<Self> {
 		let mut ctx = NixContext::new();
-		let store = ctx
-			.run_in_context(|c| unsafe { store_open(c, c"auto".as_ptr(), null_mut()) })
-			.map(Store)?;
+		let store = Arc::new(
+			ctx.run_in_context(|c| unsafe { store_open(c, c"auto".as_ptr(), null_mut()) })
+				.map(Store)?,
+		);
 
 		let builder = ctx.run_in_context(|c| unsafe { eval_state_builder_new(c, store.0) })?;
 		ctx.run_in_context(|c| unsafe { eval_state_builder_load(c, builder) })?;
@@ -385,66 +386,8 @@
 	v
 }
 
-/// Same as with_default_context, but also passes store...
-/// Yep, this code is garbage and needs to be refactored.
-pub(crate) fn with_store_context<T>(
-	f: impl FnOnce(*mut c_context, *mut c_store, *mut c_eval_state) -> T,
-) -> Result<T> {
-	let global = &GLOBAL_STATE;
-	let (ctx, store, state) =
-		THREAD_STATE.with_borrow_mut(|w| (w.ctx.0, global.store.0, global.state.0));
-	let mut ctx = NixContext(ctx);
-	let v = ctx.run_in_context(|c| f(c, store, state));
-	std::mem::forget(ctx);
-	v
-}
-
 pub fn set_setting(s: &CStr, v: &CStr) -> Result<()> {
 	with_default_context(|c, _| unsafe { setting_set(c, s.as_ptr(), v.as_ptr()) }).map(|_| ())
-}
-
-#[instrument(skip(dst))]
-pub fn copy_closure_to(dst: &Store, path: &Utf8Path) -> Result<()> {
-	let path_c = CString::new(path.as_str())?;
-	with_store_context(|c, src_store, _state| -> Result<()> {
-		let sp = unsafe { store_parse_path(c, src_store, path_c.as_ptr()) };
-		if sp.is_null() {
-			bail!("failed to parse store path {path}");
-		}
-		let rc = unsafe { store_copy_closure(c, src_store, dst.0, sp) };
-		unsafe { store_path_free(sp) };
-		if rc != nix_raw::err_NIX_OK {
-			bail!("store_copy_closure failed (code {rc})");
-		}
-		Ok(())
-	})?
-}
-
-#[instrument]
-pub fn switch_profile(profile: &str, store_path: &Utf8Path) -> Result<()> {
-	let msg = with_store_context(|_c, store, _state| unsafe {
-		nix_cxx::switch_profile(store.cast(), profile, store_path.as_str())
-	})?
-	.to_string();
-	if msg.is_empty() {
-		Ok(())
-	} else {
-		bail!("failed to switch profile {profile}: {msg}");
-	}
-}
-
-// TODO: fleet operator-managed key file
-#[instrument]
-pub fn sign_closure(store_path: &str, key_file: &str) -> Result<()> {
-	let msg = with_store_context(|_c, store, _state| unsafe {
-		nix_cxx::sign_closure(store.cast(), store_path, key_file)
-	})?
-	.to_string();
-	if msg.is_empty() {
-		Ok(())
-	} else {
-		bail!("failed to sign {store_path}: {msg}");
-	}
 }
 
 #[derive(Debug)]
@@ -480,48 +423,8 @@
 			current: g.current,
 		})
 		.collect())
-}
-
-#[instrument]
-pub fn add_file_to_store(name: &str, path: &Utf8Path) -> Result<AddedFile> {
-	let res = with_store_context(|_c, store, _state| unsafe {
-		nix_cxx::add_file_to_store(store.cast(), name, path.as_str())
-	})?;
-	if !res.error.is_empty() {
-		bail!("failed to add {path} to store: {}", res.error);
-	}
-	Ok(AddedFile {
-		store_path: Utf8PathBuf::from(res.store_path),
-		hash: res.hash,
-	})
-}
-
-pub fn build_drv_outputs(drv_path: &str, output_names: &[String]) -> Result<Vec<String>> {
-	let joined = output_names.join("\n");
-	let res = with_store_context(|_c, store, _state| unsafe {
-		nix_cxx::build_drv_outputs(store.cast(), drv_path, &joined)
-	})?;
-	if !res.error.is_empty() {
-		bail!("build of {drv_path} failed: {}", res.error);
-	}
-	Ok(res.outputs)
 }
 
-pub fn substitute_paths(paths: &[String]) -> Result<Vec<String>> {
-	let joined = paths.join("\n");
-	let res = with_store_context(|_c, store, _state| unsafe {
-		nix_cxx::substitute_paths(store.cast(), &joined)
-	})?;
-	if !res.error.is_empty() {
-		warn!("substitute_paths reported: {}", res.error);
-	}
-	Ok(res.outputs)
-}
-
-pub fn is_valid_path(path: &str) -> Result<bool> {
-	with_store_context(|_c, store, _state| unsafe { nix_cxx::is_valid_path(store.cast(), path) })
-}
-
 pub struct FetchSettings(*mut fetchers_settings);
 impl FetchSettings {
 	pub fn new() -> Self {
@@ -624,6 +527,10 @@
 unsafe impl Send for Store {}
 unsafe impl Sync for Store {}
 
+pub fn eval_store() -> Arc<Store> {
+	GLOBAL_STATE.store.clone()
+}
+
 impl Store {
 	pub fn open(uri: &str) -> Result<Self> {
 		let uri = CString::new(uri)?;
@@ -634,11 +541,108 @@
 		Ok(Store(ptr))
 	}
 
-	fn parse_path(&self, path: &CStr) -> Result<StorePath> {
+	pub fn parse_path(&self, path: &Utf8Path) -> Result<StorePath> {
+		let path = CString::new(path.as_str()).expect("valid cstr");
 		with_default_context(|c, _| {
 			StorePath(unsafe { store_parse_path(c, self.0, path.as_ptr()) })
 		})
 	}
+
+	#[instrument(skip(self))]
+	pub fn sign_closure(&self, path: &Utf8Path, key_file: &Utf8Path) -> Result<()> {
+		let err = with_default_context(|_, _| unsafe {
+			nix_cxx::sign_closure(self.as_ptr().cast(), path.as_str(), key_file.as_str())
+		})?
+		.to_string();
+
+		if err.is_empty() {
+			Ok(())
+		} else {
+			bail!("failed to sign {path}: {err}");
+		}
+	}
+
+	#[instrument(skip(self, dst))]
+	pub fn copy_to(&self, dst: &Store, path: &Utf8Path) -> Result<()> {
+		let sp = self
+			.parse_path(&path)
+			.context("failed to parse store path")?;
+		let rc = with_default_context(|c, _| unsafe {
+			store_copy_closure(c, self.as_ptr(), dst.0, sp.as_ptr())
+		})?;
+		if rc != err_NIX_OK {
+			bail!("store_copy_closure failed (code {rc})");
+		}
+		Ok(())
+	}
+
+	/// Would only work with local store.
+	#[instrument(skip(self))]
+	pub fn switch_profile(&self, profile: &str, path: &Utf8Path) -> Result<()> {
+		let msg = unsafe { nix_cxx::switch_profile(self.as_ptr().cast(), profile, path.as_str()) };
+		if msg.is_empty() {
+			Ok(())
+		} else {
+			bail!("failed to switch profile {profile}: {msg}");
+		}
+	}
+
+	#[instrument(skip(self))]
+	pub fn add_file(&self, name: &str, path: &Utf8Path) -> Result<AddedFile> {
+		let msg = unsafe { nix_cxx::add_file_to_store(self.as_ptr().cast(), name, path.as_str()) };
+		if !msg.error.is_empty() {
+			bail!("failed to add {path} to store: {}", msg.error)
+		}
+		Ok(AddedFile {
+			store_path: Utf8PathBuf::from(msg.store_path),
+			hash: msg.hash,
+		})
+	}
+
+	#[instrument(skip(self))]
+	pub fn substitute_paths(&self, paths: &[Utf8PathBuf]) -> Result<Vec<Utf8PathBuf>> {
+		let joined = paths.into_iter().join("\n");
+		let res = unsafe { nix_cxx::substitute_paths(self.as_ptr().cast(), &joined) };
+		if !res.error.is_empty() {
+			warn!("substitute_paths reported: {}", res.error);
+		}
+		Ok(res.outputs.into_iter().map(Utf8PathBuf::from).collect())
+	}
+
+	#[instrument(skip(self))]
+	pub fn is_valid_path(&self, path: &Utf8Path) -> bool {
+		unsafe { nix_cxx::is_valid_path(self.as_ptr().cast(), path.as_str()) }
+	}
+
+	#[instrument(skip(self))]
+	pub fn build_drv_outputs(
+		&self,
+		drv_path: &Utf8Path,
+		output_names: &[String],
+	) -> Result<Vec<String>> {
+		let joined = output_names.join("\n");
+		let res =
+			unsafe { nix_cxx::build_drv_outputs(self.as_ptr().cast(), drv_path.as_str(), &joined) };
+		if !res.error.is_empty() {
+			bail!("build of {drv_path} failed: {}", res.error);
+		}
+		Ok(res.outputs)
+	}
+
+	#[instrument(skip(self))]
+	pub fn store_dir(&self) -> Result<Utf8PathBuf> {
+		let mut out = String::new();
+		with_default_context(|c, es| unsafe {
+			nix_raw::store_get_storedir(c, self.as_ptr(), Some(copy_nix_str), (&raw mut out).cast())
+		})?;
+		let p = Utf8PathBuf::from(out);
+		assert!(p.is_absolute());
+		Ok(p)
+	}
+
+	fn as_ptr(&self) -> *mut c_store {
+		self.0
+	}
 }
 impl Drop for Store {
 	fn drop(&mut self) {
@@ -1060,11 +1064,12 @@
 			self.clone()
 		};
 
-		let drv_path = v
-			.get_field("drvPath")
-			.context("getting drvPath")?
-			.to_string()?;
-		let graph = Arc::new(drv::DrvGraph::resolve(&drv_path)?);
+		let drv_path = Utf8PathBuf::from(
+			v.get_field("drvPath")
+				.context("getting drvPath")?
+				.to_string()?,
+		);
+		let graph = Arc::new(drv::DrvGraph::resolve(&eval_store(), &drv_path)?);
 		let _guard = logging::register_build_graph(&Span::current(), &graph);
 
 		scheduler::build_graph_sync(graph.clone(), vec![output.to_owned()])?;
@@ -1255,8 +1260,12 @@
 	}
 }
 
-struct StorePath(*mut c_store_path);
-impl StorePath {}
+pub struct StorePath(*mut c_store_path);
+impl StorePath {
+	fn as_ptr(&self) -> *mut c_store_path {
+		self.0
+	}
+}
 
 impl Drop for StorePath {
 	fn drop(&mut self) {
modifiedcrates/nix-eval/src/logging.rsdiffbeforeafterboth
after · crates/nix-eval/src/logging.rs
1use std::collections::{HashMap, VecDeque};2use std::fmt::Arguments;3use std::sync::{LazyLock, Mutex};45use camino::{Utf8Path, Utf8PathBuf};6use cxx::ExternType;7use tracing::{8	Level, Span, debug, debug_span, error, error_span, info, info_span, trace, trace_span, warn,9	warn_span,10};11#[cfg(feature = "indicatif")]12use tracing_indicatif::span_ext::IndicatifSpanExt as _;13use vte::Parser;1415use crate::drv::extract_drv_name;1617#[derive(Debug)]18enum ActivityType {19	Unknown = 0,20	CopyPath = 100,21	FileTransfer = 101,22	Realise = 102,23	CopyPaths = 103,24	Builds = 104,25	Build = 105,26	OptimiseStore = 106,27	VerifyPaths = 107,28	Substitute = 108,29	QueryPathInfo = 109,30	PostBuildHook = 110,31	BuildWaiting = 111,32	FetchTree = 112,33}3435fn strip_prefix_suffix<'s, 'p>(a: &'s str, pref: &'p str, suff: &'p str) -> Option<&'s str> {36	a.strip_prefix(pref)?.strip_suffix(suff)37}3839fn parse_path(path: &str) -> Utf8PathBuf {40	Utf8PathBuf::from(strip_prefix_suffix(path, "\x1b[35;1m", "\x1b[0m").unwrap_or(path))41}4243fn parse_drv(drv: &str) -> String {44	let drv = parse_path(drv);45	extract_drv_name(&drv)46}47fn parse_host(host: &str) -> &str {48	if host.is_empty() || host == "local" {49		return "local";50	}51	// https/ssh is the default52	host.strip_prefix("https://").unwrap_or(host)53}5455impl ActivityType {56	fn name(&self) -> &'static str {57		match self {58			ActivityType::Unknown => "nix",59			ActivityType::CopyPath => "nix::copy-path",60			ActivityType::FileTransfer => "nix::file-transfer",61			ActivityType::Realise => "nix::realise",62			ActivityType::CopyPaths => "nix::copy-paths",63			ActivityType::Builds => "nix::builds",64			ActivityType::Build => "nix::build",65			ActivityType::OptimiseStore => "nix::optimise-store",66			ActivityType::VerifyPaths => "nix::verify-paths",67			ActivityType::Substitute => "nix::substitute",68			ActivityType::QueryPathInfo => "nix::query-path-info",69			ActivityType::PostBuildHook => "nix::post-build-hook",70			ActivityType::BuildWaiting => "nix::build-waiting",71			ActivityType::FetchTree => "nix::fetch-tree",72		}73	}74	fn format(75		&self,76		values: &[FieldValue],77		s: &str,78		into: impl FnOnce(Arguments<'_>) -> Span,79	) -> Span {80		use FieldValue::*;81		match (self, values) {82			(ActivityType::QueryPathInfo, [Str(drv), Str(host)]) => {83				let drv = parse_drv(drv);84				let host = parse_host(host);85				debug_span!(target: "nix::query-path-info", "querying", drv, host)86			}87			(ActivityType::Substitute, [Str(drv), Str(host)]) => {88				let drv = parse_drv(drv);89				let host = parse_host(host);90				debug_span!(target: "nix::substitute", "substituting", drv, host)91			}92			(ActivityType::CopyPath, [Str(drv), Str(from), Str(to)]) => {93				let drv = parse_drv(drv);94				let from = parse_host(from);95				let to = parse_host(to);96				debug_span!(target: "nix::copy-path", "copying", drv, from, to)97			}98			(ActivityType::Build, [Str(drv), Str(host), Int(_), Int(_)]) => {99				let drv = parse_drv(drv);100				let host = parse_host(host);101				info_span!(target: "nix::build", "building", drv, host)102			}103			(ActivityType::FileTransfer, [Str(file)]) => {104				info_span!(target: "nix::file-transfer", "downloading", file)105			}106			(ActivityType::Realise, []) => {107				debug_span!(target: "nix::realise", "realising")108			}109			(ActivityType::CopyPaths, []) => {110				debug_span!(target: "nix::copy-paths", "copying paths")111			}112			(ActivityType::Unknown, [])113				if s.starts_with("copying \"") && s.ends_with("\" to the store") =>114			{115				let tree = s116					.trim_start_matches("copying \"")117					.trim_end_matches("\" to the store");118				debug_span!(target: "nix::trees", "copying", tree)119			}120			(ActivityType::Unknown, [])121				if s.starts_with("copying '") && s.ends_with("' to the store") =>122			{123				let tree = s124					.trim_start_matches("copying '")125					.trim_end_matches("' to the store");126				debug_span!(target: "nix::trees", "copying", tree)127			}128			(ActivityType::Unknown, []) if s.starts_with("hashing '") && s.ends_with("'") => {129				let tree = s.trim_start_matches("hashing '").trim_end_matches("'");130				debug_span!(target: "nix::trees", "hashing", tree)131			}132			(ActivityType::Unknown, []) if s.starts_with("connecting to '") && s.ends_with("'") => {133				let host = s134					.trim_start_matches("connecting to '")135					.trim_end_matches("'");136				debug_span!(target: "nix::remote", "connecting", host)137			}138			(ActivityType::Unknown, [])139				if s.starts_with("copying outputs from '") && s.ends_with("'") =>140			{141				let host = s142					.trim_start_matches("copying outputs from '")143					.trim_end_matches("'");144				debug_span!(target: "nix::remote", "copying outputs", host)145			}146			(ActivityType::Unknown, [])147				if s.starts_with("copying dependencies to '") && s.ends_with("'") =>148			{149				let host = s150					.trim_start_matches("copying dependencies to '")151					.trim_end_matches("'");152				debug_span!(target: "nix::remote", "copying dependencies", host)153			}154			(ActivityType::Unknown, [])155				if s.starts_with("waiting for the upload lock to '") && s.ends_with("'") =>156			{157				let host = s158					.trim_start_matches("waiting for the upload lock to '")159					.trim_end_matches("'");160				debug_span!(target: "nix::remote", "waiting for upload lock", host)161			}162			(ActivityType::BuildWaiting, [])163				if s.starts_with("waiting for a machine to build '") && s.ends_with("'") =>164			{165				let drv = parse_drv(166					s.trim_start_matches("waiting for a machine to build '")167						.trim_end_matches("'"),168				);169				debug_span!(target: "nix::build-waiting", "waiting for available builder", drv)170			}171			(ActivityType::Unknown, []) if s == "querying info about missing paths" => {172				debug_span!(target: "nix::remote", "querying")173			}174			_ => into(format_args!("{}({values:?})", self.name())),175		}176	}177	fn from_int(v: u32) -> Self {178		match v {179			0 => Self::Unknown,180			100 => Self::CopyPath,181			101 => Self::FileTransfer,182			102 => Self::Realise,183			103 => Self::CopyPaths,184			104 => Self::Builds,185			105 => Self::Build,186			106 => Self::OptimiseStore,187			107 => Self::VerifyPaths,188			108 => Self::Substitute,189			109 => Self::QueryPathInfo,190			110 => Self::PostBuildHook,191			111 => Self::BuildWaiting,192			112 => Self::FetchTree,193			_ => {194				warn!("unknown nix action: {v}");195				Self::Unknown196			}197		}198	}199}200201#[derive(Debug)]202enum ResultType {203	FileLinked = 100,204	BuildLogLine = 101,205	UntrustedPath = 102,206	CorruptedPath = 103,207	SetPhase = 104,208	Progress = 105,209	SetExpected = 106,210	PostBuildLogLine = 107,211	FetchStatus = 108,212213	Unknown = 999,214}215impl ResultType {216	fn from_int(v: u32) -> Self {217		match v {218			100 => Self::FileLinked,219			101 => Self::BuildLogLine,220			102 => Self::UntrustedPath,221			103 => Self::CorruptedPath,222			104 => Self::SetPhase,223			105 => Self::Progress,224			106 => Self::SetExpected,225			107 => Self::PostBuildLogLine,226			108 => Self::FetchStatus,227228			_ => {229				warn!("unknown nix result: {v}");230				Self::Unknown231			}232		}233	}234}235#[derive(Clone, Copy)]236enum Verbosity {237	Error,238	Warn,239	Notice,240	Info,241	Talkative,242	Chatty,243	Debug,244	Vomit,245}246impl From<Verbosity> for tracing::Level {247	fn from(val: Verbosity) -> Self {248		match val {249			Verbosity::Error => Level::ERROR,250			Verbosity::Warn => Level::WARN,251			Verbosity::Notice => Level::WARN,252			Verbosity::Info => Level::INFO,253			Verbosity::Talkative => Level::DEBUG,254			Verbosity::Chatty => Level::DEBUG,255			Verbosity::Debug => Level::DEBUG,256			Verbosity::Vomit => Level::TRACE,257		}258	}259}260impl Verbosity {261	fn from_int(u: u32) -> Self {262		[263			Self::Error,264			Self::Warn,265			Self::Notice,266			Self::Info,267			Self::Talkative,268			Self::Chatty,269			Self::Debug,270			Self::Vomit,271		]272		.get(u as usize)273		.cloned()274		.unwrap_or_else(|| {275			warn!("unknown log level: {u}");276			Verbosity::Vomit277		})278	}279}280281static NIX_SPAN_MAPPING: LazyLock<Mutex<HashMap<u64, Span>>> =282	LazyLock::new(|| Mutex::new(HashMap::new()));283284struct DrvGraphEntry {285	name: String,286	parent: Option<Utf8PathBuf>,287	span: Option<Span>,288	refcount: usize,289}290291static DRV_GRAPH: LazyLock<Mutex<HashMap<Utf8PathBuf, DrvGraphEntry>>> =292	LazyLock::new(|| Mutex::new(HashMap::new()));293294static ACTIVITY_TO_DRV: LazyLock<Mutex<HashMap<u64, Utf8PathBuf>>> =295	LazyLock::new(|| Mutex::new(HashMap::new()));296297pub struct BuildGraphGuard {298	paths: Vec<Utf8PathBuf>,299}300301impl Drop for BuildGraphGuard {302	fn drop(&mut self) {303		let mut drv_graph = DRV_GRAPH.lock().expect("not poisoned");304		for path in &self.paths {305			if let Some(entry) = drv_graph.get_mut(path) {306				entry.refcount -= 1;307				if entry.refcount == 0 {308					drv_graph.remove(path);309				}310			}311		}312	}313}314315pub fn register_build_graph(parent: &Span, graph: &crate::drv::DrvGraph) -> BuildGraphGuard {316	let mut drv_graph = DRV_GRAPH.lock().expect("not poisoned");317	let mut paths = Vec::new();318319	drv_graph320		.entry(graph.root.clone())321		.and_modify(|e| e.refcount += 1)322		.or_insert_with(|| DrvGraphEntry {323			name: graph.nodes[&graph.root].name.clone(),324			parent: None,325			span: Some(parent.clone()),326			refcount: 1,327		});328	paths.push(graph.root.clone());329330	let mut queue = VecDeque::new();331	queue.push_back(graph.root.clone());332333	let mut visited = std::collections::HashSet::new();334	visited.insert(graph.root.clone());335336	while let Some(path) = queue.pop_front() {337		let Some(node) = graph.nodes.get(&path) else {338			continue;339		};340		for dep_path in node.input_drvs.keys() {341			if !visited.insert(dep_path.clone()) {342				continue;343			}344			let Some(dep_node) = graph.nodes.get(dep_path) else {345				continue;346			};347			if let Some(entry) = drv_graph.get_mut(dep_path) {348				entry.refcount += 1;349			} else {350				drv_graph.insert(351					dep_path.clone(),352					DrvGraphEntry {353						name: dep_node.name.clone(),354						parent: Some(path.clone()),355						span: None,356						refcount: 1,357					},358				);359			}360			paths.push(dep_path.clone());361			queue.push_back(dep_path.clone());362		}363	}364365	BuildGraphGuard { paths }366}367368fn ensure_drv_span(drv_path: &Utf8Path) -> Option<Span> {369	let mut drv_graph = DRV_GRAPH.lock().expect("not poisoned");370371	if let Some(span) = drv_graph.get(drv_path).and_then(|e| e.span.clone()) {372		return Some(span);373	}374375	let mut chain = vec![];376	let mut current = drv_path.to_owned();377	loop {378		let Some(entry) = drv_graph.get(&current) else {379			break;380		};381		if entry.span.is_some() {382			chain.push(current);383			break;384		}385		chain.push(current.clone());386		match &entry.parent {387			Some(p) => current = p.clone(),388			None => break,389		}390	}391392	if chain.is_empty() {393		return None;394	}395396	for i in (0..chain.len()).rev() {397		let path = &chain[i];398		if drv_graph.get(path).unwrap().span.is_some() {399			continue;400		}401		let parent_span = chain402			.get(i + 1)403			.and_then(|p| drv_graph.get(p))404			.and_then(|e| e.span.clone());405		let name = drv_graph.get(path).unwrap().name.clone();406		let span = {407			let _enter = parent_span.as_ref().map(|s| s.enter());408			info_span!(target: "nix::build", "building", drv = %name)409		};410		drv_graph.get_mut(path).unwrap().span = Some(span);411	}412413	drv_graph.get(drv_path).and_then(|e| e.span.clone())414}415416#[derive(Debug)]417enum FieldValue {418	Int(i32),419	Str(String),420}421422struct StartActivityBuilder {423	activity_id: u64,424	verbosity: Verbosity,425	typ: ActivityType,426	fields: Vec<FieldValue>,427}428impl StartActivityBuilder {429	fn add_int_field(&mut self, i: i32) {430		self.fields.push(FieldValue::Int(i));431	}432	fn add_string_field(&mut self, v: &[u8]) {433		let v = String::from_utf8_lossy(v);434		self.fields.push(FieldValue::Str(v.to_string()));435	}436	fn emit(&mut self, parent: u64, s: &str) {437		let graph_span = if matches!(self.typ, ActivityType::Build) {438			self.fields.first().and_then(|f| match f {439				FieldValue::Str(drv_path) => {440					let clean = parse_path(drv_path);441					let span = ensure_drv_span(&clean);442					if span.is_some() {443						ACTIVITY_TO_DRV444							.lock()445							.expect("not poisoned")446							.insert(self.activity_id, clean.to_owned());447					}448					span449				}450				_ => None,451			})452		} else {453			None454		};455456		let mut mapping = NIX_SPAN_MAPPING.lock().expect("not poisoned");457458		let span = if let Some(span) = graph_span {459			#[cfg(feature = "indicatif")]460			span.pb_start();461			span462		} else {463			let parent = mapping.get(&parent);464			let _in_parent = parent.map(|p| p.enter());465			let level: Level = self.verbosity.into();466			if level == Level::ERROR {467				self.typ468					.format(&self.fields, s, |v| error_span!("action", v))469			} else if level == Level::WARN {470				self.typ471					.format(&self.fields, s, |v| warn_span!("action", v))472			} else if level == Level::INFO {473				self.typ474					.format(&self.fields, s, |v| info_span!("action", v))475			} else if level == Level::DEBUG {476				self.typ477					.format(&self.fields, s, |v| debug_span!("action", v))478			} else {479				self.typ480					.format(&self.fields, s, |v| trace_span!("action", v))481			}482		};483		if !s.trim().is_empty() {484			let s = ansi_filter(s);485			#[cfg(feature = "indicatif")]486			{487				span.pb_set_message(&s);488			}489			let _e = span.enter();490			let level: Level = self.verbosity.into();491			if level == Level::ERROR {492				error!(target: "nix", "{}", s)493			} else if level == Level::WARN {494				warn!(target: "nix", "{}", s)495			} else if level == Level::INFO {496				info!(target: "nix", "{}", s)497			} else if level == Level::DEBUG {498				if s != "querying info about missing paths" {499					debug!(target: "nix", "{}", s)500				}501			} else {502				trace!(target: "nix", "{}", s)503			}504		} else {505			#[cfg(feature = "indicatif")]506			{507				span.pb_start();508			}509		}510		mapping.insert(self.activity_id, span);511	}512	fn emit_result(&mut self, ty: u32) {513		let mapping = NIX_SPAN_MAPPING.lock().expect("not poisoned");514515		let Some(parent) = mapping.get(&self.activity_id) else {516			panic!("unexpected result for dead parent");517		};518519		let _in_parent = parent.enter();520		let res = ResultType::from_int(ty);521522		use FieldValue::*;523		match (&res, self.fields.as_slice()) {524			// ResultType::FileLinked => todo!(),525			(ResultType::BuildLogLine, [Str(s)]) => {526				let s = ansi_filter(s);527				info!("{s}");528			}529			// ResultType::UntrustedPath => todo!(),530			// ResultType::CorruptedPath => todo!(),531			// ResultType::SetPhase => todo!(),532			(ResultType::SetExpected, [Int(act_ty), Int(_expected)]) => {533				let _act_ty = ActivityType::from_int(*act_ty as u32);534			}535			(ResultType::SetPhase, [Str(phase)]) => {536				// parent.pb_set_message(phase);537				debug!(target: "nix::phase", phase)538			}539			(ResultType::Progress, [Int(_done), Int(_expected), Int(_), Int(_)]) => {540				#[cfg(feature = "indicatif")]541				{542					parent.pb_set_length(*_expected as u64);543					parent.pb_set_position(*_done as u64);544				}545			}546			_ => warn!("unknown progress report: {:?}({:?})", &res, &self.fields),547		}548	}549}550fn new_start_activity(activity_id: u64, lvl: u32, typ: u32) -> Box<StartActivityBuilder> {551	Box::new(StartActivityBuilder {552		activity_id,553		verbosity: Verbosity::from_int(lvl),554		typ: ActivityType::from_int(typ),555		fields: vec![],556	})557}558559fn emit_warn(v: &str) {560	warn!(target: "nix::eval", "{v}")561}562fn emit_stop(v: u64) {563	{564		let mut mapping = NIX_SPAN_MAPPING.lock().expect("not poisoned");565		mapping.remove(&v);566	}567	if let Some(drv_path) = ACTIVITY_TO_DRV.lock().expect("not poisoned").remove(&v) {568		if let Some(entry) = DRV_GRAPH.lock().expect("not poisoned").get_mut(&drv_path) {569			entry.span = None;570		}571	}572}573fn emit_log(lvl: u32, v: &[u8]) {574	let verbosity = Verbosity::from_int(lvl);575	let level: Level = verbosity.into();576	let v = String::from_utf8_lossy(v);577	if level == Level::ERROR {578		error!(target: "nix", "{v}")579	} else if level == Level::WARN {580		warn!(target: "nix", "{v}")581	} else if level == Level::INFO {582		info!(target: "nix", "{v}")583	} else if level == Level::DEBUG {584		if v != "querying info about missing paths" {585			debug!(target: "nix", "{v}")586		}587	} else {588		trace!(target: "nix", "{v}")589	}590}591592struct AnsiFiltered {593	output: String,594}595impl vte::Perform for AnsiFiltered {596	fn print(&mut self, c: char) {597		self.output.push(c);598	}599600	fn execute(&mut self, byte: u8) {601		// We don't want \r, bells, etc602		if byte == b'\n' {603			self.output.push('\n');604		} else if byte == b'\t' {605			// TODO: align output to the correct multiplier?606			self.output.push('\t');607		}608	}609610	fn osc_dispatch(&mut self, _params: &[&[u8]], _bell_terminated: bool) {}611	fn esc_dispatch(&mut self, _intermediates: &[u8], _ignore: bool, _byte: u8) {}612613	fn csi_dispatch(614		&mut self,615		params: &vte::Params,616		_intermediates: &[u8],617		_ignore: bool,618		action: char,619	) {620		use std::fmt::Write;621		if action != 'm' {622			// Only plain colors are enabled, everything other might corrupt the output623			return;624		}625		self.output.push_str("\x1b[");626		for (i, par) in params.iter().enumerate() {627			if i != 0 {628				let _ = write!(self.output, ";");629			}630			for (i, sub) in par.iter().enumerate() {631				if i != 0 {632					let _ = write!(self.output, ":");633				}634				let _ = write!(self.output, "{sub}");635			}636		}637		self.output.push(action);638	}639}640fn ansi_filter(i: &str) -> String {641	let mut out = AnsiFiltered {642		output: String::new(),643	};644	let mut parser = Parser::new();645646	// For some reason it gets stuck with longer inputs647	for chunk in i.as_bytes().chunks(50) {648		parser.advance(&mut out, chunk);649	}650651	out.output652}653654#[derive(Debug)]655pub struct StackFrame {656	pub msg: String,657	pub pos: String,658}659660#[derive(Debug)]661pub struct ErrorInfoBuilder {662	level: Level,663	msg: String,664	pub stack_frames: Vec<StackFrame>,665}666fn new_error_info(lvl: u32, v: &[u8]) -> Box<ErrorInfoBuilder> {667	let verbosity = Verbosity::from_int(lvl);668	let level: Level = verbosity.into();669	let v = String::from_utf8_lossy(v);670	Box::new(ErrorInfoBuilder {671		level,672		msg: v.to_string(),673		stack_frames: Vec::new(),674	})675}676impl ErrorInfoBuilder {677	fn push_stack_frame(&mut self, v: &[u8], pos: &[u8]) {678		let v = String::from_utf8_lossy(v);679		let pos = String::from_utf8_lossy(pos);680		self.stack_frames.push(StackFrame {681			msg: v.to_string(),682			pos: pos.to_string(),683		});684	}685	fn emit_error_info(&mut self) {686		error!("{}", self.msg);687		for frame in &self.stack_frames {688			error!("  {} at {}", frame.msg, frame.pos)689		}690	}691}692693#[cxx::bridge]694pub mod nix_logging_cxx {695	extern "Rust" {696		type StartActivityBuilder;697		fn new_start_activity(activity_id: u64, lvl: u32, typ: u32) -> Box<StartActivityBuilder>;698		fn add_int_field(&mut self, i: i32);699		fn add_string_field(&mut self, v: &[u8]);700		fn emit(&mut self, parent: u64, s: &str);701		fn emit_result(&mut self, ty: u32);702	}703	extern "Rust" {704		type ErrorInfoBuilder;705		fn new_error_info(lvl: u32, v: &[u8]) -> Box<ErrorInfoBuilder>;706		fn push_stack_frame(&mut self, v: &[u8], pos: &[u8]);707		fn emit_error_info(&mut self);708	}709	extern "Rust" {710		fn emit_warn(v: &str);711		fn emit_stop(id: u64);712		fn emit_log(lvl: u32, v: &[u8]);713	}714	unsafe extern "C++" {715		include!("nix-eval/src/logging.hh");716717		type nix_c_context = crate::nix_raw::c_context;718719		fn apply_tracing_logger();720		unsafe fn extract_error_info(ctx: *const nix_c_context) -> Box<ErrorInfoBuilder>;721	}722}723724unsafe impl ExternType for crate::nix_raw::c_context {725	type Id = cxx::type_id!("nix_c_context");726727	type Kind = cxx::kind::Opaque;728}
modifiedcrates/nix-eval/src/scheduler.rsdiffbeforeafterboth
--- a/crates/nix-eval/src/scheduler.rs
+++ b/crates/nix-eval/src/scheduler.rs
@@ -1,12 +1,16 @@
 use std::collections::{HashMap, HashSet};
+use std::mem;
 use std::sync::Arc;
 
 use anyhow::{Context, Result, bail};
+use camino::{Utf8Path, Utf8PathBuf};
 use futures::stream::{FuturesUnordered, StreamExt};
 use tokio::sync::{Semaphore, broadcast};
+use tokio::task::spawn_blocking;
 use tracing::{debug, info, instrument, warn};
 
 use crate::drv::DrvGraph;
+use crate::{Store, eval_store};
 
 #[derive(Clone, Debug)]
 pub enum BuildEvent {
@@ -17,31 +21,32 @@
 		satisfied: usize,
 	},
 	DrvStarted {
-		drv_path: String,
+		drv_path: Utf8PathBuf,
 		name: String,
 		wanted: Vec<String>,
 	},
 	DrvSkipped {
-		drv_path: String,
+		drv_path: Utf8PathBuf,
 		name: String,
 	},
 	DrvFinished {
-		drv_path: String,
+		drv_path: Utf8PathBuf,
 		name: String,
 	},
 	DrvFailed {
-		drv_path: String,
+		drv_path: Utf8PathBuf,
 		name: String,
 		error: String,
 	},
 	DrvCancelled {
-		drv_path: String,
+		drv_path: Utf8PathBuf,
 		name: String,
-		failed_dep: String,
+		failed_dep: Utf8PathBuf,
 	},
 }
 
 pub struct Scheduler {
+	store: Arc<Store>,
 	parallelism: usize,
 	events: broadcast::Sender<BuildEvent>,
 }
@@ -51,6 +56,7 @@
 		let parallelism = parallelism.max(1);
 		let (events, _) = broadcast::channel(1024);
 		Self {
+			store: eval_store(),
 			parallelism,
 			events,
 		}
@@ -71,7 +77,7 @@
 	async fn substitute_prepass(
 		&self,
 		graph: &DrvGraph,
-		wanted: &HashMap<String, Vec<String>>,
+		wanted: &HashMap<Utf8PathBuf, Vec<String>>,
 	) -> Result<()> {
 		let paths = collect_substitute_paths(graph, wanted);
 		if paths.is_empty() {
@@ -82,7 +88,8 @@
 			.send(BuildEvent::SubstitutePrepassStarted { paths: paths.len() });
 		debug!("substitute pre-pass: {} paths", paths.len());
 
-		let satisfied = tokio::task::spawn_blocking(move || crate::substitute_paths(&paths))
+		let store = self.store.clone();
+		let satisfied = spawn_blocking(move || store.substitute_paths(&paths))
 			.await
 			.expect("substitute pre-pass task should not panic")?;
 
@@ -95,14 +102,14 @@
 	async fn build_topo(
 		&self,
 		graph: &Arc<DrvGraph>,
-		wanted: HashMap<String, Vec<String>>,
+		wanted: HashMap<Utf8PathBuf, Vec<String>>,
 	) -> Result<()> {
-		let mut indeg: HashMap<String, usize> = graph
+		let mut indeg: HashMap<Utf8PathBuf, usize> = graph
 			.nodes
 			.iter()
 			.map(|(k, n)| (k.clone(), n.input_drvs.len()))
 			.collect();
-		let mut dependents: HashMap<String, Vec<String>> = HashMap::new();
+		let mut dependents: HashMap<Utf8PathBuf, Vec<Utf8PathBuf>> = HashMap::new();
 		for (path, node) in &graph.nodes {
 			for dep in node.input_drvs.keys() {
 				dependents
@@ -113,18 +120,18 @@
 		}
 
 		let sem = Arc::new(Semaphore::new(self.parallelism));
-		let mut ready: Vec<String> = indeg
+		let mut ready: Vec<Utf8PathBuf> = indeg
 			.iter()
 			.filter(|(_, d)| **d == 0)
 			.map(|(k, _)| k.clone())
 			.collect();
 		let mut in_flight = FuturesUnordered::new();
-		let mut failed: HashMap<String, String> = HashMap::new();
+		let mut failed: HashMap<Utf8PathBuf, String> = HashMap::new();
 		// Tainted = transitively depends on a failed drv
-		let mut tainted: HashMap<String, String> = HashMap::new();
+		let mut tainted: HashMap<Utf8PathBuf, Utf8PathBuf> = HashMap::new();
 
 		loop {
-			let batch: Vec<String> = std::mem::take(&mut ready);
+			let batch: Vec<Utf8PathBuf> = mem::take(&mut ready);
 			for path in batch {
 				if let Some(failed_dep) = tainted.get(&path) {
 					let name = graph
@@ -145,6 +152,7 @@
 				let events = self.events.clone();
 				let graph = graph.clone();
 				let wanted_here = wanted.get(&path).cloned().unwrap_or_default();
+				let store = self.store.clone();
 				in_flight.push(tokio::spawn(async move {
 					let _permit = sem.acquire_owned().await.expect("semaphore not closed");
 					let node = graph
@@ -158,7 +166,7 @@
 						&& wanted_here.iter().all(|o| {
 							node.outputs
 								.get(o)
-								.map(|p| crate::is_valid_path(p).unwrap_or(false))
+								.map(|p| store.is_valid_path(p))
 								.unwrap_or(false)
 						});
 					if all_valid {
@@ -176,8 +184,9 @@
 					});
 
 					let path_for_build = path.clone();
-					let res = tokio::task::spawn_blocking(move || {
-						crate::build_drv_outputs(&path_for_build, &wanted_here)
+					let store = store.clone();
+					let res = spawn_blocking(move || {
+						store.build_drv_outputs(&path_for_build, &wanted_here)
 					})
 					.await
 					.expect("build task should not panic");
@@ -259,10 +268,10 @@
 }
 
 fn propagate_done(
-	dependents: &HashMap<String, Vec<String>>,
-	indeg: &mut HashMap<String, usize>,
-	ready: &mut Vec<String>,
-	finished: &str,
+	dependents: &HashMap<Utf8PathBuf, Vec<Utf8PathBuf>>,
+	indeg: &mut HashMap<Utf8PathBuf, usize>,
+	ready: &mut Vec<Utf8PathBuf>,
+	finished: &Utf8Path,
 ) {
 	if let Some(deps) = dependents.get(finished) {
 		for d in deps {
@@ -276,11 +285,11 @@
 }
 
 fn mark_tainted(
-	dependents: &HashMap<String, Vec<String>>,
-	failed: &str,
-	tainted: &mut HashMap<String, String>,
+	dependents: &HashMap<Utf8PathBuf, Vec<Utf8PathBuf>>,
+	failed: &Utf8Path,
+	tainted: &mut HashMap<Utf8PathBuf, Utf8PathBuf>,
 ) {
-	let mut queue: Vec<String> = dependents.get(failed).cloned().unwrap_or_default();
+	let mut queue: Vec<Utf8PathBuf> = dependents.get(failed).cloned().unwrap_or_default();
 	while let Some(node) = queue.pop() {
 		if tainted
 			.entry(node.clone())
@@ -298,20 +307,17 @@
 	}
 }
 
-fn path_to_root(graph: &DrvGraph, from: &str) -> Vec<String> {
-	let mut dependents: HashMap<&str, Vec<&str>> = HashMap::new();
+fn path_to_root(graph: &DrvGraph, from: &Utf8Path) -> Vec<String> {
+	let mut dependents: HashMap<&Utf8Path, Vec<&Utf8Path>> = HashMap::new();
 	for (path, node) in &graph.nodes {
 		for dep in node.input_drvs.keys() {
-			dependents
-				.entry(dep.as_str())
-				.or_default()
-				.push(path.as_str());
+			dependents.entry(dep).or_default().push(path);
 		}
 	}
 
 	let mut chain: Vec<String> = vec![node_name(graph, from)];
 	let mut cur = from;
-	let mut seen: HashSet<&str> = HashSet::new();
+	let mut seen: HashSet<&Utf8Path> = HashSet::new();
 	seen.insert(cur);
 	while cur != graph.root.as_str() {
 		let Some(next) = dependents.get(cur).and_then(|v| v.first().copied()) else {
@@ -326,19 +332,19 @@
 	chain
 }
 
-fn node_name(graph: &DrvGraph, path: &str) -> String {
+fn node_name(graph: &DrvGraph, path: &Utf8Path) -> String {
 	graph
 		.nodes
 		.get(path)
 		.map(|n| n.name.clone())
-		.unwrap_or_else(|| path.to_owned())
+		.unwrap_or_else(|| path.to_string())
 }
 
 fn collect_substitute_paths(
 	graph: &DrvGraph,
-	wanted: &HashMap<String, Vec<String>>,
-) -> Vec<String> {
-	let mut paths: HashSet<String> = HashSet::new();
+	wanted: &HashMap<Utf8PathBuf, Vec<String>>,
+) -> Vec<Utf8PathBuf> {
+	let mut paths: HashSet<Utf8PathBuf> = HashSet::new();
 	for node in graph.nodes.values() {
 		for src in &node.input_srcs {
 			paths.insert(src.clone());
modifiedcrates/remowt-fleet/src/lib.rsdiffbeforeafterboth
--- a/crates/remowt-fleet/src/lib.rs
+++ b/crates/remowt-fleet/src/lib.rs
@@ -1,13 +1,15 @@
 use std::path::PathBuf;
 
 use anyhow::{Context as _, Result};
+use bifrostlink::declarative::endpoints;
 use bifrostlink::Config;
-use bifrostlink::declarative::endpoints;
 use camino::Utf8PathBuf;
+use nix_eval::eval_store;
 use remowt_client::Remowt;
 use remowt_endpoints::nix_daemon::NixDaemonClient;
 use serde::{Deserialize, Serialize};
 use tokio::net::UnixListener;
+use tokio::task::spawn_blocking;
 use tracing::error;
 
 pub struct Nix;
@@ -35,9 +37,10 @@
 		profile: String,
 		store_path: Utf8PathBuf,
 	) -> Result<(), NixError> {
-		tokio::task::spawn_blocking(move || nix_eval::switch_profile(&profile, &store_path))
+		let store = eval_store();
+		spawn_blocking(move || store.switch_profile(&profile, &store_path))
 			.await
-			.map_err(|e| NixError::Profile(e.to_string()))?
+			.expect("switch_profile panicked")
 			.map_err(|e| NixError::Profile(e.to_string()))
 	}
 
@@ -47,11 +50,12 @@
 		store_path: Utf8PathBuf,
 		key_file: Utf8PathBuf,
 	) -> Result<(), NixError> {
-		tokio::task::spawn_blocking(move || {
-			nix_eval::sign_closure(store_path.as_str(), key_file.as_str())
+		spawn_blocking(move || {
+			let store = eval_store();
+			store.sign_closure(&store_path, &key_file)
 		})
 		.await
-		.map_err(|e| NixError::Sign(e.to_string()))?
+		.expect("store signing panicked")
 		.map_err(|e| NixError::Sign(e.to_string()))
 	}
 
@@ -60,11 +64,11 @@
 		&self,
 		profile: String,
 	) -> Result<Vec<nix_eval::ProfileGeneration>, NixError> {
-		tokio::task::spawn_blocking(move || {
+		spawn_blocking(move || {
 			nix_eval::list_generations(&format!("/nix/var/nix/profiles/{profile}"))
 		})
 		.await
-		.map_err(|e| NixError::ListGenerations(e.to_string()))?
+		.expect("generation listing panicked")
 		.map_err(|e| NixError::ListGenerations(e.to_string()))
 	}
 }