Skip to main content

kanbrain_core/
platform.rs

1//! Operating-system specifics.
2
3use anyhow::{Result, bail};
4
5/// kanbrain is a per-user tool: as root it would write root-owned files into the user's data folder,
6/// or use root's instead. Containers that only have root can opt in with `KANBRAIN_ALLOW_ROOT=1`.
7pub fn ensure_not_root() -> Result<()> {
8    if is_root() && std::env::var_os("KANBRAIN_ALLOW_ROOT").is_none_or(|v| v != "1") {
9        bail!(
10            "kanbrain is a per-user app and doesn't run as root (sudo).\n\
11             Run it as your normal user. Only if root is your only user (e.g. a container), set KANBRAIN_ALLOW_ROOT=1."
12        );
13    }
14    Ok(())
15}
16
17/// Whether the effective user is root.
18#[cfg(unix)]
19fn is_root() -> bool {
20    rustix::process::geteuid().is_root()
21}
22
23/// Windows has no root; an elevated shell still runs as the user's own account.
24#[cfg(not(unix))]
25fn is_root() -> bool {
26    false
27}