snix_glue/builtins/
fetchers.rs

1//! Contains builtins that fetch paths from the Internet, or local filesystem.
2
3use super::utils::select_string;
4use crate::{
5    fetchers::{Fetch, url_basename},
6    snix_store_io::SnixStoreIO,
7};
8use nix_compat::nixhash::{HashAlgo, NixHash};
9use snix_eval::builtin_macros::builtins;
10use snix_eval::generators::Gen;
11use snix_eval::generators::GenCo;
12use snix_eval::{CatchableErrorKind, ErrorKind, Value, try_cek};
13use std::rc::Rc;
14use url::Url;
15
16// Used as a return type for extract_fetch_args, which is sharing some
17// parsing code between the fetchurl and fetchTarball builtins.
18struct NixFetchArgs {
19    url: Url,
20    name: Option<String>,
21    sha256: Option<[u8; 32]>,
22}
23
24// `fetchurl` and `fetchTarball` accept a single argument, which can either be the URL (as string),
25// or an attrset, where `url`, `sha256` and `name` keys are allowed.
26async fn extract_fetch_args(
27    co: &GenCo,
28    args: Value,
29) -> Result<Result<NixFetchArgs, CatchableErrorKind>, ErrorKind> {
30    if let Ok(url_str) = args.to_str() {
31        // Get the raw bytes, not the ToString repr.
32        let url_str =
33            String::from_utf8(url_str.as_bytes().to_vec()).map_err(|_| ErrorKind::Utf8)?;
34
35        // Parse the URL.
36        let url = Url::parse(&url_str).map_err(|e| ErrorKind::SnixError(Rc::new(e)))?;
37
38        return Ok(Ok(NixFetchArgs {
39            url,
40            name: None,
41            sha256: None,
42        }));
43    }
44
45    let attrs = args.to_attrs().map_err(|_| ErrorKind::TypeError {
46        expected: "attribute set or contextless string",
47        actual: args.type_of(),
48    })?;
49
50    // Reject disallowed attrset keys, to match Nix' behaviour.
51    // We complain about the first unexpected key we find in the list.
52    const VALID_KEYS: [&[u8]; 3] = [b"url", b"name", b"sha256"];
53    if let Some(first_invalid_key) = attrs.keys().find(|k| !&VALID_KEYS.contains(&k.as_bytes())) {
54        return Err(ErrorKind::UnexpectedArgumentBuiltin(
55            first_invalid_key.clone(),
56        ));
57    }
58
59    let url_str = try_cek!(select_string(co, &attrs, "url").await?)
60        .ok_or_else(|| ErrorKind::AttributeNotFound { name: "url".into() })?;
61    let name = try_cek!(select_string(co, &attrs, "name").await?);
62    let sha256_str = try_cek!(select_string(co, &attrs, "sha256").await?);
63
64    Ok(Ok(NixFetchArgs {
65        url: Url::parse(&url_str).map_err(|e| ErrorKind::SnixError(Rc::new(e)))?,
66        name,
67        // parse the sha256 string into a digest, and bail out if it's not sha256.
68        sha256: sha256_str
69            .map(
70                |sha256_str| match NixHash::from_str(&sha256_str, Some(HashAlgo::Sha256)) {
71                    Ok(NixHash::Sha256(digest)) => Ok(digest),
72                    _ => Err(ErrorKind::InvalidHash(sha256_str)),
73                },
74            )
75            .transpose()?,
76    }))
77}
78
79#[allow(unused_variables)] // for the `state` arg, for now
80#[builtins(state = "Rc<SnixStoreIO>")]
81pub(crate) mod fetcher_builtins {
82    use bstr::ByteSlice;
83    use nix_compat::{flakeref, nixhash::NixHash};
84    use snix_eval::try_cek_to_value;
85    use std::collections::BTreeMap;
86
87    use super::*;
88
89    /// Consumes a fetch.
90    /// If there is enough info to calculate the store path without fetching,
91    /// queue the fetch to be fetched lazily, and return the store path.
92    /// If there's not enough info to calculate it, do the fetch now, and then
93    /// return the store path.
94    fn fetch_lazy(state: Rc<SnixStoreIO>, name: String, fetch: Fetch) -> Result<Value, ErrorKind> {
95        match fetch
96            .store_path(&name)
97            .map_err(|e| ErrorKind::SnixError(Rc::new(e)))?
98        {
99            Some(store_path) => {
100                // Move the fetch to KnownPaths, so it can be actually fetched later.
101                let sp = state
102                    .known_paths
103                    .borrow_mut()
104                    .add_fetch(fetch, &name)
105                    .expect("Snix bug: should only fail if the store path cannot be calculated");
106
107                debug_assert_eq!(
108                    sp, store_path,
109                    "calculated store path by KnownPaths should match"
110                );
111
112                // Emit the calculated Store Path.
113                Ok(Value::Path(Box::new(store_path.to_absolute_path().into())))
114            }
115            None => {
116                // If we don't have enough info, do the fetch now.
117                let (store_path, _path_info) = state
118                    .tokio_handle
119                    .block_on(async { state.fetcher.ingest_and_persist(&name, fetch).await })
120                    .map_err(|e| ErrorKind::SnixError(Rc::new(e)))?;
121
122                Ok(Value::Path(Box::new(store_path.to_absolute_path().into())))
123            }
124        }
125    }
126
127    #[builtin("fetchurl")]
128    async fn builtin_fetchurl(
129        state: Rc<SnixStoreIO>,
130        co: GenCo,
131        args: Value,
132    ) -> Result<Value, ErrorKind> {
133        let args = try_cek_to_value!(extract_fetch_args(&co, args).await?);
134
135        // Derive the name from the URL basename if not set explicitly.
136        let name = args
137            .name
138            .unwrap_or_else(|| url_basename(&args.url).to_owned());
139
140        fetch_lazy(
141            state,
142            name,
143            Fetch::URL {
144                url: args.url,
145                exp_hash: args.sha256.map(NixHash::Sha256),
146            },
147        )
148    }
149
150    #[builtin("fetchTarball")]
151    async fn builtin_fetch_tarball(
152        state: Rc<SnixStoreIO>,
153        co: GenCo,
154        args: Value,
155    ) -> Result<Value, ErrorKind> {
156        let args = try_cek_to_value!(extract_fetch_args(&co, args).await?);
157
158        // Name defaults to "source" if not set explicitly.
159        const DEFAULT_NAME_FETCH_TARBALL: &str = "source";
160        let name = args
161            .name
162            .unwrap_or_else(|| DEFAULT_NAME_FETCH_TARBALL.to_owned());
163
164        fetch_lazy(
165            state,
166            name,
167            Fetch::Tarball {
168                url: args.url,
169                exp_nar_sha256: args.sha256,
170            },
171        )
172    }
173
174    #[builtin("fetchGit")]
175    async fn builtin_fetch_git(
176        state: Rc<SnixStoreIO>,
177        co: GenCo,
178        args: Value,
179    ) -> Result<Value, ErrorKind> {
180        Err(ErrorKind::NotImplemented("fetchGit"))
181    }
182
183    // FUTUREWORK: make it a feature flag once #64 is implemented
184    #[builtin("parseFlakeRef")]
185    async fn builtin_parse_flake_ref(
186        state: Rc<SnixStoreIO>,
187        co: GenCo,
188        value: Value,
189    ) -> Result<Value, ErrorKind> {
190        let flake_ref = value.to_str()?;
191        let flake_ref_str = flake_ref.to_str()?;
192
193        let fetch_args = flake_ref_str
194            .parse()
195            .map_err(|err| ErrorKind::SnixError(Rc::new(err)))?;
196
197        // Convert the FlakeRef to our Value format
198        let mut attrs = BTreeMap::new();
199
200        // Extract type and url based on the variant
201        match fetch_args {
202            flakeref::FlakeRef::Git { url, .. } => {
203                attrs.insert("type".into(), Value::from("git"));
204                attrs.insert("url".into(), Value::from(url.to_string()));
205            }
206            flakeref::FlakeRef::GitHub {
207                owner, repo, r#ref, ..
208            } => {
209                attrs.insert("type".into(), Value::from("github"));
210                attrs.insert("owner".into(), Value::from(owner));
211                attrs.insert("repo".into(), Value::from(repo));
212                if let Some(ref_name) = r#ref {
213                    attrs.insert("ref".into(), Value::from(ref_name));
214                }
215            }
216            flakeref::FlakeRef::GitLab { owner, repo, .. } => {
217                attrs.insert("type".into(), Value::from("gitlab"));
218                attrs.insert("owner".into(), Value::from(owner));
219                attrs.insert("repo".into(), Value::from(repo));
220            }
221            flakeref::FlakeRef::File { url, .. } => {
222                attrs.insert("type".into(), Value::from("file"));
223                attrs.insert("url".into(), Value::from(url.to_string()));
224            }
225            flakeref::FlakeRef::Tarball { url, .. } => {
226                attrs.insert("type".into(), Value::from("tarball"));
227                attrs.insert("url".into(), Value::from(url.to_string()));
228            }
229            flakeref::FlakeRef::Path { path, .. } => {
230                attrs.insert("type".into(), Value::from("path"));
231                attrs.insert(
232                    "path".into(),
233                    Value::from(path.to_string_lossy().into_owned()),
234                );
235            }
236            _ => {
237                // For all other ref types, return a simple type/url attributes
238                attrs.insert("type".into(), Value::from("indirect"));
239                attrs.insert("url".into(), Value::from(flake_ref_str));
240            }
241        }
242
243        Ok(Value::Attrs(Box::new(attrs.into())))
244    }
245}