evix 0.3.2

Evaluate a Nix expression and stream derivation info as JSON lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use std::collections::BTreeMap;

use anyhow::{Context as _, Result};
use nix_bindings::{EvalState, Store, StorePath, Value, ValueType};
use tracing::{debug, warn};

use crate::{Config, EvalError, Event};

/// Evaluate a single attribute path against the Nix expression root.
///
/// Navigates `root` along `path` (auto-calling functions at each step), then
/// inspects the resulting value: if it is a derivation, the function reads
/// name, system, outputs, and, depending on [`Config`], meta, input
/// derivations, and constituents. If it is an attrset, child names are
/// collected for further traversal. If it is neither, an empty attrset is
/// emitted.
pub fn process_attr<'s>(
  state: &'s EvalState,
  store: &Store,
  root: &Value<'s>,
  path: &[String],
  auto_args: Option<&Value<'s>>,
  config: &Config,
) -> Event {
  let attr = path.join(".");

  let value = match navigate(state, root, path, auto_args) {
    Ok(v) => v,
    Err(e) => {
      return Event::Error(EvalError {
        attr,
        attr_path: path.to_vec(),
        error: e.to_string(),
        fatal: false,
      });
    },
  };

  if value.value_type() != ValueType::Attrs {
    return Event::AttrSet {
      attr,
      attr_path: path.to_vec(),
      attrs: vec![],
    };
  }

  match state.get_derivation(&value) {
    Ok(Some(drv_path)) => {
      match make_job(store, &value, path, drv_path, config) {
        Ok(ev) => ev,
        Err(e) => {
          Event::Error(EvalError {
            attr,
            attr_path: path.to_vec(),
            error: e.to_string(),
            fatal: false,
          })
        },
      }
    },
    Ok(None) => {
      let children = collect_recurse(&value, path, config.force_recurse);
      Event::AttrSet {
        attr,
        attr_path: path.to_vec(),
        attrs: children,
      }
    },
    Err(e) => {
      Event::Error(EvalError {
        attr,
        attr_path: path.to_vec(),
        error: e.to_string(),
        fatal: false,
      })
    },
  }
}

fn navigate<'s>(
  state: &'s EvalState,
  root: &Value<'_>,
  path: &[String],
  auto_args: Option<&Value<'s>>,
) -> Result<Value<'s>> {
  if path.is_empty() {
    return Ok(state.auto_call_function(auto_args, root)?);
  }
  let mut current: Value<'s> = {
    let raw = root.get_attr(&path[0])?;
    state.auto_call_function(auto_args, &raw)?
  };
  for key in &path[1..] {
    let next = {
      let raw = current.get_attr(key)?;
      state.auto_call_function(auto_args, &raw)?
    };
    current = next;
  }
  Ok(current)
}

fn collect_recurse(
  value: &Value<'_>,
  path: &[String],
  force_recurse: bool,
) -> Vec<String> {
  let Ok(keys) = value.attr_keys() else {
    return vec![];
  };

  let recurse = force_recurse
    || path.is_empty()
    || value
      .get_attr("recurseForDerivations")
      .and_then(|v| v.as_bool())
      .unwrap_or(false);

  if recurse {
    keys
      .into_iter()
      .filter(|k| k != "recurseForDerivations")
      .collect()
  } else {
    vec![]
  }
}

fn make_job(
  store: &Store,
  value: &Value<'_>,
  path: &[String],
  drv_path: nix_bindings::StorePath,
  config: &Config,
) -> Result<Event> {
  let attr = path.join(".");
  let drv_path_str =
    store.print_path(&drv_path).context("printing drv path")?;

  let name = value
    .get_attr("name")
    .and_then(|v| v.as_string())
    .context("reading .name")?;
  let system = value
    .get_attr("system")
    .and_then(|v| v.as_string())
    .unwrap_or_default();
  let outputs = output_paths(value);

  let meta = if config.meta { read_meta(value) } else { None };
  let constituents = read_constituents(value);
  let input_drvs = if config.show_input_drvs {
    read_input_drvs(store, &drv_path)
  } else {
    BTreeMap::new()
  };

  let gc_root_error = config.gc_roots_dir.as_ref().and_then(|dir| {
    register_gc_root(dir, &drv_path_str).err().map(|e| {
      warn!(drv_path = %drv_path_str, error = %e, "failed to register gc root");
      e.to_string()
    })
  });

  debug!(name = %name, drv_path = %drv_path_str, "found derivation");

  Ok(Event::Derivation(crate::Derivation {
    attr,
    attr_path: path.to_vec(),
    name,
    system,
    drv_path: drv_path_str,
    outputs,
    meta,
    input_drvs,
    constituents,
    gc_root_error,
  }))
}

