Menu

Template System

Relevant source files

This document covers Lima's template system, including built-in templates, template variables, and custom template creation. Lima templates provide pre-configured VM configurations for common use cases and support dynamic value substitution through Go template variables.

For information about YAML processing and validation, see page 5.1. For details about configuration merging and defaults, see page 5.2.

Built-in Templates

Lima includes several built-in templates that provide pre-configured VM setups for common use cases. These templates can be referenced using the template:// scheme when creating instances.

Available Templates

Built-in templates are located in the templates/ directory and tested in .github/workflows/test.yml217-550 The following templates are available:

Template NameDescriptionPrimary Use Case
defaultBasic Ubuntu VM with minimal configurationGeneral-purpose Linux VM
dockerUbuntu VM with Docker pre-installedDocker container development
k8sKubernetes cluster with k3sKubernetes development and testing
alpineAlpine Linux (lightweight)Minimal footprint VM
debianDebian LinuxDebian-based development
fedoraFedora LinuxRed Hat ecosystem development
archlinuxArch LinuxArch-based development
opensuseopenSUSE LinuxSUSE-based development

Using Built-in Templates

Templates can be referenced using the template:// scheme:

The template mechanism is implemented in the YAML loading process, where base: template://name references are resolved to the embedded template files.

Template Inheritance with Base

Templates support inheritance through the base field, which can reference other templates:

Multiple levels of inheritance are supported, with later templates overriding earlier ones according to the configuration merging rules documented in page 5.2.

Sources: .github/workflows/test.yml217-550 pkg/limayaml/default.yaml1-340 hack/test-templates.sh1-700

Template Variable System

Lima supports Go template variables in configuration values, allowing dynamic substitution based on instance metadata and user-defined parameters. Variables use the syntax {{.VariableName}} and are processed during instance creation.

Template Variable Architecture

Sources: pkg/limayaml/defaults.go834-852 pkg/limayaml/defaults.go854-883

Guest Context Variables

Guest context variables are used in configuration fields that will be processed or used inside the VM. The executeGuestTemplate() function in pkg/limayaml/defaults.go834-852 provides these variables:

VariableTypeExample ValueDescription
{{.Name}}string"default"Instance name derived from instance directory
{{.Hostname}}string"lima-default"VM hostname generated by hostname.FromInstName()
{{.UID}}uint32501Guest user ID from the user configuration
{{.User}}string"alice"Guest username from the user configuration
{{.Home}}string"/home/alice.linux"Guest home directory path
{{.Param.KEY}}stringUser-definedCustom parameter value from param map

Host Context Variables

Host context variables are used in configuration fields that reference host system paths and resources. The executeHostTemplate() function in pkg/limayaml/defaults.go854-883 provides these variables:

VariableTypeExample ValueDescription
{{.Dir}}string"/Users/alice/.lima/default"Full path to instance directory
{{.Name}}string"default"Instance name
{{.UID}}string"501"Host user ID
{{.User}}string"alice"Host username
{{.Home}}string"/Users/alice"Host home directory
{{.Param.KEY}}stringUser-definedCustom parameter value from param map
{{.GlobalTempDir}}string"/tmp" (Unix)
C:\Temp (Windows)
System-wide temporary directory
{{.TempDir}}stringOS-specificUser-specific temporary directory
{{.Instance}}string"default"DEPRECATED: Use {{.Name}}
{{.LimaHome}}string"/Users/alice/.lima"DEPRECATED: Use {{.Dir}}

Sources: pkg/limayaml/defaults.go834-852 pkg/limayaml/defaults.go854-883

Configuration Fields Supporting Template Variables

Template variables can be used in specific configuration fields. The processing occurs during the FillDefault() function in pkg/limayaml/defaults.go138-794:

