nika_daemon/lib.rs
1//! Nika native daemon — background services for secrets, jobs, watch, and cache.
2//!
3//! The daemon is Nika's optional background brain. `nika run` works without it,
4//! but the daemon adds persistent features: keychain secrets, job scheduling,
5//! file watching, and LLM response caching.
6//!
7//! ## Architecture
8//!
9//! - Single binary: `nika daemon start` uses the same binary
10//! - Unix socket IPC: `~/.nika/daemon/nika.sock`
11//! - Wire format: 4-byte big-endian length + JSON payload
12//! - tokio-native async runtime
13//!
14//! ## Crate Independence
15//!
16//! This crate depends on `nika-core` only (for path constants and provider info).
17//! It does NOT depend on `nika-engine` — the daemon is lightweight.
18
19#[cfg(unix)]
20pub mod client;
21pub mod error;
22pub mod events;
23#[cfg(unix)]
24pub mod install;
25#[cfg(unix)]
26pub mod lifecycle;
27pub mod protocol;
28#[cfg(unix)]
29pub mod server;
30pub mod services;
31pub mod storage;
32
33#[cfg(unix)]
34pub use client::{read_auth_token, ConnectedClient, DaemonClient};
35pub use error::{DaemonError, DaemonResult};
36pub use protocol::{DaemonRequest, DaemonResponse};
37
38#[cfg(unix)]
39pub use server::{DaemonConfig, DaemonServer};
40
41// ═══════════════════════════════════════════════════════════════════════════
42// DAEMON PATHS
43// ═══════════════════════════════════════════════════════════════════════════
44
45use std::path::PathBuf;
46
47/// Environment variable to override the Nika home directory.
48pub const NIKA_HOME_ENV: &str = "NIKA_HOME";
49
50/// Returns the daemon directory (`~/.nika/daemon/`).
51pub fn daemon_dir() -> PathBuf {
52 nika_home().join("daemon")
53}
54
55/// Returns the daemon socket path (`~/.nika/daemon/nika.sock`).
56pub fn daemon_socket_path() -> PathBuf {
57 daemon_dir().join("nika.sock")
58}
59
60/// Returns the daemon PID file path (`~/.nika/daemon/nika.pid`).
61pub fn daemon_pid_path() -> PathBuf {
62 daemon_dir().join("nika.pid")
63}
64
65/// Returns the daemon log file path (`~/.nika/daemon/nika.log`).
66pub fn daemon_log_path() -> PathBuf {
67 daemon_dir().join("nika.log")
68}
69
70/// Returns the daemon auth token file path (`~/.nika/daemon/.token`).
71pub fn daemon_token_path() -> PathBuf {
72 daemon_dir().join(".token")
73}
74
75fn nika_home() -> PathBuf {
76 if let Ok(custom) = std::env::var(NIKA_HOME_ENV) {
77 return PathBuf::from(custom);
78 }
79 dirs::home_dir()
80 .map(|h| h.join(".nika"))
81 .unwrap_or_else(|| std::env::temp_dir().join(".nika"))
82}