diff options
Diffstat (limited to 'tvix')
-rw-r--r-- | tvix/nix-compat/src/lib.rs | 1 | ||||
-rw-r--r-- | tvix/nix-compat/src/store_path/utils.rs | 41 | ||||
-rw-r--r-- | tvix/nix-compat/src/texthash.rs | 42 |
3 files changed, 40 insertions, 44 deletions
diff --git a/tvix/nix-compat/src/lib.rs b/tvix/nix-compat/src/lib.rs index 2aac179bcfce..4cfd708e52d8 100644 --- a/tvix/nix-compat/src/lib.rs +++ b/tvix/nix-compat/src/lib.rs @@ -5,4 +5,3 @@ pub mod nixhash; mod nixhash_algos; mod nixhash_with_mode; pub mod store_path; -mod texthash; diff --git a/tvix/nix-compat/src/store_path/utils.rs b/tvix/nix-compat/src/store_path/utils.rs index 0d96414a212d..449117717260 100644 --- a/tvix/nix-compat/src/store_path/utils.rs +++ b/tvix/nix-compat/src/store_path/utils.rs @@ -1,6 +1,6 @@ use crate::nixbase32; +use crate::nixhash::NixHash; use crate::store_path::StorePath; -use crate::texthash::text_hash_string; use sha2::{Digest, Sha256}; use super::Error; @@ -60,6 +60,45 @@ pub fn build_store_path_from_fingerprint( StorePath::from_string(format!("{}-{}", nixbase32::encode(&compressed), name).as_str()) } +/// This contains the Nix logic to create "text hash strings", which are used +/// in `builtins.toFile`, as well as in Derivation Path calculation. +/// +/// A text hash is calculated by concatenating the following fields, separated by a `:`: +/// +/// - text +/// - references, individually joined by `:` +/// - the nix_hash_string representation of the sha256 digest of some contents +/// - the value of `storeDir` +/// - the name +pub fn text_hash_string<S: AsRef<str>, I: IntoIterator<Item = S>, C: AsRef<[u8]>>( + name: &str, + content: C, + references: I, +) -> String { + let mut s = String::from("text:"); + + for reference in references { + s.push_str(reference.as_ref()); + s.push(':'); + } + + // the nix_hash_string representation of the sha256 digest of some contents + s.push_str( + &{ + let content_digest = { + let hasher = Sha256::new_with_prefix(content); + hasher.finalize() + }; + NixHash::new(crate::nixhash::HashAlgo::Sha256, content_digest.to_vec()) + } + .to_nix_hash_string(), + ); + + s.push_str(&format!(":{}:{}", crate::store_path::STORE_DIR, name)); + + s +} + /// Nix placeholders (i.e. values returned by `builtins.placeholder`) /// are used to populate outputs with paths that must be /// string-replaced with the actual placeholders later, at runtime. diff --git a/tvix/nix-compat/src/texthash.rs b/tvix/nix-compat/src/texthash.rs deleted file mode 100644 index 959a4dc3dcf0..000000000000 --- a/tvix/nix-compat/src/texthash.rs +++ /dev/null @@ -1,42 +0,0 @@ -use sha2::{Digest, Sha256}; - -use crate::{nixhash::NixHash, store_path}; - -/// This contains the Nix logic to create "text hash strings", which are used -/// in `builtins.toFile`, as well as in Derivation Path calculation. -/// -/// A text hash is calculated by concatenating the following fields, separated by a `:`: -/// -/// - text -/// - references, individually joined by `:` -/// - the nix_hash_string representation of the sha256 digest of some contents -/// - the value of `storeDir` -/// - the name -pub fn text_hash_string<S: AsRef<str>, I: IntoIterator<Item = S>, C: AsRef<[u8]>>( - name: &str, - content: C, - references: I, -) -> String { - let mut s = String::from("text:"); - - for reference in references { - s.push_str(reference.as_ref()); - s.push(':'); - } - - // the nix_hash_string representation of the sha256 digest of some contents - s.push_str( - &{ - let content_digest = { - let hasher = Sha256::new_with_prefix(content); - hasher.finalize() - }; - NixHash::new(crate::nixhash::HashAlgo::Sha256, content_digest.to_vec()) - } - .to_nix_hash_string(), - ); - - s.push_str(&format!(":{}:{}", store_path::STORE_DIR, name)); - - s -} |