twpayne/chezmoi · error

%s: cannot add chezmoi's config file to chezmoi, use a confi

Error message

%s: cannot add chezmoi's config file to chezmoi, use a config file template instead

What it means

chezmoi refuses to add its own config file (the file passed via --config or the default config location) into the source state via 'chezmoi add'. Managing the config file as a regular source entry would create a recursive dependency; the documented alternative is a config file template so machine-specific values are rendered at runtime.

Source

Thrown at internal/chezmoi/sourcestate.go:423

		targetRelPath := destAbsPath.MustTrimDirPrefix(s.destDirAbsPath)
		if s.Ignore(targetRelPath) {
			if options.OnIgnoreFunc != nil {
				options.OnIgnoreFunc(targetRelPath)
			}
			continue
		}

		destAbsPaths[n] = destAbsPath
		n++
	}
	destAbsPaths = destAbsPaths[:n]

	// Check for protected paths.
	for _, destAbsPath := range destAbsPaths {
		if destAbsPath == options.ConfigFileAbsPath {
			format := "%s: cannot add chezmoi's config file to chezmoi, use a config file template instead"
			return fmt.Errorf(format, destAbsPath)
		}
	}
	for _, destAbsPath := range destAbsPaths {
		for _, protectedAbsPath := range options.ProtectedAbsPaths {
			if protectedAbsPath.IsEmpty() {
				continue
			}
			if destAbsPath.HasDirPrefix(protectedAbsPath) {
				format := "%s: cannot add chezmoi file to chezmoi (%s is protected)"
				return fmt.Errorf(format, destAbsPath, protectedAbsPath)
			}
		}
	}

	type sourceUpdate struct {
		destAbsPath    AbsPath
		entryState     *EntryState
		sourceRelPaths []SourceRelPath

View on GitHub (pinned to f901167e46)

Solutions

  1. Create a config file template instead: run 'chezmoi add --template ~/.config/chezmoi/chezmoi.toml' is also blocked, so manually create the file under ~/.local/share/chezmoi/ with a .tmpl suffix containing the template form of the config.
  2. Move the config content into e.g. ~/.local/share/chezmoi/dot_config/chezmoi/chezmoi.toml.tmpl and let chezmoi apply it as a managed file.
  3. If you only need the config on the current machine, exclude it from add operations (avoid passing it to 'chezmoi add').

Example fix

// before (fails)
$ chezmoi add ~/.config/chezmoi/chezmoi.toml
// after
$ chezmoi cd
$ mkdir -p dot_config/chezmoi
$ cat > dot_config/chezmoi/chezmoi.toml.tmpl <<EOF
[git]
autoCommit = {{ .chezmoi.hostname == "work" }}
EOF
Defensive patterns

Strategy: validation

Validate before calling

#!/bin/sh
cfg=$(chezmoi source-path 2>/dev/null)
if [ "$1" = "$HOME/.config/chezmoi/chezmoi.toml" ]; then
  echo "use a config file template instead of chezmoi add" >&2; exit 1
fi

Prevention

When it happens

Trigger: Running 'chezmoi add ~/.config/chezmoi/chezmoi.toml' (or equivalent path) where the destination path exactly equals options.ConfigFileAbsPath during source state update in addEntries/add path handling.

Common situations: Users try to version-control their chezmoi config by adding it like any other dotfile; scripts that blanket-add ~/.config; users switching config locations and adding the old file.

Related errors


AI-assisted analysis of twpayne/chezmoi@f901167e46 (2026-09-01). Data as JSON: /api/errors/b133b25a4b06dd5b. Report an issue: GitHub.