about summary refs log tree commit diff
path: root/tvix/nix-compat/src/derivation/output.rs
diff options
context:
space:
mode:
authorPeter Kolloch <info@eigenvalue.net>2024-02-21T11·31+0700
committerclbot <clbot@tvl.fyi>2024-02-21T11·38+0000
commitfde488ec6dc444561ae353f979d87c8ae87261fb (patch)
treec9b673d9d0fc19709f61c88ceb59092f11d7facd /tvix/nix-compat/src/derivation/output.rs
parent035f617b7f11f2ec4a9e08e3a31a175e71a6544b (diff)
feat(tvix/nix-compat): Use `StorePath` in `Output` r/7585
https: //b.tvl.fyi/issues/264
Change-Id: Icb09be9643245cc68d09f01d7723af2d44d6bd1a
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11001
Autosubmit: Peter Kolloch <info@eigenvalue.net>
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/nix-compat/src/derivation/output.rs')
-rw-r--r--tvix/nix-compat/src/derivation/output.rs57
1 files changed, 34 insertions, 23 deletions
diff --git a/tvix/nix-compat/src/derivation/output.rs b/tvix/nix-compat/src/derivation/output.rs
index 521ea3dedc9c..e2a8282c7686 100644
--- a/tvix/nix-compat/src/derivation/output.rs
+++ b/tvix/nix-compat/src/derivation/output.rs
@@ -1,14 +1,16 @@
-use crate::derivation::OutputError;
 use crate::nixhash::CAHash;
 use crate::store_path::StorePathRef;
+use crate::{derivation::OutputError, store_path::StorePath};
+use serde::de::Unexpected;
 use serde::{Deserialize, Serialize};
 use serde_json::Map;
+use std::borrow::Cow;
 
 /// References the derivation output.
 #[derive(Clone, Debug, Default, Eq, PartialEq, Serialize)]
 pub struct Output {
     /// Store path of build result.
-    pub path: String,
+    pub path: Option<StorePath>,
 
     #[serde(flatten)]
     pub ca_hash: Option<CAHash>, // we can only represent a subset here.
@@ -20,18 +22,21 @@ impl<'de> Deserialize<'de> for Output {
         D: serde::Deserializer<'de>,
     {
         let fields = Map::deserialize(deserializer)?;
+        let path: &str = fields
+            .get("path")
+            .ok_or(serde::de::Error::missing_field(
+                "`path` is missing but required for outputs",
+            ))?
+            .as_str()
+            .ok_or(serde::de::Error::invalid_type(
+                serde::de::Unexpected::Other("certainly not a string"),
+                &"a string",
+            ))?;
+
+        let path = StorePathRef::from_absolute_path(path.as_bytes())
+            .map_err(|_| serde::de::Error::invalid_value(Unexpected::Str(path), &"StorePath"))?;
         Ok(Self {
-            path: fields
-                .get("path")
-                .ok_or(serde::de::Error::missing_field(
-                    "`path` is missing but required for outputs",
-                ))?
-                .as_str()
-                .ok_or(serde::de::Error::invalid_type(
-                    serde::de::Unexpected::Other("certainly not a string"),
-                    &"a string",
-                ))?
-                .to_owned(),
+            path: Some(path.to_owned()),
             ca_hash: CAHash::from_map::<D>(&fields)?,
         })
     }
@@ -42,6 +47,14 @@ impl Output {
         self.ca_hash.is_some()
     }
 
+    /// The output path as a string -- use `""` to indicate an unset output path.
+    pub fn path_str(&self) -> Cow<str> {
+        match &self.path {
+            None => Cow::Borrowed(""),
+            Some(path) => Cow::Owned(path.to_absolute_path()),
+        }
+    }
+
     pub fn validate(&self, validate_output_paths: bool) -> Result<(), OutputError> {
         if let Some(fixed_output_hash) = &self.ca_hash {
             match fixed_output_hash {
@@ -52,10 +65,8 @@ impl Output {
             }
         }
 
-        if validate_output_paths {
-            if let Err(e) = StorePathRef::from_absolute_path(self.path.as_bytes()) {
-                return Err(OutputError::InvalidOutputPath(self.path.to_string(), e));
-            }
+        if validate_output_paths && self.path.is_none() {
+            return Err(OutputError::MissingOutputPath);
         }
         Ok(())
     }
@@ -67,7 +78,7 @@ impl Output {
 fn deserialize_valid_input_addressed_output() {
     let json_bytes = r#"
     {
-      "path": "/nix/store/blablabla"
+      "path": "/nix/store/00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432"
     }"#;
     let output: Output = serde_json::from_str(json_bytes).expect("must parse");
 
@@ -80,7 +91,7 @@ fn deserialize_valid_input_addressed_output() {
 fn deserialize_valid_fixed_output() {
     let json_bytes = r#"
     {
-        "path": "/nix/store/blablablabla",
+        "path": "/nix/store/00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432",
         "hash": "08813cbee9903c62be4c5027726a418a300da4500b2d369d3af9286f4815ceba",
         "hashAlgo": "r:sha256"
     }"#;
@@ -95,7 +106,7 @@ fn deserialize_valid_fixed_output() {
 fn deserialize_with_error_invalid_hash_encoding_fixed_output() {
     let json_bytes = r#"
     {
-        "path": "/nix/store/blablablabla",
+        "path": "/nix/store/00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432",
         "hash": "IAMNOTVALIDNIXBASE32",
         "hashAlgo": "r:sha256"
     }"#;
@@ -110,7 +121,7 @@ fn deserialize_with_error_invalid_hash_encoding_fixed_output() {
 fn deserialize_with_error_invalid_hash_algo_fixed_output() {
     let json_bytes = r#"
     {
-        "path": "/nix/store/blablablabla",
+        "path": "/nix/store/00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432",
         "hash": "08813cbee9903c62be4c5027726a418a300da4500b2d369d3af9286f4815ceba",
         "hashAlgo": "r:sha1024"
     }"#;
@@ -125,7 +136,7 @@ fn deserialize_with_error_invalid_hash_algo_fixed_output() {
 fn deserialize_with_error_missing_hash_algo_fixed_output() {
     let json_bytes = r#"
     {
-        "path": "/nix/store/blablablabla",
+        "path": "/nix/store/00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432",
         "hash": "08813cbee9903c62be4c5027726a418a300da4500b2d369d3af9286f4815ceba",
     }"#;
     let output: Result<Output, _> = serde_json::from_str(json_bytes);
@@ -139,7 +150,7 @@ fn deserialize_with_error_missing_hash_algo_fixed_output() {
 fn deserialize_with_error_missing_hash_fixed_output() {
     let json_bytes = r#"
     {
-        "path": "/nix/store/blablablabla",
+        "path": "/nix/store/00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432",
         "hashAlgo": "r:sha1024"
     }"#;
     let output: Result<Output, _> = serde_json::from_str(json_bytes);