diff options
author | Jürgen Hahn <mail.jhahn@gmail.com> | 2023-01-02T15·09+0100 |
---|---|---|
committer | jrhahn <mail.jhahn@gmail.com> | 2023-01-04T12·24+0000 |
commit | 6f993b8bde8201213fe2953ea663ac387de916e3 (patch) | |
tree | f82cd8094172c18a5eb0601f278fa4205e48344c /tvix/derivation/src/derivation.rs | |
parent | 79c05f38109d2eac07b9ba893b9b6f67fd34c29c (diff) |
feat(tvix/derivation): add nix drv path generation to Derivation r/5580
This adds a function to generate the derivation path. The computation is based on the Go implementation. Change-Id: Iae89db4976f5fd9208f0453f73688689a245cd66 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7729 Tested-by: BuildkiteCI Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/derivation/src/derivation.rs')
-rw-r--r-- | tvix/derivation/src/derivation.rs | 71 |
1 files changed, 70 insertions, 1 deletions
diff --git a/tvix/derivation/src/derivation.rs b/tvix/derivation/src/derivation.rs index e624be20cab3..9531157d6f98 100644 --- a/tvix/derivation/src/derivation.rs +++ b/tvix/derivation/src/derivation.rs @@ -1,7 +1,10 @@ +use crate::nix_hash; use crate::output::Output; use crate::write; use serde::{Deserialize, Serialize}; -use std::{collections::BTreeMap, fmt, fmt::Write}; +use sha2::{Digest, Sha256}; +use std::{collections::BTreeMap, fmt, fmt::Write, iter::FromIterator}; +use tvix_store::nixbase32::NIXBASE32; #[derive(Serialize, Deserialize)] pub struct Derivation { @@ -31,6 +34,72 @@ impl Derivation { Ok(()) } + + /// Returns the path of a Derivation struct. + /// + /// The path is calculated like this: + /// - Write the fingerprint of the Derivation to the sha256 hash function. + /// This is: `text:`, + /// all d.InputDerivations and d.InputSources (sorted, separated by a `:`), + /// a `:`, + /// a `sha256:`, followed by the sha256 digest of the ATerm representation (hex-encoded) + /// a `:`, + /// the storeDir, followed by a `:`, + /// the name of a derivation, + /// a `.drv`. + /// - Write the .drv A-Term contents to a hash function + /// - Take the digest, run hash.CompressHash(digest, 20) on it. + /// - Encode it with nixbase32 + /// - Construct the full path $storeDir/$nixbase32EncodedCompressedHash-$name.drv + pub fn calculate_derivation_path(&self, name: &str) -> String { + let mut hasher = Sha256::new(); + + // collect the list of paths from input_sources and input_derivations + // into a sorted list, and join them by : + hasher.update(write::TEXT_COLON); + + let concat_inputs: Vec<String> = { + let mut inputs = self.input_sources.clone(); + let input_derivation_keys: Vec<String> = + self.input_derivations.keys().cloned().collect(); + inputs.extend(input_derivation_keys); + inputs.sort(); + inputs + }; + + for input in concat_inputs { + hasher.update(input); + hasher.update(write::COLON); + } + + // calculate the sha256 hash of the ATerm representation, and represent + // it as a hex-encoded string (prefixed with sha256:). + hasher.update(write::SHA256_COLON); + + let digest = { + let mut derivation_hasher = Sha256::new(); + derivation_hasher.update(self.to_string()); + derivation_hasher.finalize() + }; + + hasher.update(format!("{:x}", digest)); + hasher.update(write::COLON); + hasher.update(write::STORE_PATH_COLON); + hasher.update(name); + hasher.update(write::DOT_FILE_EXT); + + let compressed = { + let aterm_digest = Vec::from_iter(hasher.finalize()); + nix_hash::compress_hash(&aterm_digest, 20) + }; + + format!( + "{}-{}{}", + NIXBASE32.encode(&compressed), + name, + write::DOT_FILE_EXT + ) + } } impl fmt::Display for Derivation { |