bcicen/ctop · error

$HOME not set

Error message

$HOME not set

What it means

getConfigPath derives the config file location from the environment. If the HOME environment variable is not set, it cannot determine where to read/write the config and returns the '$HOME not set' error. Read and Write both depend on it.

Source

Thrown at config/file.go:119

	file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		return path, fmt.Errorf("failed to open config for writing: %s", err)
	}

	writer := toml.NewEncoder(file)
	err = writer.Encode(exportConfig())
	if err != nil {
		return path, fmt.Errorf("failed to write config: %s", err)
	}

	return path, nil
}

// determine config path from environment
func getConfigPath() (path string, err error) {
	homeDir, ok := os.LookupEnv("HOME")
	if !ok {
		return path, fmt.Errorf("$HOME not set")
	}

	// use xdg config home if possible
	if xdgSupport() {
		xdgHome, ok := os.LookupEnv("XDG_CONFIG_HOME")
		if !ok {
			xdgHome = fmt.Sprintf("%s/.config", homeDir)
		}
		path = fmt.Sprintf("%s/ctop/config", xdgHome)
	} else {
		path = fmt.Sprintf("%s/.ctop", homeDir)
	}

	return path, nil
}

// test for environemnt supporting XDG spec
func xdgSupport() bool {

View on GitHub (pinned to 59f00dd6aa)

Solutions

  1. Set the HOME environment variable for the process (e.g. Environment=HOME=/home/user in systemd, ENV HOME in Dockerfile)
  2. Run the process as a user whose HOME is defined
  3. Check the runtime environment (os.Getenv) before launching

Example fix

// before
# docker run app  # no HOME
// after
# docker run -e HOME=/home/app app
Defensive patterns

Strategy: validation

Validate before calling

if os.Getenv("HOME") == "" {
    os.Setenv("HOME", "/tmp") // or refuse to start with a clear message
}

Type guard

func homeSet() bool { _, ok := os.LookupEnv("HOME"); return ok }

Try / catch

_, err := config.Read()
if err != nil && err.Error() == "$HOME not set" {
    // set HOME or use an explicit config path
}

Prevention

When it happens

Trigger: Calling config.Read() or config.Write() in a process whose environment lacks HOME — e.g. a daemon/systemd service, cron job, or container run without HOME.

Common situations: Systemd unit with a minimal Environment; Docker containers running as non-root without ENV HOME; exec environments stripped via env -i.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of bcicen/ctop@59f00dd6aa (2026-09-02). Data as JSON: /api/errors/fc2060676c80478e. Report an issue: GitHub.