snix_glue/
snix_io.rs

1//! This module implements a wrapper around snix-eval's [EvalIO] type,
2//! adding functionality which is required by snix-cli:
3//!
4//! 1. Handling the C++ Nix `__corepkgs__`-hack for nixpkgs bootstrapping.
5//!
6//! All uses of [EvalIO] in snix-cli must make use of this wrapper,
7//! otherwise nixpkgs bootstrapping will not work.
8
9use snix_eval::{EvalIO, FileType};
10use std::io::{self, Cursor};
11use std::path::{Path, PathBuf};
12
13// TODO: Merge this together with SnixStoreIO?
14pub struct SnixIO<T> {
15    // Actual underlying [EvalIO] implementation.
16    actual: T,
17}
18
19impl<T> SnixIO<T> {
20    pub fn new(actual: T) -> Self {
21        Self { actual }
22    }
23}
24
25impl<T> EvalIO for SnixIO<T>
26where
27    T: AsRef<dyn EvalIO>,
28{
29    fn store_dir(&self) -> Option<String> {
30        self.actual.as_ref().store_dir()
31    }
32
33    fn import_path(&self, path: &Path) -> io::Result<PathBuf> {
34        self.actual.as_ref().import_path(path)
35    }
36
37    fn path_exists(&self, path: &Path) -> io::Result<bool> {
38        if path.starts_with("/__corepkgs__") {
39            return Ok(true);
40        }
41
42        self.actual.as_ref().path_exists(path)
43    }
44
45    fn open(&self, path: &Path) -> io::Result<Box<dyn io::Read>> {
46        // Bundled version of corepkgs/fetchurl.nix. The counterpart
47        // of this happens in [crate::configure_nix_path], where the `nix_path`
48        // of the evaluation has `nix=/__corepkgs__` added to it.
49        //
50        // This workaround is similar to what cppnix does for passing
51        // the path through.
52        //
53        // TODO: this comparison is bad we should use the sane path library.
54        if path.starts_with("/__corepkgs__/fetchurl.nix") {
55            return Ok(Box::new(Cursor::new(include_bytes!("fetchurl.nix"))));
56        }
57
58        self.actual.as_ref().open(path)
59    }
60
61    fn file_type(&self, path: &Path) -> io::Result<FileType> {
62        self.actual.as_ref().file_type(path)
63    }
64
65    fn read_dir(&self, path: &Path) -> io::Result<Vec<(bytes::Bytes, FileType)>> {
66        self.actual.as_ref().read_dir(path)
67    }
68}