lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

commit 386e2d7e8cca95bdd0f6ce3444b5e741ea61ef8a
parent 8929943c6efde01499921c0e3f879b693252494b
Author: triesap <tyson@radroots.org>
Date:   Fri, 16 Oct 2026 13:43:46 +0000

xtask: Bind current service flake helper inputs

- Admit the exact current Lib helper graph and Crane follow.
- Share bounded input validation across source and artifact consumers.
- Preserve historical deferred input admission and qualification limits.
- Verify exact source drift refusals and all tooling checks.

Diffstat:
Mcontracts/architecture/deviations.toml | 15+++++++++++++++
Mtools/xtask/src/main.rs | 1+
Mtools/xtask/src/service_release_artifacts.rs | 9+++++----
Mtools/xtask/src/service_source_lock.rs | 4++--
Mtools/xtask/src/service_source_lock_command.rs | 17+++++++----------
Atools/xtask/src/service_source_lock_nix.rs | 237+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 267 insertions(+), 16 deletions(-)

diff --git a/contracts/architecture/deviations.toml b/contracts/architecture/deviations.toml @@ -2,6 +2,21 @@ schema_version = 1 architecture_id = "radroots.crates.release.v1" [[deviation]] +id = "RCRV1-DEV-042" +date = "2026-10-16" +status = "closed" +approval = "The user authorizes full current compiler/source-lock integration and verified owning-repository source contributions." +affected_steps = ["030", "130"] +spec_anchors = ["contracts/crates/release_v1/radroots_crates_release_v1.toml#release_policy"] +source_evidence = ["The current service-source-lock and artifact commands reuse a deferred-only Nix validator. Actual services use the Lib flake and its five helper inputs with a root Crane follow, which that validator refuses."] +replacement_action = "Share current bounded Nix input admission between source-lock and artifact consumers. Admit the exact helper graph with immutable referenced inputs and one exact Lib selector, reject duplicate/unknown or drifting Lib identity, and preserve historical deferred admission unchanged. Bound metadata remains distinct from executed Nix qualification." +verification = ["Prove the actual helper input shape and independent source, graph, hash and duplicate refusals with synthetic fixtures.", "Run current locked tooling checks/tests, strict lint, formatting, architecture and contracts; preserve earlier failure records."] +unresolved_risk = "Input validation alone does not qualify service Nix builds, runtime artifacts or release acceptance." +normative_architecture_change = false +adr_required = false +closure_evidence = ["Current flake helper input admission and independent revision, unknown-field, input, immutable hash, missing-reference, source-selector and duplicate refusals pass. Historical deferred admission and its tests retain their behavior.", "Exact Rust 1.99 checking and all 627 serialized tooling tests pass, with the same two original ignored annotations. Strict Clippy, formatting, architecture and contract validation pass. The initial unused-helper lint failure and a refused overlapping formatter check remain recorded separately."] + +[[deviation]] id = "RCRV1-DEV-041" date = "2026-10-16" status = "closed" diff --git a/tools/xtask/src/main.rs b/tools/xtask/src/main.rs @@ -57,6 +57,7 @@ mod service_release_artifacts; mod service_repro_install; mod service_source_lock; mod service_source_lock_command; +mod service_source_lock_nix; mod service_source_lock_parts; #[cfg(test)] mod service_source_lock_v3; diff --git a/tools/xtask/src/service_release_artifacts.rs b/tools/xtask/src/service_release_artifacts.rs @@ -14,7 +14,8 @@ use tar::{Builder as TarBuilder, Header as TarHeader}; use tempfile::TempDir; use crate::safe_artifact_io::{TarGzipLimits, TraversalLimits}; -use crate::service_source_lock::{LIB_REPOSITORY, validate_deferred_nix_material}; +use crate::service_source_lock::LIB_REPOSITORY; +use crate::service_source_lock_nix::validate_bound_nix_material; use crate::service_source_lock_v4::{ LOCK_FILENAME, MAX_LOCK_BYTES, PREDECESSOR_LOCK_FILENAMES, RUST_VERSION, ServiceSourceLockV4, validate_lib_compiler_inputs, validate_toolchain_bytes, @@ -923,15 +924,15 @@ fn validate_source_lock_files( MAX_SERVICE_FLAKE_LOCK_BYTES, ReleaseArtifactError::InvalidSourceLock, )?; - let evidence = validate_deferred_nix_material(&flake_nix, &flake_lock) + let evidence = validate_bound_nix_material(&flake_nix, &flake_lock) .map_err(|_| ReleaseArtifactError::InvalidSourceLock)?; let artifact_contract = hash_regular( &root.join(source_lock.artifact_contract_path()), MAX_TEXT_INPUT_BYTES, ) .map_err(|_| ReleaseArtifactError::InvalidSourceLock)?; - if evidence.lib_revision() == source_lock.revision() - && evidence.flake_lock_sha256() == source_lock.flake_lock_sha256() + if evidence.revision == source_lock.revision() + && evidence.lock_sha256 == source_lock.flake_lock_sha256() && artifact_contract.sha256 == source_lock.artifact_contract_sha256() { Ok(()) diff --git a/tools/xtask/src/service_source_lock.rs b/tools/xtask/src/service_source_lock.rs @@ -384,7 +384,7 @@ pub(crate) fn validate_deferred_nix_lock(bytes: &[u8]) -> Result<String, Service } } -struct NoDuplicateJsonValue(serde_json::Value); +pub(crate) struct NoDuplicateJsonValue(pub(crate) serde_json::Value); impl<'de> Deserialize<'de> for NoDuplicateJsonValue { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> @@ -479,7 +479,7 @@ impl<'de> Visitor<'de> for NoDuplicateJsonValueVisitor { } } -fn valid_nix_sha256(value: &str) -> bool { +pub(crate) fn valid_nix_sha256(value: &str) -> bool { let Some(encoded) = value.strip_prefix("sha256-") else { return false; }; diff --git a/tools/xtask/src/service_source_lock_command.rs b/tools/xtask/src/service_source_lock_command.rs @@ -14,9 +14,8 @@ use walkdir::WalkDir; use crate::build_output::is_build_output_directory; use crate::exact_tree_archive; -use crate::service_source_lock::{ - ContractVersions, LIB_REPOSITORY, validate_deferred_nix_material, -}; +use crate::service_source_lock::{ContractVersions, LIB_REPOSITORY}; +use crate::service_source_lock_nix::{validate_bound_nix_lock, validate_bound_nix_material}; use crate::service_source_lock_v4::{ LOCK_FILENAME, MAX_LOCK_BYTES, PREDECESSOR_LOCK_FILENAMES, ServiceSourceLockV4, ServiceSourceLockV4Parts, validate_lib_compiler_inputs, validate_toolchain_bytes, @@ -619,20 +618,18 @@ fn read_nix_material(root: &Path) -> Result<NixMaterialEvidence, CommandError> { MAX_FLAKE_LOCK_BYTES, CommandError::InvalidFlakeLock, )?; - crate::service_source_lock::validate_deferred_nix_lock(&lock) - .map_err(|_| CommandError::InvalidFlakeLock)?; - let evidence = validate_deferred_nix_material(&expression, &lock) + validate_bound_nix_lock(&lock).map_err(|_| CommandError::InvalidFlakeLock)?; + let evidence = validate_bound_nix_material(&expression, &lock) .map_err(|_| CommandError::InvalidNixMaterial)?; Ok(NixMaterialEvidence { - lib_revision: evidence.lib_revision().to_owned(), - flake_lock_sha256: evidence.flake_lock_sha256().to_owned(), + lib_revision: evidence.revision, + flake_lock_sha256: evidence.lock_sha256, }) } #[cfg(test)] fn validate_flake_lock(bytes: &[u8]) -> Result<String, CommandError> { - crate::service_source_lock::validate_deferred_nix_lock(bytes) - .map_err(|_| CommandError::InvalidFlakeLock) + validate_bound_nix_lock(bytes).map_err(|_| CommandError::InvalidFlakeLock) } fn validate_toolchain(root: &Path) -> Result<(), CommandError> { diff --git a/tools/xtask/src/service_source_lock_nix.rs b/tools/xtask/src/service_source_lock_nix.rs @@ -0,0 +1,237 @@ +use std::collections::BTreeSet; + +use serde::Deserialize; +use serde_json::Value; +use sha2::{Digest as _, Sha256}; + +use crate::service_source_lock::{ + NoDuplicateJsonValue, valid_nix_sha256, validate_deferred_nix_lock, + validate_deferred_nix_material, +}; + +pub(crate) struct BoundNixMaterial { + pub(crate) revision: String, + pub(crate) lock_sha256: String, +} + +pub(crate) fn validate_bound_nix_lock(bytes: &[u8]) -> Result<String, ()> { + parse_lock(bytes).map(|(revision, _)| revision) +} + +pub(crate) fn validate_bound_nix_material( + expression: &[u8], + lock: &[u8], +) -> Result<BoundNixMaterial, ()> { + if expression.len() > 1_048_576 { + return Err(()); + } + let (revision, helpers) = parse_lock(lock)?; + if helpers { + let text = std::str::from_utf8(expression).map_err(|_| ())?; + let lines = text + .lines() + .map(str::trim) + .filter(|line| !line.is_empty() && !line.starts_with('#')) + .collect::<Vec<_>>(); + let expected = format!("url = \"github:radrootslabs/lib/{revision}\";"); + let selectors = lines + .iter() + .filter(|line| line.contains("github:radrootslabs/lib")) + .count(); + let blocks = lines + .windows(4) + .filter(|block| { + block[0] == "lib = {" + && lines.iter().filter(|line| **line == "inputs = {").count() == 1 + && block[1] == expected + && block[2] == "inputs.crane.follows = \"crane\";" + && block[3] == "};" + }) + .count(); + if selectors != 1 || blocks != 1 { + return Err(()); + } + } else { + let evidence = validate_deferred_nix_material(expression, lock).map_err(|_| ())?; + return Ok(BoundNixMaterial { + revision: evidence.lib_revision().to_owned(), + lock_sha256: evidence.flake_lock_sha256().to_owned(), + }); + } + Ok(BoundNixMaterial { + revision, + lock_sha256: hex::encode(Sha256::digest(lock)), + }) +} + +fn parse_lock(bytes: &[u8]) -> Result<(String, bool), ()> { + if bytes.len() > 4_194_304 { + return Err(()); + } + let mut decoder = serde_json::Deserializer::from_slice(bytes); + let mut value = NoDuplicateJsonValue::deserialize(&mut decoder) + .map_err(|_| ())? + .0; + decoder.end().map_err(|_| ())?; + let root = value.get("root").and_then(Value::as_str).ok_or(())?; + let nodes = value.get("nodes").and_then(Value::as_object).ok_or(())?; + let root_inputs = nodes + .get(root) + .and_then(|node| node.get("inputs")) + .and_then(Value::as_object) + .ok_or(())?; + let lib_name = root_inputs + .get("lib") + .and_then(Value::as_str) + .ok_or(())? + .to_owned(); + let lib = nodes.get(&lib_name).and_then(Value::as_object).ok_or(())?; + let helpers = if let Some(inputs) = lib.get("inputs") { + let inputs = inputs.as_object().ok_or(())?; + if inputs.keys().map(String::as_str).collect::<BTreeSet<_>>() + != BTreeSet::from([ + "crane", + "flake-parts", + "nixpkgs", + "rust-overlay", + "treefmt-nix", + ]) + { + return Err(()); + } + for (name, target) in inputs { + let target = if name == "crane" { + if target != &serde_json::json!(["crane"]) { + return Err(()); + } + root_inputs.get("crane").and_then(Value::as_str).ok_or(())? + } else { + target.as_str().ok_or(())? + }; + if target == lib_name || target == root { + return Err(()); + } + let locked = nodes + .get(target) + .and_then(|node| node.get("locked")) + .ok_or(())?; + let revision = locked.get("rev").and_then(Value::as_str).ok_or(())?; + if locked.get("type").and_then(Value::as_str) != Some("github") + || revision.len() != 40 + || !revision + .bytes() + .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)) + || !locked + .get("narHash") + .and_then(Value::as_str) + .is_some_and(valid_nix_sha256) + { + return Err(()); + } + } + true + } else { + false + }; + if helpers { + value["nodes"][&lib_name] + .as_object_mut() + .ok_or(())? + .remove("inputs"); + } + let normalized = serde_json::to_vec(&value).map_err(|_| ())?; + let revision = validate_deferred_nix_lock(&normalized).map_err(|_| ())?; + Ok((revision, helpers)) +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + fn fixture() -> (String, Value) { + let revision = "a".repeat(40); + let mut lock = json!({ + "version": 7, "root": "root", + "nodes": { + "root": {"inputs": {"lib": "lib", "crane": "crane"}}, + "lib": { + "inputs": {"crane": ["crane"], "flake-parts": "parts", "nixpkgs": "pkgs", "rust-overlay": "rust", "treefmt-nix": "fmt"}, + "locked": {"lastModified": 1, "narHash": format!("sha256-{}=", "A".repeat(43)), "owner": "radrootslabs", "repo": "lib", "rev": revision, "type": "github"}, + "original": {"owner": "radrootslabs", "repo": "lib", "rev": revision, "type": "github"} + } + } + }); + for node in ["crane", "parts", "pkgs", "rust", "fmt"] { + lock["nodes"][node] = json!({"locked": {"type": "github", "rev": "b".repeat(40), "narHash": format!("sha256-{}=", "A".repeat(43))}}); + } + let expression = format!( + "{{\n inputs = {{\n crane.url = \"github:owner/crane/{}\";\n lib = {{\n url = \"github:radrootslabs/lib/{revision}\";\n inputs.crane.follows = \"crane\";\n }};\n }};\n outputs = {{...}}: {{}};\n}}", + "b".repeat(40) + ); + (expression, lock) + } + + #[test] + fn actual_service_helper_shape_binds_exact_inputs_without_qualification() { + let (expression, lock) = fixture(); + let bytes = serde_json::to_vec(&lock).unwrap(); + assert!(validate_deferred_nix_material(expression.as_bytes(), &bytes).is_err()); + let evidence = validate_bound_nix_material(expression.as_bytes(), &bytes).unwrap(); + assert_eq!(evidence.revision, "a".repeat(40)); + assert_eq!(evidence.lock_sha256, hex::encode(Sha256::digest(&bytes))); + let direct = expression.replace("lib = {", "inputs.lib = {"); + assert!(validate_bound_nix_material(direct.as_bytes(), &bytes).is_err()); + } + + #[test] + fn helper_graph_identity_and_source_drift_are_refused() { + let (expression, lock) = fixture(); + for (pointer, value) in [ + ("/nodes/lib/original/rev", json!("c".repeat(40))), + ("/nodes/lib/unexpected", json!(true)), + ("/nodes/lib/inputs/extra", json!("pkgs")), + ("/nodes/lib/inputs/crane", json!(["other"])), + ("/nodes/lib/inputs/nixpkgs", json!("missing")), + ("/nodes/lib/inputs/nixpkgs", json!("lib")), + ("/nodes/pkgs/locked/rev", json!("stable")), + ("/nodes/pkgs/locked/narHash", json!("invalid")), + ] { + let mut changed = lock.clone(); + if pointer.ends_with("unexpected") { + changed["nodes"]["lib"]["unexpected"] = value; + } else if pointer.ends_with("extra") { + changed["nodes"]["lib"]["inputs"]["extra"] = value; + } else { + *changed.pointer_mut(pointer).unwrap() = value; + } + assert!( + validate_bound_nix_material( + expression.as_bytes(), + &serde_json::to_vec(&changed).unwrap() + ) + .is_err(), + "{pointer}" + ); + } + for source in [ + expression.replace(&"a".repeat(40), &"c".repeat(40)), + expression.replace("inputs.crane.follows = \"crane\";", "flake = false;"), + format!( + "{expression}\nurl = \"github:radrootslabs/lib/{}\";", + "a".repeat(40) + ), + ] { + assert!( + validate_bound_nix_material(source.as_bytes(), &serde_json::to_vec(&lock).unwrap()) + .is_err() + ); + } + let duplicate = serde_json::to_string(&lock).unwrap().replacen( + "\"version\":7", + "\"version\":7,\"version\":7", + 1, + ); + assert!(validate_bound_nix_lock(duplicate.as_bytes()).is_err()); + } +}