spacedriveapp/spacedrive · error · anyhow::Error

Failed to enable systemd service: {}

Error message

Failed to enable systemd service: {}

What it means

During Linux install, after writing the unit and running `systemctl --user daemon-reload`, the CLI runs `systemctl --user enable spacedrive-daemon.service` and requires success. A non-zero exit means the systemd user instance could not process the request, and its stderr is included in the message. Note the daemon-reload result is intentionally ignored (`.output()` without `?`), so this enable step is the first hard checkpoint.

Source

Thrown at apps/cli/src/domains/daemon/mod.rs:333

	println!("Created systemd service: {}", service_path.display());

	// Reload systemd daemon
	let _ = std::process::Command::new("systemctl")
		.arg("--user")
		.arg("daemon-reload")
		.output();

	// Enable the service
	let output = std::process::Command::new("systemctl")
		.arg("--user")
		.arg("enable")
		.arg(&service_name)
		.output()?;

	if !output.status.success() {
		let stderr = String::from_utf8_lossy(&output.stderr);
		return Err(anyhow::anyhow!(
			"Failed to enable systemd service: {}",
			stderr
		));
	}

	// Start the service
	let output = std::process::Command::new("systemctl")
		.arg("--user")
		.arg("start")
		.arg(&service_name)
		.output()?;

	if !output.status.success() {
		let stderr = String::from_utf8_lossy(&output.stderr);
		return Err(anyhow::anyhow!(
			"Failed to start systemd service: {}",
			stderr
		));

View on GitHub (pinned to 6dfeccf211)

Solutions

  1. Verify the user manager works in that exact shell: `systemctl --user status`; if it errors about D-Bus or XDG_RUNTIME_DIR, fix that first.
  2. Over SSH, set `export XDG_RUNTIME_DIR=/run/user/$(id -u)` and `export DBUS_SESSION_BUS_ADDRESS=unix:path=$XDG_RUNTIME_DIR/bus`, or enable lingering so the user manager persists: `sudo loginctl enable-linger $USER`.
  3. On WSL, add `[boot]\nsystemd=true` to /etc/wsl.conf and restart the distro (`wsl --shutdown`).
  4. Read the stderr embedded in the message; for invalid units inspect `systemctl --user cat spacedrive-daemon`.

Example fix

# before (headless SSH, no user bus)
sd-cli daemon install  # -> Failed to enable systemd service: Failed to connect to bus: ...

# after
sudo loginctl enable-linger $USER
export XDG_RUNTIME_DIR=/run/user/$(id -u)
sd-cli daemon install  # -> Daemon installed and started successfully!
Defensive patterns

Strategy: validation

Validate before calling

# preflight: a functional systemd user manager is required
systemctl --user is-system-running >/dev/null 2>&1 || { echo 'no user systemd session' >&2; exit 1; }
sd-cli daemon install

Try / catch

match install_autostart().await {
    Err(e) if e.to_string().contains("Failed to enable systemd service") => {
        // unit file is already written; retry enable manually after fixing the bus
        run!("systemctl --user enable spacedrive-daemon.service")?;
        run!("systemctl --user start spacedrive-daemon.service")?;
    }
    other => other?,
}

Prevention

When it happens

Trigger: `systemctl --user` over SSH when there is no user bus (XDG_RUNTIME_DIR unset, no lingering enabled); running inside a container or WSL distro where systemd is not PID 1; unit name resolution failing because the unit file was written under a different name (instance variant `spacedrive-daemon@<inst>.service`).

Common situations: Headless servers accessed via SSH; Docker/LXC containers without systemd; WSL before enabling `[boot] systemd=true` in /etc/wsl.conf; fresh boot where the user manager is not running.

Related errors


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