/// Convert a derivation's `meta` attribute to freeform JSON.
///
/// `meta` is informational and nixpkgs fields can fail to force (functions,
/// `throw`), so unreadable nested attributes are dropped rather than failing
/// the job. Such omissions are intentional and not logged.
///
/// # Returns
///
/// The `meta` attrset as a JSON object, or `None` if the derivation declares no
/// `meta` attribute.
fn read_meta(value: &Value<'_>) -> Option<serde_json::Value> {
  if !value.has_attr("meta").unwrap_or(false) {
    return None;
  }
  let meta = value.get_attr("meta").ok()?;
  value_to_json(meta, 64)
}

/// Recursively convert a Nix value to JSON, forcing each node on entry.
///
/// # Returns
///
/// The value as JSON, or `None` if the node fails to force or has no JSON
/// analogue (thunks that error, functions, external values).
fn value_to_json(
  mut value: Value<'_>,
  depth_remaining: u32,
) -> Option<serde_json::Value> {
  use serde_json::Value as J;

  if depth_remaining == 0 {
    return None;
  }

  value.force().ok()?;
  match value.value_type() {
    ValueType::Null => Some(J::Null),
    ValueType::Bool => value.as_bool().ok().map(J::Bool),
    ValueType::Int => value.as_int().ok().map(|i| J::Number(i.into())),
    ValueType::Float => {
      value
        .as_float()
        .ok()
        .and_then(serde_json::Number::from_f64)
        .map(J::Number)
    },
    ValueType::String => value.as_string().ok().map(J::String),
    ValueType::Path => {
      value
        .as_path()
        .ok()
        .map(|p| J::String(p.to_string_lossy().into_owned()))
    },
    ValueType::List => {
      let len = value.list_len().ok()?;
      let mut arr = Vec::with_capacity(len);
      for i in 0..len {
        let item = value.list_get(i).ok()?;
        arr.push(value_to_json(item, depth_remaining - 1).unwrap_or(J::Null));
      }
      Some(J::Array(arr))
    },
    ValueType::Attrs => {
      let keys = value.attr_keys().ok()?;
      let mut map = serde_json::Map::new();
      for key in keys {
        if let Ok(child) = value.get_attr(&key)
          && let Some(child_json) = value_to_json(child, depth_remaining - 1)
        {
          map.insert(key, child_json);
        }
      }
      Some(J::Object(map))
    },
    ValueType::Thunk | ValueType::Function | ValueType::External => None,
  }
}

/// Read the `constituents` attribute of an aggregate (Hydra) job.
///
/// # Returns
///
/// The constituent attribute-path strings, or `None` when the derivation does
/// not declare `constituents` (an ordinary, non-aggregate job).
fn read_constituents(value: &Value<'_>) -> Option<Vec<String>> {
  if !value.has_attr("constituents").unwrap_or(false) {
    return None;
  }
  let mut list = value.get_attr("constituents").ok()?;
  list.force().ok()?;
  let len = list.list_len().ok()?;
  let mut out = Vec::with_capacity(len);
  for i in 0..len {
    if let Ok(item) = list.list_get(i)
      && let Ok(s) = item.as_string()
    {
      out.push(s);
    }
  }
  Some(out)
}

