Skip to main content

nix_compat/derivation/
mod.rs

1use crate::store_path::{self, StorePath, StorePathRef};
2use bstr::BString;
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5use sha2::{Digest, Sha256};
6use std::collections::{BTreeMap, BTreeSet};
7use std::io;
8
9mod errors;
10mod output;
11mod output_name;
12pub mod outputs;
13mod parse_error;
14mod parser;
15mod validate;
16mod write;
17
18#[cfg(test)]
19mod tests;
20
21// Public API of the crate.
22pub use crate::nixhash::{CAHash, NixHash};
23pub use errors::DerivationError;
24pub use output::{Output, OutputHash, OutputHashMode};
25pub use output_name::{OutputName, ParseOutputNameError};
26#[doc(inline)]
27pub use outputs::Outputs;
28pub use parser::Error as ParserError;
29
30#[derive(Clone, Debug, Default, Eq, PartialEq)]
31#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
32pub struct Derivation {
33    #[cfg_attr(feature = "serde", serde(rename = "args"))]
34    pub arguments: Vec<String>,
35
36    pub builder: String,
37
38    #[cfg_attr(feature = "serde", serde(rename = "env"))]
39    pub environment: BTreeMap<String, BString>,
40
41    /// Map from drv path to output names used from this derivation.
42    #[cfg_attr(feature = "serde", serde(rename = "inputDrvs"))]
43    pub input_derivations: BTreeMap<StorePath, BTreeSet<OutputName>>,
44
45    /// Plain store paths of additional inputs.
46    #[cfg_attr(feature = "serde", serde(rename = "inputSrcs"))]
47    pub input_sources: BTreeSet<StorePath>,
48
49    /// Maps output names to Output.
50    pub outputs: Outputs,
51
52    pub system: String,
53}
54
55impl Derivation {
56    /// write the Derivation to the given [std::io::Write], in ATerm format.
57    ///
58    /// The only errors returns are these when writing to the passed writer.
59    pub fn serialize(&self, writer: &mut impl std::io::Write) -> Result<(), io::Error> {
60        self.serialize_with_replacements(writer, self.input_derivations.iter())
61    }
62
63    /// return the ATerm serialization.
64    pub fn to_aterm_bytes(&self) -> Vec<u8> {
65        let mut buf = Vec::new();
66        self.serialize(&mut buf).unwrap();
67        buf
68    }
69
70    /// Parse an Derivation in ATerm serialization, and validate it passes our
71    /// set of validations.
72    pub fn from_aterm_bytes(b: &[u8]) -> Result<Derivation, parser::Error<&[u8]>> {
73        parser::parse(b)
74    }
75
76    /// Returns the drv path of a [Derivation] struct.
77    ///
78    /// The drv path is calculated by invoking [store_path::build_text_path], using
79    /// the `name` with a `.drv` suffix as name, all [Derivation::input_sources] and
80    /// keys of [Derivation::input_derivations] as references, and the ATerm string of
81    /// the [Derivation] as content.
82    pub fn calculate_derivation_path(&self, name: &str) -> Result<StorePath, DerivationError> {
83        // collect the list of paths from input_sources AND input_derivations
84        // into a sorted list of references.
85        let mut references: BTreeSet<StorePathRef> = self
86            .input_derivations
87            .keys()
88            .map(StorePath::as_ref)
89            .collect();
90        references.extend(self.input_sources.iter().map(StorePath::as_ref));
91
92        let drv_name = format!("{name}.drv");
93        store_path::build_text_path(
94            // append .drv to the name
95            &drv_name,
96            self.to_aterm_bytes(),
97            references,
98        )
99        .map_err(|err| DerivationError::InvalidDerivationName(drv_name.to_string(), err))
100        .map(|sp| sp.to_owned())
101    }
102
103    /// Returns the FOD digest, if the derivation is fixed-output, or None if
104    /// it's not.
105    // NOTE: this is called twice, once when constructing out_output.path is None,
106    // it'll later get populated with the path.
107    pub fn fod_digest(&self) -> Option<[u8; 32]> {
108        if self.outputs.len() != 1 {
109            return None;
110        }
111
112        let out_output = self.outputs.get(&OutputName::out())?;
113        let out_output_hash = out_output.output_hash.as_ref()?;
114
115        Some(store_path::fod_digest(
116            out_output_hash.mode == OutputHashMode::Recursive,
117            &out_output_hash.hash,
118            out_output.path.as_ref().map(|sp| sp.as_ref()),
119        ))
120    }
121
122    /// Calculates the hash of a derivation modulo fixed-output subderivations.
123    ///
124    /// This is called `hashDerivationModulo` in nixcpp.
125    ///
126    /// It returns the sha256 digest of the derivation ATerm representation,
127    /// except that:
128    ///  -  any input derivation paths have beed replaced "by the result of a
129    ///     recursive call to this function" and that
130    ///  - for fixed-output derivations the special
131    ///    `fixed:out:${algo}:${digest}:${fodPath}` string is hashed instead of
132    ///    the A-Term.
133    ///
134    /// It's up to the caller of this function to provide a (infallible) lookup
135    /// function to query the [Derivation::hash_derivation_modulo] of direct
136    /// input derivations, by their [StorePathRef].
137    /// It will only be called in case the derivation is not a fixed-output
138    /// derivation.
139    pub fn hash_derivation_modulo<F>(&self, fn_lookup_hash_derivation_modulo: F) -> [u8; 32]
140    where
141        F: Fn(&StorePathRef) -> [u8; 32],
142    {
143        // Fixed-output derivations return a fixed hash.
144        // Non-Fixed-output derivations return the sha256 digest of the ATerm
145        // notation, but with all input_derivation paths replaced by a recursive
146        // call to this function.
147        // We call [fn_lookup_hash_derivation_modulo] rather than recursing
148        // ourselves, so callers can precompute this.
149        self.fod_digest().unwrap_or({
150            // For each input_derivation, look up the hash derivation modulo,
151            // and replace the derivation path with the hash_derivation_modulo.
152            let mut replacements = Vec::from_iter(self.input_derivations.iter().map(
153                |(drv_path, output_names)| {
154                    (
155                        fn_lookup_hash_derivation_modulo(&drv_path.as_ref()),
156                        output_names,
157                    )
158                },
159            ));
160            // changing the keys changes the order, so we need to sort by keys again
161            replacements.sort_by_key(|(k, _output_names)| *k);
162
163            let mut hasher = Sha256::new();
164            self.serialize_with_replacements(&mut hasher, replacements.into_iter())
165                .unwrap();
166
167            hasher.finalize().into()
168        })
169    }
170
171    /// This calculates all output paths of a `Derivation` and updates the struct.
172    ///
173    /// It requires the outputs to be initially without output paths.
174    /// This means, [`Output::path`] needs to be `None` for each output.
175    ///
176    /// Output path calculation requires knowledge of the
177    /// [`hash_derivation_modulo`], which (in case of non-fixed-output
178    /// derivations) also requires knowledge of the
179    /// [`hash_derivation_modulo`] of input derivations (recursively).
180    ///
181    /// To avoid recursing and doing unnecessary calculation, we simply
182    /// ask the caller of this function to provide the result of the
183    /// [`hash_derivation_modulo`] call of the current [`Derivation`],
184    /// and leave it up to them to calculate it when needed.
185    ///
186    /// On completion, [`Output::path`] of each output is set to the calculated output path.
187    ///
188    /// [`hash_derivation_modulo`]: Derivation::hash_derivation_modulo
189    pub fn calculate_output_paths(
190        &mut self,
191        drv_name: &str,
192        hash_derivation_modulo: &[u8; 32],
193    ) -> Result<(), DerivationError> {
194        self.outputs
195            .calculate_output_paths(drv_name, hash_derivation_modulo)?;
196
197        // The fingerprint and hash differs per output
198        for (output_name, output) in self.outputs.iter() {
199            self.environment.insert(
200                output_name.to_string(),
201                output.path.as_ref().unwrap().to_absolute_path().into(),
202            );
203        }
204
205        Ok(())
206    }
207}
208
209#[cfg(feature = "async")]
210#[allow(dead_code)]
211trait DerivationAsyncExt {
212    /// Parse an Derivation in ATerm serialization, and validate it passes
213    /// our set of validations, from a asynchronous buffered reader.
214    /// This is a streaming variant of [Derivation::from_aterm_bytes].
215    async fn from_streaming_aterm_bytes<R>(reader: R) -> Result<Derivation, parser::Error<Vec<u8>>>
216    where
217        R: tokio::io::AsyncBufRead + Unpin + Send;
218}
219
220#[cfg(feature = "async")]
221impl DerivationAsyncExt for Derivation {
222    async fn from_streaming_aterm_bytes<R>(
223        mut reader: R,
224    ) -> Result<Derivation, parser::Error<Vec<u8>>>
225    where
226        R: tokio::io::AsyncBufRead + Unpin + Send,
227    {
228        use tokio::io::AsyncBufReadExt;
229        let mut buffer = Vec::new();
230        loop {
231            let rest = reader.fill_buf().await.unwrap();
232            let length = rest.len();
233
234            // We reached EOF, we can stop and return incompleteness.
235            if length == 0 {
236                return Err(ParserError::Incomplete);
237            }
238
239            buffer.extend_from_slice(rest);
240
241            // Parse the so-far internal buffer of reader.
242            match parser::parse_streaming(&buffer) {
243                (Err(parser::Error::Incomplete), _) => {
244                    reader.consume(length);
245                    continue;
246                }
247                (Ok(derivation), leftover) => {
248                    // We cannot inline it in the next call because `reader` is mutably borrowed
249                    // and has a relationship with the lifetime of `leftover`.
250                    let leftover_length = leftover.len();
251
252                    // Well, if we already had consumed the leftovers of the past fetch
253                    // while believing we were just parsing incomplete ATerm, there's nothing
254                    // we can do about it. The protocol is made this way.
255                    if length >= leftover_length {
256                        // We still have leftover, let's not consume it.
257                        // It's not for us.
258                        reader.consume(length - leftover_length);
259                    }
260                    return Ok(derivation);
261                }
262                (Err(e), _) => {
263                    return Err(e.into());
264                }
265            }
266        }
267    }
268}