tvix_glue/known_paths.rs
1//! This module implements logic required for persisting known paths
2//! during an evaluation.
3//!
4//! Tvix needs to be able to keep track of each Nix store path that it
5//! knows about during the scope of a single evaluation and its
6//! related builds.
7//!
8//! This data is required to find the derivation needed to actually trigger the
9//! build, if necessary.
10
11use nix_compat::{derivation::Derivation, store_path::StorePath};
12use std::collections::HashMap;
13
14// use crate::fetchers::Fetch;
15
16/// Struct keeping track of all known Derivations in the current evaluation.
17/// This keeps both the Derivation struct, as well as the "Hash derivation
18/// modulo".
19#[derive(Debug, Default)]
20pub struct KnownPaths {
21 /// All known derivation or FOD hashes.
22 ///
23 /// Keys are derivation paths, values are a tuple of the "hash derivation
24 /// modulo" and the Derivation struct itself.
25 derivations: HashMap<StorePath<String>, ([u8; 32], Derivation)>,
26
27 /// A map from output path to (one) drv path.
28 /// Note that in the case of FODs, multiple drvs can produce the same output
29 /// path. We use one of them.
30 outputs_to_drvpath: HashMap<StorePath<String>, StorePath<String>>,
31 /*
32 /// A map from output path to fetches (and their names).
33 outputs_to_fetches: HashMap<StorePath<String>, (String, Fetch)>,
34 */
35}
36
37impl KnownPaths {
38 /// Fetch the opaque "hash derivation modulo" for a given derivation path.
39 pub fn get_hash_derivation_modulo(&self, drv_path: &StorePath<String>) -> Option<&[u8; 32]> {
40 self.derivations
41 .get(drv_path)
42 .map(|(hash_derivation_modulo, _derivation)| hash_derivation_modulo)
43 }
44
45 /// Return a reference to the Derivation for a given drv path.
46 pub fn get_drv_by_drvpath(&self, drv_path: &StorePath<String>) -> Option<&Derivation> {
47 self.derivations
48 .get(drv_path)
49 .map(|(_hash_derivation_modulo, derivation)| derivation)
50 }
51
52 /// Return the drv path of the derivation producing the passed output path.
53 /// Note there can be multiple Derivations producing the same output path in
54 /// flight; this function will only return one of them.
55 pub fn get_drv_path_for_output_path(
56 &self,
57 output_path: &StorePath<String>,
58 ) -> Option<&StorePath<String>> {
59 self.outputs_to_drvpath.get(output_path)
60 }
61
62 /// Insert a new [Derivation] into this struct.
63 /// The Derivation struct must pass validation, and its output paths need to
64 /// be fully calculated.
65 /// All input derivations this refers to must also be inserted to this
66 /// struct.
67 pub fn add_derivation(&mut self, drv_path: StorePath<String>, drv: Derivation) {
68 // check input derivations to have been inserted.
69 #[cfg(debug_assertions)]
70 {
71 for input_drv_path in drv.input_derivations.keys() {
72 debug_assert!(self.derivations.contains_key(input_drv_path));
73 }
74 }
75
76 // compute the hash derivation modulo
77 let hash_derivation_modulo = drv.hash_derivation_modulo(|drv_path| {
78 self.get_hash_derivation_modulo(&drv_path.to_owned())
79 .unwrap_or_else(|| panic!("{drv_path} not found"))
80 .to_owned()
81 });
82
83 // For all output paths, update our lookup table.
84 // We only write into the lookup table once.
85 for output in drv.outputs.values() {
86 self.outputs_to_drvpath
87 .entry(output.path.as_ref().expect("missing store path").clone())
88 .or_insert(drv_path.to_owned());
89 }
90
91 // insert the derivation itself
92 #[allow(unused_variables)] // assertions on this only compiled in debug builds
93 let old = self
94 .derivations
95 .insert(drv_path.to_owned(), (hash_derivation_modulo, drv));
96
97 #[cfg(debug_assertions)]
98 {
99 if let Some(old) = old {
100 debug_assert!(
101 old.0 == hash_derivation_modulo,
102 "hash derivation modulo for a given derivation should always be calculated the same"
103 );
104 }
105 }
106 }
107
108 /*
109 /// Insert a new [Fetch] into this struct, which *must* have an expected
110 /// hash (otherwise we wouldn't be able to calculate the store path).
111 /// Fetches without a known hash need to be fetched inside builtins.
112 pub fn add_fetch<'a>(
113 &mut self,
114 fetch: Fetch,
115 name: &'a str,
116 ) -> Result<StorePathRef<'a>, BuildStorePathError> {
117 let store_path = fetch
118 .store_path(name)?
119 .expect("Tvix bug: fetch must have an expected hash");
120 // insert the fetch.
121 self.outputs_to_fetches
122 .insert(store_path.to_owned(), (name.to_owned(), fetch));
123
124 Ok(store_path)
125 }
126
127 /// Return the name and fetch producing the passed output path.
128 /// Note there can also be (multiple) Derivations producing the same output path.
129 pub fn get_fetch_for_output_path(
130 &self,
131 output_path: &StorePath<String>,
132 ) -> Option<(String, Fetch)> {
133 self.outputs_to_fetches
134 .get(output_path)
135 .map(|(name, fetch)| (name.to_owned(), fetch.to_owned()))
136 }
137 */
138
139 /// Returns an iterator over all known derivations and their store path.
140 pub fn get_derivations(&self) -> impl Iterator<Item = (&StorePath<String>, &Derivation)> {
141 self.derivations.iter().map(|(k, v)| (k, &v.1))
142 }
143}
144
145#[cfg(test)]
146mod tests {
147 use std::sync::LazyLock;
148
149 use super::KnownPaths;
150 use hex_literal::hex;
151 use nix_compat::{derivation::Derivation, store_path::StorePath};
152
153 // use url::Url;
154 // use crate::fetchers::Fetch;
155
156 static BAR_DRV: LazyLock<Derivation> = LazyLock::new(|| {
157 Derivation::from_aterm_bytes(include_bytes!(
158 "tests/ss2p4wmxijn652haqyd7dckxwl4c7hxx-bar.drv"
159 ))
160 .expect("must parse")
161 });
162
163 static FOO_DRV: LazyLock<Derivation> = LazyLock::new(|| {
164 Derivation::from_aterm_bytes(include_bytes!(
165 "tests/ch49594n9avinrf8ip0aslidkc4lxkqv-foo.drv"
166 ))
167 .expect("must parse")
168 });
169
170 static BAR_DRV_PATH: LazyLock<StorePath<String>> = LazyLock::new(|| {
171 StorePath::from_bytes(b"ss2p4wmxijn652haqyd7dckxwl4c7hxx-bar.drv").expect("must parse")
172 });
173
174 static FOO_DRV_PATH: LazyLock<StorePath<String>> = LazyLock::new(|| {
175 StorePath::from_bytes(b"ch49594n9avinrf8ip0aslidkc4lxkqv-foo.drv").expect("must parse")
176 });
177
178 static BAR_OUT_PATH: LazyLock<StorePath<String>> = LazyLock::new(|| {
179 StorePath::from_bytes(b"mp57d33657rf34lzvlbpfa1gjfv5gmpg-bar").expect("must parse")
180 });
181
182 static FOO_OUT_PATH: LazyLock<StorePath<String>> = LazyLock::new(|| {
183 StorePath::from_bytes(b"fhaj6gmwns62s6ypkcldbaj2ybvkhx3p-foo").expect("must parse")
184 });
185
186 /*
187 static FETCH_URL: LazyLock<Fetch> = LazyLock::new(|| {
188 Fetch::URL {
189 url: Url::parse("https://raw.githubusercontent.com/aaptel/notmuch-extract-patch/f732a53e12a7c91a06755ebfab2007adc9b3063b/notmuch-extract-patch").unwrap(),
190 exp_hash: Some(nixhash::from_sri_str("sha256-Xa1Jbl2Eq5+L0ww+Ph1osA3Z/Dxe/RkN1/dITQCdXFk=").unwrap())
191 }
192 });
193
194 static FETCH_URL_OUT_PATH: LazyLock<StorePath<String>> = LazyLock::new(|| {
195 StorePath::from_bytes(b"06qi00hylriyfm0nl827crgjvbax84mz-notmuch-extract-patch").unwrap()
196 });
197
198 static FETCH_TARBALL: LazyLock<Fetch> = LazyLock::new(|| {
199 Fetch::Tarball {
200 url: Url::parse("https://github.com/NixOS/nixpkgs/archive/91050ea1e57e50388fa87a3302ba12d188ef723a.tar.gz").unwrap(),
201 exp_nar_sha256: Some(nixbase32::decode_fixed("1hf6cgaci1n186kkkjq106ryf8mmlq9vnwgfwh625wa8hfgdn4dm").unwrap())
202 }
203 });
204
205 static FETCH_TARBALL_OUT_PATH: LazyLock<StorePath<String>> = LazyLock::new(|| {
206 StorePath::from_bytes(b"7adgvk5zdfq4pwrhsm3n9lzypb12gw0g-source").unwrap()
207 });
208 */
209
210 /// Ensure that we don't allow adding a derivation that depends on another,
211 /// not-yet-added derivation.
212 #[test]
213 #[should_panic]
214 fn drv_reject_if_missing_input_drv() {
215 let mut known_paths = KnownPaths::default();
216
217 // FOO_DRV depends on BAR_DRV, which wasn't added.
218 known_paths.add_derivation(FOO_DRV_PATH.clone(), FOO_DRV.clone());
219 }
220
221 #[test]
222 fn drv_happy_path() {
223 let mut known_paths = KnownPaths::default();
224
225 // get_drv_by_drvpath should return None for non-existing Derivations,
226 // same as get_hash_derivation_modulo and get_drv_path_for_output_path
227 assert_eq!(None, known_paths.get_drv_by_drvpath(&BAR_DRV_PATH));
228 assert_eq!(None, known_paths.get_hash_derivation_modulo(&BAR_DRV_PATH));
229 assert_eq!(
230 None,
231 known_paths.get_drv_path_for_output_path(&BAR_OUT_PATH)
232 );
233
234 // Add BAR_DRV
235 known_paths.add_derivation(BAR_DRV_PATH.clone(), BAR_DRV.clone());
236
237 // We should get it back
238 assert_eq!(
239 Some(&BAR_DRV.clone()),
240 known_paths.get_drv_by_drvpath(&BAR_DRV_PATH)
241 );
242
243 // Test get_drv_path_for_output_path
244 assert_eq!(
245 Some(&BAR_DRV_PATH.clone()),
246 known_paths.get_drv_path_for_output_path(&BAR_OUT_PATH)
247 );
248
249 // It should be possible to get the hash derivation modulo.
250 assert_eq!(
251 Some(&hex!(
252 "c79aebd0ce3269393d4a1fde2cbd1d975d879b40f0bf40a48f550edc107fd5df"
253 )),
254 known_paths.get_hash_derivation_modulo(&BAR_DRV_PATH.clone())
255 );
256
257 // Now insert FOO_DRV too. It shouldn't panic, as BAR_DRV is already
258 // added.
259 known_paths.add_derivation(FOO_DRV_PATH.clone(), FOO_DRV.clone());
260
261 assert_eq!(
262 Some(&FOO_DRV.clone()),
263 known_paths.get_drv_by_drvpath(&FOO_DRV_PATH)
264 );
265 assert_eq!(
266 Some(&hex!(
267 "af030d36d63d3d7f56a71adaba26b36f5fa1f9847da5eed953ed62e18192762f"
268 )),
269 known_paths.get_hash_derivation_modulo(&FOO_DRV_PATH.clone())
270 );
271
272 // Test get_drv_path_for_output_path
273 assert_eq!(
274 Some(&FOO_DRV_PATH.clone()),
275 known_paths.get_drv_path_for_output_path(&FOO_OUT_PATH)
276 );
277 }
278
279 /*
280 #[test]
281 fn fetch_happy_path() {
282 let mut known_paths = KnownPaths::default();
283
284 // get_fetch_for_output_path should return None for new fetches.
285 assert!(known_paths
286 .get_fetch_for_output_path(&FETCH_TARBALL_OUT_PATH)
287 .is_none());
288
289 // add_fetch should return the properly calculated store paths.
290 assert_eq!(
291 *FETCH_TARBALL_OUT_PATH,
292 known_paths
293 .add_fetch(FETCH_TARBALL.clone(), "source")
294 .unwrap()
295 .to_owned()
296 );
297
298 assert_eq!(
299 *FETCH_URL_OUT_PATH,
300 known_paths
301 .add_fetch(FETCH_URL.clone(), "notmuch-extract-patch")
302 .unwrap()
303 .to_owned()
304 );
305 }
306 */
307
308 #[test]
309 fn get_derivations_working() {
310 let mut known_paths = KnownPaths::default();
311
312 // Add BAR_DRV
313 known_paths.add_derivation(BAR_DRV_PATH.clone(), BAR_DRV.clone());
314
315 // We should be able to find BAR_DRV_PATH and BAR_DRV as a pair in get_derivations.
316 assert_eq!(
317 Some((&BAR_DRV_PATH.clone(), &BAR_DRV.clone())),
318 known_paths
319 .get_derivations()
320 .find(|(s, d)| (*s, *d) == (&BAR_DRV_PATH, &BAR_DRV))
321 );
322 }
323
324 // TODO: add test panicking about missing digest
325}