spacedriveapp/spacedrive · critical · anyhow::Error

Could not determine home directory

Error message

Could not determine home directory

What it means

Thrown by default_data_dir() on desktop platforms (not iOS/Android) when dirs::home_dir() returns None. The function then cannot build the default '~/.spacedrive' data directory, and every config load, daemon startup, or model/speech/indexing op that calls default_data_dir()? fails with this error. dirs::home_dir() returns None when the HOME environment variable is unset or empty on Linux/macOS, or USERPROFILE is missing on Windows.

Source

Thrown at core/src/config/mod.rs:22

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;

pub mod app_config;
pub mod migration;

pub use app_config::{
	AppConfig, JobLoggingConfig, LogStreamConfig, LoggingConfig, ProxyPairingConfig, ServiceConfig,
	SpacebotConfig,
};
pub use migration::Migrate;

/// Default data directory: `~/.spacedrive` on desktop, platform data dir on mobile.
pub fn default_data_dir() -> Result<PathBuf> {
	#[cfg(not(any(target_os = "ios", target_os = "android")))]
	let dir = dirs::home_dir()
		.ok_or_else(|| anyhow!("Could not determine home directory"))?
		.join(".spacedrive");

	#[cfg(target_os = "ios")]
	let dir = dirs::data_dir()
		.ok_or_else(|| anyhow!("Could not determine data directory"))?
		.join("spacedrive");

	#[cfg(target_os = "android")]
	let dir = dirs::data_dir()
		.ok_or_else(|| anyhow!("Could not determine data directory"))?
		.join("spacedrive");

	// Create directory if it doesn't exist
	fs::create_dir_all(&dir)?;

	Ok(dir)
}

View on GitHub (pinned to 6dfeccf211)

Solutions

  1. Run the daemon with an explicit directory: `cargo run --bin sd-daemon -- --data-dir /var/lib/spacedrive` (see Args in core/src/bin/daemon.rs:27).
  2. If HOME is expected to exist, set it: `export HOME=/home/user` (Linux/macOS) or ensure USERPROFILE is set on Windows, then restart the daemon.
  3. For systemd services add `Environment="HOME=/var/lib/spacedrive"` (or WorkingDirectory plus EnvironmentFile) to the unit file.
  4. For containers, add `ENV HOME=/root` or `USER` with a proper home directory in the image.

Example fix

# before (fails: HOME unset)
systemctl start sd-daemon

# after (unit file)
[Service]
Environment="HOME=/var/lib/spacedrive"
ExecStart=/usr/bin/sd-daemon

# or bypass the default entirely
sd-daemon --data-dir /var/lib/spacedrive
Defensive patterns

Strategy: validation

Validate before calling

// Rust: verify a home directory is resolvable before starting the daemon
fn ensure_home_available() -> anyhow::Result<()> {
    #[cfg(not(any(target_os = "ios", target_os = "android")))]
    if dirs::home_dir().is_none() {
        anyhow::bail!("HOME is not set; run with --data-dir or export HOME");
    }
    Ok(())
}

Try / catch

// treat as fatal startup misconfiguration; report the missing env, do not invent a fallback dir
match sd_core::config::default_data_dir() {
    Ok(dir) => { /* proceed */ },
    Err(e) if e.to_string().contains("home directory") => {
        eprintln!("set $HOME or pass --data-dir");
        std::process::exit(1);
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Running sd-daemon or sd-cli without a --data-dir override while HOME is unset or empty: systemd/cron/launchd services with a scrubbed environment, `env -i cargo run`, minimal Docker containers that lack the HOME env var, or CI runners.

Common situations: A systemd unit without `Environment="HOME=..."`; a Dockerfile that switches USER without setting HOME; CI jobs; processes spawned from daemons that clear the environment; running as a user whose passwd entry is broken.

Related errors


AI-assisted analysis of spacedriveapp/spacedrive@6dfeccf211 (2026-08-16). Data as JSON: /api/errors/deff5a276161bd06. Report an issue: GitHub.