Skip to main content

Template Variables

chezmoi provides the following automatically-populated variables accessible in templates via the .chezmoi namespace.

System Information

VariableTypeDescriptionExample
.chezmoi.archstringArchitecture as returned by runtime.GOARCHamd64, arm64, arm
.chezmoi.osstringOperating system as returned by runtime.GOOSdarwin, linux, windows
.chezmoi.osReleaseobjectContents of /etc/os-release (Linux only)See below
.chezmoi.kernelobjectInformation from /proc/sys/kernel (Linux only)See below
.chezmoi.windowsVersionobjectWindows 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 }}

Host Information

VariableTypeDescriptionExample
.chezmoi.hostnamestringHostname up to the first .workstation
.chezmoi.fqdnHostnamestringFully-qualified domain name hostnameworkstation.example.com

Examples

{{ if eq .chezmoi.hostname "workstation" }}
# Work machine configuration
{{ else if eq .chezmoi.hostname "laptop" }}
# Personal laptop configuration
{{ end }}

User Information

VariableTypeDescriptionExample
.chezmoi.usernamestringUsername of the user running chezmoijohn
.chezmoi.uidstringUser ID1000
.chezmoi.gidstringPrimary group ID1000
.chezmoi.groupstringGroup name of the userusers

Examples

[user]
    name = {{ .chezmoi.username }}
    uid = {{ .chezmoi.uid }}

Directory Paths

VariableTypeDescriptionExample
.chezmoi.homeDirstringHome directory/home/john
.chezmoi.sourceDirstringSource directory/home/john/.local/share/chezmoi
.chezmoi.destDirstringDestination directory/home/john
.chezmoi.cacheDirstringCache directory/home/john/.cache/chezmoi
.chezmoi.workingTreestringGit 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 }}

Template File Information

VariableTypeDescriptionExample
.chezmoi.sourceFilestringPath of template relative to source directorydot_bashrc.tmpl
.chezmoi.targetFilestringAbsolute path of the target file/home/john/.bashrc

Examples

# Managed by chezmoi
# Source: {{ .chezmoi.sourceFile }}
# Target: {{ .chezmoi.targetFile }}

chezmoi Information

VariableTypeDescriptionExample
.chezmoi.executablestringPath to chezmoi executable/usr/local/bin/chezmoi
.chezmoi.args[]stringArguments passed to chezmoi["chezmoi", "apply"]
.chezmoi.version.versionstringchezmoi version2.40.0
.chezmoi.version.commitstringGit commit of buildabc123...
.chezmoi.version.datestringBuild timestamp2024-01-15T10:30:00Z
.chezmoi.version.builtBystringBuilder namegoreleaser

Examples

# Generated by chezmoi {{ .chezmoi.version.version }}

{{ if .chezmoi.executable }}
# To update: {{ .chezmoi.executable }} update
{{ end }}

Configuration

VariableTypeDescription
.chezmoi.configobjectConfiguration file contents
.chezmoi.configFilestringPath to configuration file

Examples

{{ if .chezmoi.config.data.email }}
email = {{ .chezmoi.config.data.email }}
{{ end }}

Path Separators

VariableTypeDescriptionExample
.chezmoi.pathSeparatorstringOS path separator/ (Unix), \ (Windows)
.chezmoi.pathListSeparatorstringOS 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:
KeyTypeDescription
currentBuildstringBuild number
currentMajorVersionNumberintegerMajor version
currentMinorVersionNumberintegerMinor version
currentVersionstringVersion string
displayVersionstringDisplay version
editionIDstringEdition (e.g., “Professional”)
productNamestringProduct 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 = "[email protected]"
    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

dot_bashrc.tmpl
# 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

dot_gitconfig.tmpl
[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 }}

Build docs developers (and LLMs) love