FieldContextExample Usage
user.homeGuest"/home/{{.User}}.linux"
provision[].scriptGuest"#!/bin/bash\necho {{.User}}"
provision[].contentGuest"User: {{.User}}\nUID: {{.UID}}"
provision[].expressionGuest".version = \"{{.Param.VERSION}}\""
provision[].ownerGuest"{{.User}}:{{.User}}"
provision[].pathGuest"/home/{{.User}}/config"
probes[].scriptGuest"#!/bin/sh\ntest -f {{.Home}}/ready"
mounts[].locationHost"{{.Home}}/projects"
mounts[].mountPointGuest"/mnt/{{.Param.PROJECT}}"
portForwards[].guestSocketGuest"/run/user/{{.UID}}/socket"
portForwards[].hostSocketHost"{{.Dir}}/sock/socket"
copyToHost[].guestFileGuest"{{.Home}}/output.log"
copyToHost[].hostFileHost"{{.Home}}/lima-logs/output.log"

Template processing happens after configuration merging but before validation, allowing inherited templates to use variables that will be resolved at instance creation time.

Sources: pkg/limayaml/defaults.go199-203 pkg/limayaml/defaults.go398-451 pkg/limayaml/defaults.go521-526 pkg/limayaml/defaults.go640-668 pkg/limayaml/defaults.go913-924 pkg/limayaml/defaults.go939-953

Using Template Variables

User-Defined Parameters

The param map allows defining custom variables that can be referenced throughout the configuration:

Parameter names must start with a letter and contain only letters, digits, and underscores, as validated in pkg/limayaml/validate.go396-408 Parameters are available as environment variables $PARAM_KEY in provision scripts and probes.

User Home Directory Customization

The user home directory supports template variables for dynamic paths:

This is processed in pkg/limayaml/defaults.go199-203 and allows customizing the guest home directory based on the username or other variables.

Provision Scripts with Variables

Provision scripts can use template variables for dynamic configuration:

Dynamic Mount Configuration

Mounts can use different template contexts for host and guest paths:

The location field uses host variables (like {{.Home}} referring to the host home directory), while mountPoint uses guest variables.

Port Forward Socket Paths

Socket forwarding paths can be customized using template variables:

This example forwards the Docker socket from the guest to a socket file in the instance directory on the host.

Sources: pkg/limayaml/defaults.go199-203 pkg/limayaml/defaults.go398-451 pkg/limayaml/defaults.go640-668 pkg/limayaml/defaults.go913-924 pkg/limayaml/validate.go396-408 hack/test-templates/test-misc.yaml16-113

Creating Custom Templates

Template File Structure

Custom templates are YAML files that follow the Lima configuration schema documented in pkg/limayaml/default.yaml Templates can inherit from other templates using the base field:

Template Inheritance Chain

The configuration merging follows the priority order documented in page 5.2. Template variables are resolved after all configurations are merged but before validation.

Sources: pkg/limayaml/load.go23-75 pkg/limayaml/defaults.go138-794

Example: Custom Development Template

This example creates a template for web development with Node.js and Docker:

Testing Custom Templates

Custom templates can be validated before use:

The validation process is implemented in pkg/limayaml/validate.go34-421 and checks all configuration fields including template variable syntax.

Sources: pkg/limayaml/default.yaml1-340 pkg/limayaml/validate.go34-421 hack/test-templates/test-misc.yaml1-124

Template Variable Error Handling

When template variable processing fails, Lima logs a warning but continues with the original untemplated value. This graceful degradation allows instances to start even with malformed templates.

Error Handling Pattern

Template variable processing in pkg/limayaml/defaults.go199-203 follows this pattern:

Common error scenarios:

  • Invalid template syntax (e.g., unclosed braces)
  • Reference to undefined variables
  • Syntax errors in template expressions

The error is logged but doesn't prevent instance creation, with the original value used unchanged.

Sources: pkg/limayaml/defaults.go199-203 pkg/limayaml/defaults.go398-451 pkg/limayaml/defaults.go640-668

List Entry Combination Logic

Lima implements sophisticated list combination logic for additionalDisks, mounts, and networks that allows entries to be merged based on shared keys and wildcard patterns.

List Combination Implementation

The combineListEntries() function in pkg/limatmpl/embed.go331-548 implements sophisticated merging logic for three list types:

Wildcard Merging Example

For additionalDisks with wildcard entries:

Sources: pkg/limatmpl/embed.go331-548 pkg/limatmpl/embed_test.go252-278