Skip to main content

sui_cache/
lib.rs

1//! Built-in binary cache server and push pipeline for sui.
2//!
3//! Replaces Attic, Cachix, and nix-serve with a single integrated component.
4//! Implements the standard Nix binary cache HTTP protocol (narinfo + NAR).
5//!
6//! # Architecture
7//!
8//! - [`sui_castore`] — pluggable storage backends (shared with sui-registry);
9//!   re-exported from here for backward compatibility.
10//! - [`server`] — axum HTTP server implementing the cache protocol
11//! - [`signing`] — ed25519 key management and narinfo signing
12//! - [`push`] — pipeline to push store paths to the cache
13//! - [`gc`] — garbage collection of unreferenced cache entries
14//! - [`config`] — cache configuration types (CacheConfig; BackendConfig is in sui-castore)
15
16pub mod config;
17pub mod gc;
18pub mod push;
19pub mod server;
20pub mod signing;
21
22// ---------------------------------------------------------------------------
23// Backward-compatibility re-exports from sui-castore.
24//
25// Every `sui_cache::X` path that existed before the extract-and-dominate
26// refactor continues to resolve — callers need zero changes. The canonical
27// home is now `sui_castore::X`.
28// ---------------------------------------------------------------------------
29
30/// `CacheError` is now `sui_castore::StoreError` (same variants, same derives).
31/// This type alias preserves every existing `sui_cache::CacheError` use site.
32pub use sui_castore::StoreError as CacheError;
33
34pub use config::CacheConfig;
35
36// BackendConfig lives in sui-castore; re-export at the sui-cache surface so
37// `sui_cache::BackendConfig` still resolves.
38pub use sui_castore::BackendConfig;
39
40// `${VAR}` config-text expansion also lives in sui-castore; re-export here so
41// `sui cache serve`'s config loader (`sui_cache::expand_env_vars`) can inject a
42// secret-sourced DSN password without the value ever entering the ConfigMap.
43pub use sui_castore::{expand_env_vars, ExpandEnvError};
44
45pub use gc::GcResult;
46pub use push::PushResult;
47pub use server::{build_router, serve, AppState};
48pub use signing::{verify_narinfo_signature, CacheSigner};
49
50// Storage primitives — all moved to sui-castore, re-exported here.
51pub use sui_castore::{
52    advertised_nar_url, advertised_url_line, build_backend, bytes_stream, collect_nar,
53    empty_stream, file_stream, is_addressable_nar_path, is_servable_narinfo, referrer_of,
54    spool_or_buffer,
55    whole_value_stream, BytesNarSource, FileNarSource, LocalStorage, MemNarRefIndex, NarRefIndex,
56    NarRefKey, NarRefScan, NarResidency, NarSource, NarStream, PgCacheConn, PgStorageBackend,
57    PgTable, RedisBackend, RedisConn, S3Storage, SpooledNarSource, StorageBackend, StorageIndex,
58    TieredBackend, TieredTier, WritePolicy, DEFAULT_INGEST_MEMORY_CAP, NAR_CHUNK_BYTES,
59    NAR_REF_PREFIX, TIERED_BACKEND_TIER,
60};
61
62#[cfg(feature = "redis-client")]
63pub use sui_castore::RedisConnectionManager;
64
65#[cfg(feature = "postgres")]
66pub use sui_castore::SqlxPgCacheConn;
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn cache_error_is_store_error_display() {
74        let e = CacheError::PathNotFound("/nix/store/abc".to_string());
75        assert!(format!("{e}").contains("/nix/store/abc"));
76    }
77
78    #[test]
79    fn cache_error_io_display() {
80        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "missing");
81        let e = CacheError::Io(io_err);
82        assert!(format!("{e}").contains("missing"));
83    }
84
85    #[test]
86    fn cache_error_signing_display() {
87        let e = CacheError::Signing("bad key".to_string());
88        assert!(format!("{e}").contains("bad key"));
89    }
90
91    #[test]
92    fn cache_error_not_implemented_display() {
93        let e = CacheError::NotImplemented("S3");
94        assert!(format!("{e}").contains("S3"));
95    }
96
97    #[test]
98    fn cache_error_narinfo_display() {
99        let e = CacheError::NarInfo("parse failed".to_string());
100        assert!(format!("{e}").contains("parse failed"));
101    }
102}