Documentation Index
Fetch the complete documentation index at: https://mintlify.com/twpayne/chezmoi/llms.txt
Use this file to discover all available pages before exploring further.
Template Variables
chezmoi provides the following automatically-populated variables accessible in templates via the .chezmoi namespace.
| Variable | Type | Description | Example |
|---|
.chezmoi.arch | string | Architecture as returned by runtime.GOARCH | amd64, arm64, arm |
.chezmoi.os | string | Operating system as returned by runtime.GOOS | darwin, linux, windows |
.chezmoi.osRelease | object | Contents of /etc/os-release (Linux only) | See below |
.chezmoi.kernel | object | Information from /proc/sys/kernel (Linux only) | See below |
.chezmoi.windowsVersion | object | Windows version information (Windows only) | See below |
Examples
{{ if eq .chezmoi.os "darwin" }}
# macOS-specific configuration
{{ else if eq .chezmoi.os "linux" }}
# Linux-specific configuration
{{ end }}
{{ if eq .chezmoi.arch "amd64" }}
# 64-bit configuration
{{ end }}
| Variable | Type | Description | Example |
|---|
.chezmoi.hostname | string | Hostname up to the first . | workstation |
.chezmoi.fqdnHostname | string | Fully-qualified domain name hostname | workstation.example.com |
Examples
{{ if eq .chezmoi.hostname "workstation" }}
# Work machine configuration
{{ else if eq .chezmoi.hostname "laptop" }}
# Personal laptop configuration
{{ end }}
| Variable | Type | Description | Example |
|---|
.chezmoi.username | string | Username of the user running chezmoi | john |
.chezmoi.uid | string | User ID | 1000 |
.chezmoi.gid | string | Primary group ID | 1000 |
.chezmoi.group | string | Group name of the user | users |
Examples
[user]
name = {{ .chezmoi.username }}
uid = {{ .chezmoi.uid }}
Directory Paths
| Variable | Type | Description | Example |
|---|
.chezmoi.homeDir | string | Home directory | /home/john |
.chezmoi.sourceDir | string | Source directory | /home/john/.local/share/chezmoi |
.chezmoi.destDir | string | Destination directory | /home/john |
.chezmoi.cacheDir | string | Cache directory | /home/john/.cache/chezmoi |
.chezmoi.workingTree | string | Git working tree | /home/john/.local/share/chezmoi |
Examples
# Include a file from source directory
{{ include (joinPath .chezmoi.sourceDir "scripts" "common.sh") }}
# Check if file exists in home directory
{{ if stat (joinPath .chezmoi.homeDir ".ssh" "id_rsa") }}
SSH key exists
{{ end }}
| Variable | Type | Description | Example |
|---|
.chezmoi.sourceFile | string | Path of template relative to source directory | dot_bashrc.tmpl |
.chezmoi.targetFile | string | Absolute path of the target file | /home/john/.bashrc |
Examples
# Managed by chezmoi
# Source: {{ .chezmoi.sourceFile }}
# Target: {{ .chezmoi.targetFile }}
| Variable | Type | Description | Example |
|---|
.chezmoi.executable | string | Path to chezmoi executable | /usr/local/bin/chezmoi |
.chezmoi.args | []string | Arguments passed to chezmoi | ["chezmoi", "apply"] |
.chezmoi.version.version | string | chezmoi version | 2.40.0 |
.chezmoi.version.commit | string | Git commit of build | abc123... |
.chezmoi.version.date | string | Build timestamp | 2024-01-15T10:30:00Z |
.chezmoi.version.builtBy | string | Builder name | goreleaser |
Examples
# Generated by chezmoi {{ .chezmoi.version.version }}
{{ if .chezmoi.executable }}
# To update: {{ .chezmoi.executable }} update
{{ end }}
Configuration
| Variable | Type | Description |
|---|
.chezmoi.config | object | Configuration file contents |
.chezmoi.configFile | string | Path to configuration file |
Examples
{{ if .chezmoi.config.data.email }}
email = {{ .chezmoi.config.data.email }}
{{ end }}
Path Separators
| Variable | Type | Description | Example |
|---|
.chezmoi.pathSeparator | string | OS path separator | / (Unix), \ (Windows) |
.chezmoi.pathListSeparator | string | OS path list separator | : (Unix), ; (Windows) |
Examples
export PATH="$HOME{{ .chezmoi.pathSeparator }}bin{{ .chezmoi.pathListSeparator }}$PATH"
OS-Specific Variables
.chezmoi.osRelease (Linux)
Contains information from /etc/os-release:
{{ .chezmoi.osRelease.id }} # "ubuntu", "arch", etc.
{{ .chezmoi.osRelease.versionID }} # "22.04", etc.
{{ .chezmoi.osRelease.name }} # "Ubuntu"
{{ .chezmoi.osRelease.prettyName }} # "Ubuntu 22.04 LTS"
Example
{{ if eq .chezmoi.osRelease.id "ubuntu" }}
# Ubuntu-specific configuration
{{ else if eq .chezmoi.osRelease.id "arch" }}
# Arch-specific configuration
{{ end }}
.chezmoi.kernel (Linux)
Contains information from /proc/sys/kernel:
{{ .chezmoi.kernel.osrelease }} # Kernel version
{{ .chezmoi.kernel.ostype }} # "Linux"
Useful for detecting WSL:
{{ if .chezmoi.kernel.osrelease | lower | contains "microsoft" }}
# Running in WSL
{{ end }}
.chezmoi.windowsVersion (Windows)
Contains Windows version information from the registry:
| Key | Type | Description |
|---|
currentBuild | string | Build number |
currentMajorVersionNumber | integer | Major version |
currentMinorVersionNumber | integer | Minor version |
currentVersion | string | Version string |
displayVersion | string | Display version |
editionID | string | Edition (e.g., “Professional”) |
productName | string | Product name |
Example
{{ if ge .chezmoi.windowsVersion.currentBuild "22000" }}
# Windows 11 or later
{{ end }}
Custom Data Variables
Additional variables can be defined in the config file’s data section or in .chezmoidata.$FORMAT files:
~/.config/chezmoi/chezmoi.toml
[data]
email = "user@example.com"
name = "John Doe"
work = true
~/.local/share/chezmoi/.chezmoidata.yaml
packages:
- vim
- git
editor: nvim
Access in templates:
{{ .email }}
{{ .name }}
{{ if .work }}
# Work configuration
{{ end }}
{{ range .packages }}
install {{ . }}
{{ end }}
Testing Variables
To see all available template data:
# View all chezmoi variables
chezmoi data
# View specific value
chezmoi execute-template '{{ .chezmoi.os }}'
# View custom data
chezmoi execute-template '{{ .email }}'
Practical Examples
Multi-OS Configuration
# Common configuration
export EDITOR=vim
{{ if eq .chezmoi.os "darwin" }}
# macOS
export PATH="/usr/local/bin:$PATH"
alias ls="ls -G"
{{ else if eq .chezmoi.os "linux" }}
# Linux
export PATH="/usr/bin:$PATH"
alias ls="ls --color=auto"
{{ end }}
Machine-Specific Configuration
[user]
name = {{ .name }}
email = {{ if eq .chezmoi.hostname "work-laptop" }}{{ .work_email }}{{ else }}{{ .personal_email }}{{ end }}
{{ if .work }}
[url "git@github.com:company/"]
insteadOf = https://github.com/company/
{{ end }}
Distribution-Specific Configuration
run_once_install-packages.sh.tmpl
#!/bin/bash
{{ if eq .chezmoi.osRelease.id "ubuntu" }}
apt-get update
apt-get install -y {{ range .packages }}{{ . }} {{ end }}
{{ else if eq .chezmoi.osRelease.id "arch" }}
pacman -Syu --noconfirm {{ range .packages }}{{ . }} {{ end }}
{{ else if eq .chezmoi.osRelease.id "fedora" }}
dnf install -y {{ range .packages }}{{ . }} {{ end }}
{{ end }}
Related Pages