snix_store/path_info.rs
1use nix_compat::{
2 narinfo::{Flags, Signature},
3 nixhash::CAHash,
4 store_path::StorePath,
5};
6
7/// Holds metadata about a store path, but not its contents.
8///
9/// This is somewhat equivalent to the information Nix holds in its SQLite
10/// database, or publishes as .narinfo files, except we also embed the
11/// [snix_castore::Node] describing the contents in the castore model.
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct PathInfo {
14 /// The store path this is about.
15 pub store_path: StorePath<String>,
16 /// The contents in the snix-castore model.
17 //// Can be a directory, file or symlink.
18 pub node: snix_castore::Node,
19 /// A list of references.
20 pub references: Vec<StorePath<String>>,
21 /// The size of the NAR representation of the contents, in bytes.
22 pub nar_size: u64,
23 /// The sha256 digest of the NAR representation of the contents.
24 pub nar_sha256: [u8; 32],
25 /// The signatures, usually shown in a .narinfo file.
26 pub signatures: Vec<Signature<String>>,
27 /// The StorePath of the .drv file producing this output.
28 /// The .drv suffix is omitted in its `name` field.
29 pub deriver: Option<StorePath<String>>,
30 /// The CA field in the .narinfo.
31 /// Its textual representations seen in the wild are one of the following:
32 ///
33 /// * `fixed:r:sha256:1gcky5hlf5vqfzpyhihydmm54grhc94mcs8w7xr8613qsqb1v2j6`
34 /// fixed-output derivations using "recursive" `outputHashMode`.
35 /// * `fixed:sha256:19xqkh72crbcba7flwxyi3n293vav6d7qkzkh2v4zfyi4iia8vj8 fixed-output derivations using "flat" `outputHashMode\`
36 /// * `text:sha256:19xqkh72crbcba7flwxyi3n293vav6d7qkzkh2v4zfyi4iia8vj8`
37 /// Text hashing, used for uploaded .drv files and outputs produced by
38 /// builtins.toFile.
39 ///
40 /// Semantically, they can be split into the following components:
41 ///
42 /// * "content address prefix". Currently, "fixed" and "text" are supported.
43 /// * "hash mode". Currently, "flat" and "recursive" are supported.
44 /// * "hash type". The underlying hash function used.
45 /// Currently, sha1, md5, sha256, sha512.
46 /// * "digest". The digest itself.
47 ///
48 /// There are some restrictions on the possible combinations.
49 /// For example, `text` and `fixed:recursive` always imply sha256.
50 pub ca: Option<CAHash>,
51}
52
53impl PathInfo {
54 /// Reconstructs a [nix_compat::narinfo::NarInfo<'_>].
55 ///
56 /// It does very little allocation (a Vec each for `signatures` and
57 /// `references`), the rest points to data owned elsewhere.
58 ///
59 /// It can be used to validate Signatures, or render a .narinfo file
60 /// (after some more fields are populated)
61 ///
62 /// Keep in mind this is not able to reconstruct all data present in the
63 /// NarInfo<'_>, as some of it is not stored at all:
64 /// - the `system`, `file_hash` and `file_size` fields are set to `None`.
65 /// - the URL is set to an empty string.
66 /// - Compression is set to "none"
67 ///
68 /// If you want to render it out to a string and be able to parse it back
69 /// in, at least URL *must* be set again.
70 pub fn to_narinfo(&self) -> nix_compat::narinfo::NarInfo<'_> {
71 nix_compat::narinfo::NarInfo {
72 flags: Flags::empty(),
73 store_path: self.store_path.as_ref(),
74 nar_hash: self.nar_sha256,
75 nar_size: self.nar_size,
76 references: self.references.iter().map(StorePath::as_ref).collect(),
77 signatures: self.signatures.iter().map(Signature::as_ref).collect(),
78 ca: self.ca.clone(),
79 system: None,
80 deriver: self.deriver.as_ref().map(StorePath::as_ref),
81 url: "",
82 compression: Some("none"),
83 file_hash: None,
84 file_size: None,
85 }
86 }
87}