diff options
| author | tv <tv@krebsco.de> | 2026-11-20 02:33:17 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-20 02:33:17 +0000 |
| commit | 7e4597a38a29f66fd8bbc7415973caeaa9442bde (patch) | |
| tree | d730cc0db0bb3fdb5db9e977a996dbccc5fedbfc /pkgs/populate/src/populate.rs | |
| parent | 7837a236e728988fcd501917444828b790c367f9 (diff) | |
populate: file sources' record setting, for provenance in the run record
A consumer recording what a build used gets the commit of git sources,
and the store paths of flake, flake-input, and derivation sources, but
nothing for file sources, which often hold the configuration, taken from
work trees on the deploying machine. Recording nothing about them stays
the default, since they may hold secrets, whose hash could give them
away. file sources' record setting, with flags that default to false,
adds to the source's entry in the record:
- commit: if path is in a Git work tree, the commit HEAD names, and
dirty, whether git status lists anything in path. Git runs on the
deploying machine, without hooks, file system monitors, and writing
the index, on every run, which is cheap: a new commit can change HEAD
without changing any file.
- digest: what nix-hash --type sha256 --sri prints for the target path,
after the flakes step, which may have rewritten its lock file. That
takes reading all of it, so populate only computes it if the source
changed since the last record, and else takes it from that record,
which the command taking the lock prints, so a run that changes
nothing reads nothing more.
The record's format stays version 0: the fields are optional additions.
The README documents that the record tells what populate delivered,
which something changing the target directory meanwhile would make it
miss, and how rsync can leave out much of what Git ignores, with
:- .gitignore in a .rsync-filter file, and what that misses.
record-file checks the commit, dirty, and the digest, also taken from
the last record.
Diffstat (limited to 'pkgs/populate/src/populate.rs')
| -rw-r--r-- | pkgs/populate/src/populate.rs | 90 |
1 files changed, 83 insertions, 7 deletions
diff --git a/pkgs/populate/src/populate.rs b/pkgs/populate/src/populate.rs index efcf5c3..0366c53 100644 --- a/pkgs/populate/src/populate.rs +++ b/pkgs/populate/src/populate.rs @@ -25,7 +25,7 @@ pub fn populate(context: &Context, spec: &Spec) -> Result<()> { &[&sentinel, &context.name(&sentinel), dir, force], )?; let started = record::now(); - let previous = context.lock(&sentinel, json!({ "run": started }))?; + let (previous, last) = context.lock(&sentinel, json!({ "run": started }))?; if spec.backup { context.run(&sh::backup(), &[dir])?; } @@ -71,12 +71,54 @@ pub fn populate(context: &Context, spec: &Spec) -> Result<()> { context.note(json!({ "pruned": true })); } } - let record = record::record(spec, &started, &previous, &context.entries()); + let mut record = record::record(spec, &started, &previous, &context.entries()); + digests(context, spec, &mut record, &last.join("\n"))?; context.run(sh::RECORD, &[&sentinel, &record.to_string()])?; context.unlock(); Ok(()) } +/// Adds the digest of each file source whose record setting asks for it to +/// the record: of what's at its target path now, after the flakes step, +/// only if it changed since the last record, last, and else the one the +/// last record has, so a run that changes nothing reads nothing more. +fn digests(context: &Context, spec: &Spec, record: &mut Value, last: &str) -> Result<()> { + let last: Value = serde_json::from_str(last).unwrap_or(Value::Null); + let mut names = Vec::new(); + let mut paths = Vec::new(); + for (name, source) in &spec.source { + if !record_setting(source, "digest") { + continue; + } + let entry = &mut record["sources"][name]; + let digest = &last["sources"][name]["digest"]; + if entry["changed"] == json!(false) && digest.is_string() { + entry["digest"] = digest.clone(); + } else { + names.push(name); + paths.push(format!("{}/{name}", spec.target.path)); + } + } + if !paths.is_empty() { + let args: Vec<&str> = paths.iter().map(String::as_str).collect(); + let output = context.output(sh::DIGESTS, &args)?; + for (name, digest) in names.iter().zip(output.lines()) { + record["sources"][name.as_str()]["digest"] = json!(digest); + } + } + Ok(()) +} + +/// Whether a source's record setting has key set. +fn record_setting(source: &Source, key: &str) -> bool { + source + .settings + .get("record") + .and_then(|r| r.get(key)) + .and_then(Value::as_bool) + == Some(true) +} + /// What delivering a source did: whether that changed anything in the /// target directory, and what got delivered, for the record of the run. #[derive(Default)] @@ -94,8 +136,8 @@ impl Outcome { } /// With what got delivered, under key, as value. - pub fn with(mut self, key: &str, value: &str) -> Outcome { - self.delivered.insert(key.to_string(), json!(value)); + pub fn with(mut self, key: &str, value: impl Into<Value>) -> Outcome { + self.delivered.insert(key.to_string(), value.into()); self } @@ -230,9 +272,43 @@ impl Kind for File { // The lock file the flakes step rewrites, which delivering brings // back, changed if the flakes step says so. let lock = in_place_lock(d.source); - Ok(Outcome::changed( - changed.iter().any(|path| Some(path.as_str()) != lock), - )) + let outcome = Outcome::changed(changed.iter().any(|path| Some(path.as_str()) != lock)); + if !record_setting(d.source, "commit") { + return Ok(outcome); + } + // What the work tree's Git says, without its hooks and file system + // monitors, and without writing the index. + let git = |args: &[&str]| { + Command::new("git") + .args([ + "-c", + "core.hooksPath=/dev/null", + "-c", + "core.fsmonitor=false", + ]) + .args(["--no-optional-locks", "-C", source_path]) + .args(args) + .stdin(Stdio::null()) + .stderr(Stdio::null()) + .output() + .ok() + .filter(|output| output.status.success()) + .map(|output| { + String::from_utf8_lossy(&output.stdout) + .trim_end() + .to_string() + }) + }; + // Nothing, if the path isn't in a Git work tree. + let Some(commit) = git(&["rev-parse", "--verify", "--quiet", "HEAD"]) else { + return Ok(outcome); + }; + let status = git(&["status", "--porcelain", "--", "."]).ok_or_else(|| { + Failure::Message(format!("error: can't tell the Git status of {source_path}")) + })?; + Ok(outcome + .with("commit", commit) + .with("dirty", !status.is_empty())) } } |