/// Read a derivation's input derivations from its `.drv` file.
///
/// Unlike `meta`, missing `inputDrvs` has downstream consequences (consumers
/// use it to discover build dependencies), so each failure is logged at `warn`
/// rather than swallowed silently.
///
/// # Returns
///
/// A map from absolute input `.drv` store path to that input's output-name
/// list. Empty when the derivation has no input derivations, or when it cannot
/// be read, serialized, or parsed (each of those failures is logged).
fn read_input_drvs(
  store: &Store,
  drv_path: &StorePath,
) -> BTreeMap<String, serde_json::Value> {
  let mut map = BTreeMap::new();
  let drv = match store.read_derivation(drv_path) {
    Ok(drv) => drv,
    Err(e) => {
      warn!(error = %e, "failed to read derivation for inputDrvs");
      return map;
    },
  };
  let json = match drv.to_json() {
    Ok(json) => json,
    Err(e) => {
      warn!(error = %e, "failed to serialize derivation for inputDrvs");
      return map;
    },
  };
  let parsed = match serde_json::from_str::<serde_json::Value>(&json) {
    Ok(parsed) => parsed,
    Err(e) => {
      warn!(error = %e, "failed to parse derivation JSON for inputDrvs");
      return map;
    },
  };
  // `nix_derivation_to_json` nests input derivations under `inputs.drvs` and
  // keys them by store-relative basename. Re-add the store prefix so keys are
  // absolute `.drv` paths, and expose the value as the output-name list to
  // match the `nix-eval-jobs` `inputDrvs` contract (`{drv: ["out", ...]}`).
  let store_dir = store
    .store_dir()
    .unwrap_or_else(|_| "/nix/store".to_string());
  // A derivation with no input derivations (e.g. a fixed-output fetch)
  // legitimately has no `inputs.drvs`, so an absent key is normal and not
  // logged.
  let Some(drvs) = parsed
    .get("inputs")
    .and_then(|inputs| inputs.get("drvs"))
    .and_then(serde_json::Value::as_object)
  else {
    return map;
  };
  for (key, value) in drvs {
    let full_path = if key.starts_with('/') {
      key.clone()
    } else {
      format!("{store_dir}/{key}")
    };
    let outputs = value
      .get("outputs")
      .cloned()
      .unwrap_or_else(|| value.clone());
    map.insert(full_path, outputs);
  }
  map
}

/// Collect each output's store path from a derivation value.
///
/// # Returns
///
/// A map from output name to its resolved store path, or `None` when resolution
/// fails for an individual output.
fn output_paths(value: &Value<'_>) -> BTreeMap<String, Option<String>> {
  let mut map = BTreeMap::new();
  let Ok(list) = value.get_attr("outputs") else {
    return map;
  };
  let Ok(len) = list.list_len() else {
    return map;
  };
  for i in 0..len {
    let Ok(name_val) = list.list_get(i) else {
      continue;
    };
    let Ok(name) = name_val.as_string() else {
      continue;
    };
    let path = output_path_for(value, &name);
    map.insert(name, path);
  }
  map
}

/// Resolve the store path of a single named output.
///
/// Each output is exposed on the derivation as an attribute whose `outPath` is
/// the store path; for non-standard derivations the attribute is coerced
/// directly as a string or path.
///
/// # Returns
///
/// The output's store path, or `None` if the output attribute is missing or
/// cannot be coerced to a path.
fn output_path_for(value: &Value<'_>, name: &str) -> Option<String> {
  let out = value.get_attr(name).ok()?;
  if let Ok(path) = out.get_attr("outPath").and_then(|v| v.as_string()) {
    return Some(path);
  }
  if let Ok(s) = out.as_string() {
    return Some(s);
  }
  out.as_path().ok().map(|p| p.to_string_lossy().into_owned())
}

/// Create a symlink under `gc_dir` pointing to `drv_path` so the Nix garbage
/// collector retains the derivation and its outputs.
fn register_gc_root(gc_dir: &std::path::Path, drv_path: &str) -> Result<()> {
  let name = std::path::Path::new(drv_path)
    .file_name()
    .context("drv path has no filename")?;
  let link = gc_dir.join(name);
  if !link.exists() {
    std::os::unix::fs::symlink(drv_path, &link)
      .with_context(|| format!("symlinking {link:?} -> {drv_path}"))?;
  }
  Ok(())
}