1use nix_compat::{
12 derivation::Derivation,
13 store_path::{BuildStorePathError, StorePath, StorePathRef},
14};
15use std::collections::HashMap;
16
17use crate::fetchers::Fetch;
18
19#[derive(Debug, Default)]
23pub struct KnownPaths {
24 derivations: HashMap<StorePath<String>, ([u8; 32], Derivation)>,
29
30 outputs_to_drvpath: HashMap<StorePath<String>, StorePath<String>>,
34
35 outputs_to_fetches: HashMap<StorePath<String>, (String, Fetch)>,
37}
38
39impl KnownPaths {
40 pub fn get_hash_derivation_modulo(&self, drv_path: &StorePath<String>) -> Option<&[u8; 32]> {
42 self.derivations
43 .get(drv_path)
44 .map(|(hash_derivation_modulo, _derivation)| hash_derivation_modulo)
45 }
46
47 pub fn get_drv_by_drvpath(&self, drv_path: &StorePath<String>) -> Option<&Derivation> {
49 self.derivations
50 .get(drv_path)
51 .map(|(_hash_derivation_modulo, derivation)| derivation)
52 }
53
54 pub fn get_drv_path_for_output_path(
58 &self,
59 output_path: &StorePath<String>,
60 ) -> Option<&StorePath<String>> {
61 self.outputs_to_drvpath.get(output_path)
62 }
63
64 pub fn add_derivation(&mut self, drv_path: StorePath<String>, drv: Derivation) {
70 #[cfg(debug_assertions)]
72 {
73 for input_drv_path in drv.input_derivations.keys() {
74 debug_assert!(self.derivations.contains_key(input_drv_path));
75 }
76 }
77
78 let hash_derivation_modulo = drv.hash_derivation_modulo(|drv_path| {
80 self.get_hash_derivation_modulo(&drv_path.to_owned())
81 .unwrap_or_else(|| panic!("{} not found", drv_path))
82 .to_owned()
83 });
84
85 for output in drv.outputs.values() {
88 self.outputs_to_drvpath
89 .entry(output.path.as_ref().expect("missing store path").clone())
90 .or_insert(drv_path.to_owned());
91 }
92
93 #[allow(unused_variables)] let old = self
96 .derivations
97 .insert(drv_path.to_owned(), (hash_derivation_modulo, drv));
98
99 #[cfg(debug_assertions)]
100 {
101 if let Some(old) = old {
102 debug_assert!(
103 old.0 == hash_derivation_modulo,
104 "hash derivation modulo for a given derivation should always be calculated the same"
105 );
106 }
107 }
108 }
109
110 pub fn add_fetch<'a>(
114 &mut self,
115 fetch: Fetch,
116 name: &'a str,
117 ) -> Result<StorePathRef<'a>, BuildStorePathError> {
118 let store_path = fetch
119 .store_path(name)?
120 .expect("Snix bug: fetch must have an expected hash");
121 self.outputs_to_fetches
123 .insert(store_path.to_owned(), (name.to_owned(), fetch));
124
125 Ok(store_path)
126 }
127
128 pub fn get_fetch_for_output_path(
131 &self,
132 output_path: &StorePath<String>,
133 ) -> Option<(String, Fetch)> {
134 self.outputs_to_fetches
135 .get(output_path)
136 .map(|(name, fetch)| (name.to_owned(), fetch.to_owned()))
137 }
138
139 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 hex_literal::hex;
150 use nix_compat::{derivation::Derivation, nixbase32, nixhash::NixHash, store_path::StorePath};
151 use url::Url;
152
153 use super::KnownPaths;
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 static FETCH_URL: LazyLock<Fetch> = LazyLock::new(|| {
187 Fetch::URL {
188 url: Url::parse("https://raw.githubusercontent.com/aaptel/notmuch-extract-patch/f732a53e12a7c91a06755ebfab2007adc9b3063b/notmuch-extract-patch").unwrap(),
189 exp_hash: Some(NixHash::from_sri("sha256-Xa1Jbl2Eq5+L0ww+Ph1osA3Z/Dxe/RkN1/dITQCdXFk=").unwrap())
190 }
191 });
192
193 static FETCH_URL_OUT_PATH: LazyLock<StorePath<String>> = LazyLock::new(|| {
194 StorePath::from_bytes(b"06qi00hylriyfm0nl827crgjvbax84mz-notmuch-extract-patch").unwrap()
195 });
196
197 static FETCH_TARBALL: LazyLock<Fetch> = LazyLock::new(|| {
198 Fetch::Tarball {
199 url: Url::parse("https://github.com/NixOS/nixpkgs/archive/91050ea1e57e50388fa87a3302ba12d188ef723a.tar.gz").unwrap(),
200 exp_nar_sha256: Some(nixbase32::decode_fixed("1hf6cgaci1n186kkkjq106ryf8mmlq9vnwgfwh625wa8hfgdn4dm").unwrap())
201 }
202 });
203
204 static FETCH_TARBALL_OUT_PATH: LazyLock<StorePath<String>> = LazyLock::new(|| {
205 StorePath::from_bytes(b"7adgvk5zdfq4pwrhsm3n9lzypb12gw0g-source").unwrap()
206 });
207
208 #[test]
211 #[should_panic]
212 fn drv_reject_if_missing_input_drv() {
213 let mut known_paths = KnownPaths::default();
214
215 known_paths.add_derivation(FOO_DRV_PATH.clone(), FOO_DRV.clone());
217 }
218
219 #[test]
220 fn drv_happy_path() {
221 let mut known_paths = KnownPaths::default();
222
223 assert_eq!(None, known_paths.get_drv_by_drvpath(&BAR_DRV_PATH));
226 assert_eq!(None, known_paths.get_hash_derivation_modulo(&BAR_DRV_PATH));
227 assert_eq!(
228 None,
229 known_paths.get_drv_path_for_output_path(&BAR_OUT_PATH)
230 );
231
232 known_paths.add_derivation(BAR_DRV_PATH.clone(), BAR_DRV.clone());
234
235 assert_eq!(
237 Some(&BAR_DRV.clone()),
238 known_paths.get_drv_by_drvpath(&BAR_DRV_PATH)
239 );
240
241 assert_eq!(
243 Some(&BAR_DRV_PATH.clone()),
244 known_paths.get_drv_path_for_output_path(&BAR_OUT_PATH)
245 );
246
247 assert_eq!(
249 Some(&hex!(
250 "c79aebd0ce3269393d4a1fde2cbd1d975d879b40f0bf40a48f550edc107fd5df"
251 )),
252 known_paths.get_hash_derivation_modulo(&BAR_DRV_PATH.clone())
253 );
254
255 known_paths.add_derivation(FOO_DRV_PATH.clone(), FOO_DRV.clone());
258
259 assert_eq!(
260 Some(&FOO_DRV.clone()),
261 known_paths.get_drv_by_drvpath(&FOO_DRV_PATH)
262 );
263 assert_eq!(
264 Some(&hex!(
265 "af030d36d63d3d7f56a71adaba26b36f5fa1f9847da5eed953ed62e18192762f"
266 )),
267 known_paths.get_hash_derivation_modulo(&FOO_DRV_PATH.clone())
268 );
269
270 assert_eq!(
272 Some(&FOO_DRV_PATH.clone()),
273 known_paths.get_drv_path_for_output_path(&FOO_OUT_PATH)
274 );
275 }
276
277 #[test]
278 fn fetch_happy_path() {
279 let mut known_paths = KnownPaths::default();
280
281 assert!(
283 known_paths
284 .get_fetch_for_output_path(&FETCH_TARBALL_OUT_PATH)
285 .is_none()
286 );
287
288 assert_eq!(
290 *FETCH_TARBALL_OUT_PATH,
291 known_paths
292 .add_fetch(FETCH_TARBALL.clone(), "source")
293 .unwrap()
294 .to_owned()
295 );
296
297 assert_eq!(
298 *FETCH_URL_OUT_PATH,
299 known_paths
300 .add_fetch(FETCH_URL.clone(), "notmuch-extract-patch")
301 .unwrap()
302 .to_owned()
303 );
304 }
305
306 #[test]
307 fn get_derivations_working() {
308 let mut known_paths = KnownPaths::default();
309
310 known_paths.add_derivation(BAR_DRV_PATH.clone(), BAR_DRV.clone());
312
313 assert_eq!(
315 Some((&BAR_DRV_PATH.clone(), &BAR_DRV.clone())),
316 known_paths
317 .get_derivations()
318 .find(|(s, d)| (*s, *d) == (&BAR_DRV_PATH, &BAR_DRV))
319 );
320 }
321
322 }