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::{CACHE_TIER_ENV, 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::{ExpandEnvError, expand_env_vars};
44
45pub use gc::GcResult;
46pub use push::{LevelOutOfRange, NarCodec, PushResult, XzLevel, ZstdLevel};
47pub use server::{AppState, build_router, serve};
48pub use signing::{CacheSigner, verify_narinfo_signature};
49
50// Storage primitives — all moved to sui-castore, re-exported here.
51pub use sui_castore::{
52    BytesNarSource, DEFAULT_INGEST_MEMORY_CAP, FileNarSource, LocalStorage, MemNarRefIndex,
53    NAR_CHUNK_BYTES, NAR_REF_PREFIX, NarRefIndex, NarRefKey, NarRefScan, NarResidency, NarSource,
54    NarStream, PgCacheConn, PgStorageBackend, PgTable, RedisBackend, RedisConn, S3Storage,
55    SpooledNarSource, StorageBackend, StorageIndex, TIERED_BACKEND_TIER, TieredBackend, TieredTier,
56    WritePolicy, advertised_nar_url, advertised_url_line, build_backend, bytes_stream, collect_nar,
57    empty_stream, file_stream, is_addressable_nar_path, is_servable_narinfo, referrer_of,
58    spool_or_buffer, whole_value_stream,
59};
60
61#[cfg(feature = "redis-client")]
62pub use sui_castore::RedisConnectionManager;
63
64#[cfg(feature = "postgres")]
65pub use sui_castore::SqlxPgCacheConn;
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn cache_error_is_store_error_display() {
73        let e = CacheError::PathNotFound("/nix/store/abc".to_string());
74        assert!(format!("{e}").contains("/nix/store/abc"));
75    }
76
77    #[test]
78    fn cache_error_io_display() {
79        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "missing");
80        let e = CacheError::Io(io_err);
81        assert!(format!("{e}").contains("missing"));
82    }
83
84    #[test]
85    fn cache_error_signing_display() {
86        let e = CacheError::Signing("bad key".to_string());
87        assert!(format!("{e}").contains("bad key"));
88    }
89
90    #[test]
91    fn cache_error_not_implemented_display() {
92        let e = CacheError::NotImplemented("S3");
93        assert!(format!("{e}").contains("S3"));
94    }
95
96    #[test]
97    fn cache_error_narinfo_display() {
98        let e = CacheError::NarInfo("parse failed".to_string());
99        assert!(format!("{e}").contains("parse failed"));
100    }
101}