6 files changed
--- a/cmds/fleet/Cargo.toml
+++ b/cmds/fleet/Cargo.toml
@@ -50,7 +50,7 @@
tracing-opentelemetry.workspace = true
[features]
-default = []
+default = ["indicatif"]
# Not quite stable
indicatif = [
"dep:tracing-indicatif",
--- a/cmds/fleet/src/main.rs
+++ b/cmds/fleet/src/main.rs
@@ -120,7 +120,8 @@
let indicatif_layer = {
use std::time::Duration;
- IndicatifLayer::new().with_progress_style(
+ IndicatifLayer::new().with_max_progress_bars(10, Some(ProgressStyle::default_spinner()))
+ .with_progress_style(
ProgressStyle::with_template(
"{color_start}{span_child_prefix} {span_name}{{{span_fields}}}{color_end} {wide_msg} {color_start}{download_progress} {elapsed}{color_end}",
)
--- a/crates/nix-eval/build.rs
+++ b/crates/nix-eval/build.rs
@@ -18,6 +18,7 @@
"nix-util",
"nix-util-c",
"nix-store",
+ "nix-store-c",
"nix-expr",
"nix-flake",
"nix-fetchers",
@@ -72,6 +73,12 @@
.include_paths
.into_iter()
.chain(
+ pkg_config::probe_library("nix-store-c")
+ .expect("nix-store-c")
+ .include_paths
+ .into_iter(),
+ )
+ .chain(
pkg_config::probe_library("nix-flake-c")
.expect("nix-flake-c")
.include_paths
--- /dev/null
+++ b/crates/nix-eval/src/drv.rs
@@ -0,0 +1,150 @@
+use std::collections::{HashMap, HashSet, VecDeque};
+use std::ffi::CString;
+
+use anyhow::{Result, bail};
+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}")
+ }
+}
+
+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)
+ });
+ unsafe { crate::nix_raw::store_path_free(store_path) };
+ let drv = drv?;
+ if drv.is_null() {
+ bail!("failed to read derivation from {drv_path}");
+ }
+ Ok(Self(drv))
+ }
+
+ pub fn to_json_string(&self) -> Result<String> {
+ let mut out = String::new();
+ with_store_context(|c, _, _| unsafe {
+ derivation_to_json(c, self.0, Some(copy_nix_str), (&raw mut out).cast())
+ })?;
+ Ok(out)
+ }
+
+ pub fn parsed(&self) -> Result<DrvParsed> {
+ let s = self.to_json_string()?;
+ Ok(serde_json::from_str(&s)?)
+ }
+}
+
+impl Drop for Derivation {
+ fn drop(&mut self) {
+ unsafe { derivation_free(self.0) };
+ }
+}
+
+#[derive(Debug, Deserialize)]
+pub struct DrvParsed {
+ pub inputs: DrvInputs,
+ pub outputs: HashMap<String, serde_json::Value>,
+}
+
+#[derive(Debug, Deserialize)]
+pub struct DrvInputs {
+ #[serde(default)]
+ pub srcs: Vec<String>,
+ #[serde(default)]
+ pub drvs: HashMap<String, DrvInputEntry>,
+}
+
+#[derive(Debug, Deserialize)]
+pub struct DrvInputEntry {
+ pub outputs: Vec<String>,
+}
+
+#[derive(Debug)]
+pub struct DrvGraph {
+ pub root: String,
+ pub nodes: HashMap<String, DrvNode>,
+}
+
+#[derive(Debug)]
+pub struct DrvNode {
+ pub name: String,
+ pub input_drvs: HashMap<String, Vec<String>>,
+ pub input_srcs: Vec<String>,
+ pub outputs: Vec<String>,
+}
+
+impl DrvGraph {
+ pub fn resolve(drv_path: &str) -> Result<Self> {
+ let sd = store_dir()?;
+ let root = to_absolute_store_path(&sd, drv_path);
+
+ let mut nodes = HashMap::new();
+ let mut queue = VecDeque::new();
+ let mut visited = HashSet::new();
+ queue.push_back(root.clone());
+ visited.insert(root.clone());
+
+ while let Some(path) = queue.pop_front() {
+ let drv = Derivation::from_path(&path)?;
+ let parsed = drv.parsed()?;
+
+ let input_drvs: HashMap<String, Vec<String>> = parsed
+ .inputs
+ .drvs
+ .into_iter()
+ .map(|(k, v)| (to_absolute_store_path(&sd, &k), v.outputs))
+ .collect();
+
+ for dep_path in input_drvs.keys() {
+ if visited.insert(dep_path.clone()) {
+ queue.push_back(dep_path.clone());
+ }
+ }
+
+ nodes.insert(
+ path.clone(),
+ DrvNode {
+ name: extract_drv_name(&path),
+ input_drvs,
+ input_srcs: parsed.inputs.srcs,
+ outputs: parsed.outputs.into_keys().collect(),
+ },
+ );
+ }
+
+ Ok(Self { root, nodes })
+ }
+}
+
+fn extract_drv_name(drv_path: &str) -> String {
+ drv_path
+ .rsplit('/')
+ .next()
+ .and_then(|f| f.strip_suffix(".drv"))
+ .and_then(|f| f.split_once('-').map(|(_, name)| name))
+ .unwrap_or(drv_path)
+ .to_owned()
+}
13use std::mem::transmute;13use std::mem::transmute;
1414
15pub use anyhow::Result;15pub use anyhow::Result;
16use tracing::{Instrument, info, instrument, warn};16use tracing::{Span, instrument, warn};
1717
18use self::logging::{ErrorInfoBuilder, nix_logging_cxx};18use self::logging::{ErrorInfoBuilder, nix_logging_cxx};
19use self::nix_cxx::set_fetcher_setting;19use self::nix_cxx::set_fetcher_setting;
43};44};
4445
4546
47pub mod drv;
46pub mod logging;48pub mod logging;
47#[doc(hidden)]49#[doc(hidden)]
48pub mod macros;50pub mod macros;
321thread_local! {323thread_local! {
322 static THREAD_STATE: RefCell<ThreadState> = RefCell::new(ThreadState::new().expect("thread state init shouldn't fail"));324 static THREAD_STATE: RefCell<ThreadState> = RefCell::new(ThreadState::new().expect("thread state init shouldn't fail"));
323}325}
324fn with_default_context<T>(f: impl FnOnce(*mut c_context, *mut c_eval_state) -> T) -> Result<T> {326pub(crate) fn with_default_context<T>(f: impl FnOnce(*mut c_context, *mut c_eval_state) -> T) -> Result<T> {
325 let global = &GLOBAL_STATE.state;327 let global = &GLOBAL_STATE.state;
326 let (ctx, state) = THREAD_STATE.with_borrow_mut(|w| (w.ctx.0, global.0));328 let (ctx, state) = THREAD_STATE.with_borrow_mut(|w| (w.ctx.0, global.0));
327 let mut ctx = NixContext(ctx);329 let mut ctx = NixContext(ctx);
331 v333 v
332}334}
335
336
337
338pub(crate) fn with_store_context<T>(
339 f: impl FnOnce(*mut c_context, *mut c_store, *mut c_eval_state) -> T,
340) -> Result<T> {
341 let global = &GLOBAL_STATE;
342 let (ctx, store, state) =
343 THREAD_STATE.with_borrow_mut(|w| (w.ctx.0, global.store.0, global.state.0));
344 let mut ctx = NixContext(ctx);
345 let v = ctx.run_in_context(|c| f(c, store, state));
346 std::mem::forget(ctx);
347 v
348}
333349
334pub fn set_setting(s: &CStr, v: &CStr) -> Result<()> {350pub fn set_setting(s: &CStr, v: &CStr) -> Result<()> {
335 with_default_context(|c, _| unsafe { setting_set(c, s.as_ptr(), v.as_ptr()) }).map(|_| ())351 with_default_context(|c, _| unsafe { setting_set(c, s.as_ptr(), v.as_ptr()) }).map(|_| ())
423 }439 }
424}440}
425441
426unsafe extern "C" fn copy_nix_str(start: *const c_char, n: c_uint, user_data: *mut c_void) {442pub(crate) unsafe extern "C" fn copy_nix_str(start: *const c_char, n: c_uint, user_data: *mut c_void) {
427 let s = unsafe { slice::from_raw_parts(start.cast::<u8>(), n as usize) };443 let s = unsafe { slice::from_raw_parts(start.cast::<u8>(), n as usize) };
428 let s = std::str::from_utf8(s).expect("c string has invalid utf-8");444 let s = std::str::from_utf8(s).expect("c string has invalid utf-8");
429 unsafe { *user_data.cast::<String>() = s.to_owned() };445 unsafe { *user_data.cast::<String>() = s.to_owned() };
836 })?;852 })?;
837 Ok(out)853 Ok(out)
838 }854 }
855 #[instrument(name = "build", skip(self), fields(output))]
839 pub fn build(&self, output: &str) -> Result<PathBuf> {856 pub fn build(&self, output: &str) -> Result<PathBuf> {
840 if !self.is_derivation() {857 if !self.is_derivation() {
841 bail!("expected derivation to build")858 bail!("expected derivation to build")
854 self.clone()871 self.clone()
855 };872 };
873
874 let drv_path = v
875 .get_field("drvPath")
876 .context("getting drvPath")?
877 .to_string()?;
878 let graph = drv::DrvGraph::resolve(&drv_path)?;
879 let _guard = logging::register_build_graph(&Span::current(), &graph);
880
856 881
857 let s = v.builtin_to_string()?;882 let s = v.builtin_to_string()?;
858 let rs = s.to_realised_string()?;883 let rs = s.to_realised_string()?;
859 let drv_path = rs.as_str().to_owned();884 let out_path = rs.as_str().to_owned();
860 Ok(PathBuf::from(drv_path))885 Ok(PathBuf::from(out_path))
861 }886 }
862 pub fn as_json<T: DeserializeOwned>(&self) -> Result<T> {887 pub fn as_json<T: DeserializeOwned>(&self) -> Result<T> {
863 let to_json = Self::eval("builtins.toJSON")?;888 let to_json = Self::eval("builtins.toJSON")?;
1103 let test_result: String = nix_go_json!(builtins.uppercaseSuffix2("test")("suffix"));1128 let test_result: String = nix_go_json!(builtins.uppercaseSuffix2("test")("suffix"));
1104 assert_eq!(test_result, "TESTsuffix");1129 assert_eq!(test_result, "TESTsuffix");
11051130
1106 let nix_ctx = NixContext::new();1131 let drv_path = nix_go!(attrs.packages["x86_64-linux"]["fleet-install-secrets"].drvPath)
1132 .to_string()?;
1107 let store = GLOBAL_STATE.store.parse_path(s.as_c_str())?;1133 let graph = drv::DrvGraph::resolve(&drv_path)?;
11081134 eprintln!(
1109 1135 "fleet-install-secrets dependency graph: {} nodes",
1136 graph.nodes.len()
1137 );
1138 for (path, node) in &graph.nodes {
1139 if !node.input_drvs.is_empty() {
1140 eprintln!(" {} ({} deps)", node.name, node.input_drvs.len());
1141 }
1142 }
11101143
1111 Ok(())1144 Ok(())
1112}1145}
--- a/crates/nix-eval/src/logging.rs
+++ b/crates/nix-eval/src/logging.rs
@@ -1,4 +1,4 @@
-use std::collections::HashMap;
+use std::collections::{HashMap, VecDeque};
use std::fmt::Arguments;
use std::sync::{LazyLock, Mutex};
@@ -285,6 +285,135 @@
static NIX_SPAN_MAPPING: LazyLock<Mutex<HashMap<u64, Span>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
+struct DrvGraphEntry {
+ name: String,
+ parent: Option<String>,
+ span: Option<Span>,
+ refcount: usize,
+}
+
+static DRV_GRAPH: LazyLock<Mutex<HashMap<String, DrvGraphEntry>>> =
+ LazyLock::new(|| Mutex::new(HashMap::new()));
+
+static ACTIVITY_TO_DRV: LazyLock<Mutex<HashMap<u64, String>>> =
+ LazyLock::new(|| Mutex::new(HashMap::new()));
+
+pub struct BuildGraphGuard {
+ paths: Vec<String>,
+}
+
+impl Drop for BuildGraphGuard {
+ fn drop(&mut self) {
+ let mut drv_graph = DRV_GRAPH.lock().expect("not poisoned");
+ for path in &self.paths {
+ if let Some(entry) = drv_graph.get_mut(path) {
+ entry.refcount -= 1;
+ if entry.refcount == 0 {
+ drv_graph.remove(path);
+ }
+ }
+ }
+ }
+}
+
+pub fn register_build_graph(parent: &Span, graph: &crate::drv::DrvGraph) -> BuildGraphGuard {
+ let mut drv_graph = DRV_GRAPH.lock().expect("not poisoned");
+ let mut paths = Vec::new();
+
+ drv_graph
+ .entry(graph.root.clone())
+ .and_modify(|e| e.refcount += 1)
+ .or_insert_with(|| DrvGraphEntry {
+ name: graph.nodes[&graph.root].name.clone(),
+ parent: None,
+ span: Some(parent.clone()),
+ refcount: 1,
+ });
+ paths.push(graph.root.clone());
+
+ let mut queue = VecDeque::new();
+ queue.push_back(graph.root.clone());
+
+ let mut visited = std::collections::HashSet::new();
+ visited.insert(graph.root.clone());
+
+ while let Some(path) = queue.pop_front() {
+ let Some(node) = graph.nodes.get(&path) else {
+ continue;
+ };
+ for dep_path in node.input_drvs.keys() {
+ if !visited.insert(dep_path.clone()) {
+ continue;
+ }
+ let Some(dep_node) = graph.nodes.get(dep_path) else {
+ continue;
+ };
+ if let Some(entry) = drv_graph.get_mut(dep_path) {
+ entry.refcount += 1;
+ } else {
+ drv_graph.insert(dep_path.clone(), DrvGraphEntry {
+ name: dep_node.name.clone(),
+ parent: Some(path.clone()),
+ span: None,
+ refcount: 1,
+ });
+ }
+ paths.push(dep_path.clone());
+ queue.push_back(dep_path.clone());
+ }
+ }
+
+ BuildGraphGuard { paths }
+}
+
+fn ensure_drv_span(drv_path: &str) -> Option<Span> {
+ let mut drv_graph = DRV_GRAPH.lock().expect("not poisoned");
+
+ if let Some(span) = drv_graph.get(drv_path).and_then(|e| e.span.clone()) {
+ return Some(span);
+ }
+
+ let mut chain = vec![];
+ let mut current = drv_path.to_owned();
+ loop {
+ let Some(entry) = drv_graph.get(¤t) else {
+ break;
+ };
+ if entry.span.is_some() {
+ chain.push(current);
+ break;
+ }
+ chain.push(current.clone());
+ match &entry.parent {
+ Some(p) => current = p.clone(),
+ None => break,
+ }
+ }
+
+ if chain.is_empty() {
+ return None;
+ }
+
+ for i in (0..chain.len()).rev() {
+ let path = &chain[i];
+ if drv_graph.get(path).unwrap().span.is_some() {
+ continue;
+ }
+ let parent_span = chain
+ .get(i + 1)
+ .and_then(|p| drv_graph.get(p))
+ .and_then(|e| e.span.clone());
+ let name = drv_graph.get(path).unwrap().name.clone();
+ let span = {
+ let _enter = parent_span.as_ref().map(|s| s.enter());
+ info_span!(target: "nix::build", "building", drv = %name)
+ };
+ drv_graph.get_mut(path).unwrap().span = Some(span);
+ }
+
+ drv_graph.get(drv_path).and_then(|e| e.span.clone())
+}
+
#[derive(Debug)]
enum FieldValue {
Int(i32),
@@ -306,57 +435,33 @@
self.fields.push(FieldValue::Str(v.to_string()));
}
fn emit(&mut self, parent: u64, s: &str) {
- let mut mapping = NIX_SPAN_MAPPING.lock().expect("not poisoned");
-
- let parent = mapping.get(&parent);
+ let graph_span = if matches!(self.typ, ActivityType::Build) {
+ self.fields.first().and_then(|f| match f {
+ FieldValue::Str(drv_path) => {
+ let clean = parse_path(drv_path);
+ let span = ensure_drv_span(clean);
+ if span.is_some() {
+ ACTIVITY_TO_DRV
+ .lock()
+ .expect("not poisoned")
+ .insert(self.activity_id, clean.to_owned());
+ }
+ span
+ }
+ _ => None,
+ })
+ } else {
+ None
+ };
- // let meta = spans.alloc_metadata(
- // self.typ.name(),
- // self.verbosity.into(),
- // MetadataKind::Span,
- // "nix activity start",
- // None,
- // None,
- // None,
- // self.typ.fields(),
- // );
- //
- // let mut fields = meta.fields().iter();
- // let span = if let Some(parent) = parent {
- // let s = Span::new(
- // meta,
- // &match meta.fields().len() {
- // 1 => meta.fields().value_set(
- // &<[_; 1]>::try_from([(
- // &fields.next().expect("has field"),
- // Some(&format_args!("Test") as &dyn tracing::Value),
- // )])
- // .expect("valid size"),
- // ),
- // _ => unreachable!(),
- // },
- // );
- // s.follows_from(parent);
- // s
- // } else {
- // Span::new_root(
- // meta,
- // &match meta.fields().len() {
- // 1 => meta.fields().value_set(
- // &<[_; 1]>::try_from([(
- // &fields.next().expect("has field"),
- // Some(&format_args!("Test") as &dyn tracing::Value),
- // )])
- // .expect("valid size"),
- // ),
- // _ => unreachable!(),
- // },
- // )
- // };
- //
- // let id = span.id().expect("id created");
+ let mut mapping = NIX_SPAN_MAPPING.lock().expect("not poisoned");
- let span = {
+ let span = if let Some(span) = graph_span {
+ #[cfg(feature = "indicatif")]
+ span.pb_start();
+ span
+ } else {
+ let parent = mapping.get(&parent);
let _in_parent = parent.map(|p| p.enter());
let level: Level = self.verbosity.into();
if level == Level::ERROR {
@@ -380,7 +485,7 @@
let s = ansi_filter(s);
#[cfg(feature = "indicatif")]
{
- span.pb_set_message(s);
+ span.pb_set_message(&s);
}
let _e = span.enter();
let level: Level = self.verbosity.into();
@@ -454,8 +559,15 @@
warn!(target: "nix::eval", "{v}")
}
fn emit_stop(v: u64) {
- let mut mapping = NIX_SPAN_MAPPING.lock().expect("not poisoned");
- mapping.remove(&v);
+ {
+ let mut mapping = NIX_SPAN_MAPPING.lock().expect("not poisoned");
+ mapping.remove(&v);
+ }
+ if let Some(drv_path) = ACTIVITY_TO_DRV.lock().expect("not poisoned").remove(&v) {
+ if let Some(entry) = DRV_GRAPH.lock().expect("not poisoned").get_mut(&drv_path) {
+ entry.span = None;
+ }
+ }
}
fn emit_log(lvl: u32, v: &[u8]) {
let verbosity = Verbosity::from_int(lvl);