Skip to content

Write a template

A template is the description of a system. You write it one time. It lists the repositories, the services that run from them, and the resources that the services use. Then lo new makes any number of workspaces from it.

This guide shows how to write a template, and how localspace finds out how each service runs. For each field, see the template reference. For complete templates that you can copy, see the template examples.

A template is a directory that holds one file with the name localspace.yaml. The name of the directory is the name of the template.

~/.localspace/templates/
shop/
localspace.yaml
seeds/
orders.sql.gz

The templates: machine setting gives the list of template directories. The default is the templates directory next to config.yaml, that is ~/.localspace/templates. localspace refuses a template name that it finds in more than one directory.

~/.localspace/config.yaml
templates:
- ~/.localspace/templates
- ~/work/templates

Run lo template ls to list the templates in all the directories. For each template, it shows the number of services and resources, the number of problems, and the directory. If it finds no template, it prints the directories that it searched.

  1. Make the directory and the file:

    Terminal window
    mkdir -p ~/.localspace/templates/shop
  2. List the repositories in repos:. Each key is a name that the services use. Each value is the path of a git repository on this machine.

    localspace.yaml
    defaults:
    ref: main
    repos:
    storefront: ~/code/storefront
    orders-api: ~/code/orders-api
  3. Set defaults.ref. It is the branch that a service takes when nothing else selects one. If the template has no defaults.ref and you select no branch, lo new fails. A repo that uses a different branch can set its ref:

    localspace.yaml
    repos:
    storefront:
    path: ~/code/storefront
    ref: release/next
  4. Add the services. Each service names its repo. Set port to the port that the service listens on. Set expose: true if the browser must reach the service through a public URL.

    localspace.yaml
    services:
    storefront:
    repo: storefront
    port: 3000
    expose: true
    env:
    ORDERS_URL: ${orders.public_url}
    orders:
    repo: orders-api
    port: 8080
    expose: true
    setup: pnpm prisma migrate deploy
    env:
    DATABASE_URL: ${orders-db.url}
    REDIS_URL: ${cache.url}
  5. Add the resources. Each resource has a kind. A service refers to a resource in its env with a reference such as ${orders-db.url}.

    localspace.yaml
    resources:
    orders-db:
    kind: postgres
    version: 16
    seed: seeds/orders.sql.gz
    cache:
    kind: redis
  6. Validate the template:

    Terminal window
    lo template validate shop

    The command starts nothing. It prints each problem as file:line:column: message, in the form that editors use to jump to the line. If the template is correct, it tells you that the template is valid.

  7. Make a workspace from the template:

    Terminal window
    lo new shop-a --template shop
    lo up shop-a
Field Use
repo The key in repos: that the code comes from.
dir A directory in the repository, for a monorepo. It must stay in the repository.
port The port that the service listens on.
expose Gives the service a public URL. It is only possible with a port.
env The variables of the service. Values can hold references.
setup A command that runs before each start of the service, after the install that localspace found.
install The install step of a process, in place of the install that localspace found. false removes the install step.
command The command that starts the service. Use it for a service that runs as a process.
run container or process. See How a service runs.
image A prebuilt image, as an alternative to code from a repository.
ready How localspace tests that the service is ready. The default is the port. A path that starts with / is an HTTP request. Other text is a shell command.
depends_on The services and resources that the service must have to start. See Start order.

localspace sets PORT in the environment of each service that has a port. A process listens on a host port that localspace selects, and gets that number in PORT.

A service with an image has no source. It cannot have repo, dir, setup, install, rebuild_on or on_switch, and it always runs as a container.

For the other fields, such as mounts, shareable, on_switch and rebuild_on, see the template reference.

A value in env can hold a reference in the form ${...}. localspace replaces it for each workspace and for each consumer. A container and a host process get the address that works for each of them.

Reference Value
${orders.internal_url} The address that a service in the workspace uses to reach orders.
${orders.public_url} The public URL of the service orders. The service must have expose: true.
${orders-db.url} An attribute of a resource.
${secret:stripe/key} A secret. See Store secrets.
${env:HOME} A variable of the environment of lo.
${workspace.name} The name of the workspace.

The template checker tests each reference. A reference to a service or resource that is not in the template, or to an attribute that it does not have, is a problem. See the full list in the references reference.

lo up starts a service after the services that it references. Some references are only addresses, for example a webhook URL. To tell localspace which services a service must have, list them in depends_on:

localspace.yaml
services:
orders:
repo: orders-api
depends_on: [orders-db, pricing]
env:
PRICING_URL: ${pricing.internal_url}
ADMIN_URL: ${admin.public_url}

A service with depends_on starts after the items in the list, and its other references do not change the order. lo up --service orders also starts pricing and orders-db. If pricing fails, orders does not start. See dependencies.

A service runs as a container or as a host process. You do not have to select one. localspace looks at the source of the service:

  • If you set run: process, the service runs as a process.
  • If you set run: container, the source must have a Dockerfile. If it does not, lo new fails.
  • If you set no run and the source has a Dockerfile in its directory, the service runs as a container.
  • If you set no run and there is no Dockerfile, the service runs as a process.

The container engine runs containers and the process engine runs processes. See Engines.

localspace selects how a service runs when it makes the workspace. It reads the source that the service runs from. It reads the source again when lo set changes that source.

A container service can have the options of a compose file: platform, entrypoint, args, working_dir, publish, volumes and cap_add. Use oneshot: true for a job that runs to completion, such as a migration. The services that list it in depends_on start after it stops with the exit code 0.

services:
migrate:
image: acme/api:latest
oneshot: true
args: [npm, run, migrate]
api:
image: acme/api:latest
port: 8080
publish: [9090]
depends_on: [migrate]
volumes:
- { source: ./config, target: /app/config, read_only: true }

api publishes the port 9090 on a host port that localspace selects. A process reads that port with ${api.ports.9090}. See container options.

lo up and lo restart read run, command, install and setup from the template again each time they start a service that does not run. When you change one of these fields, the next start of the service uses the new value. The workspace keeps its worktrees and its data, so lo rm and lo new are not necessary.

A service that runs keeps its command until it restarts. lo up then shows a warning. To apply the change, run lo restart <workspace> --service <service>.

A process must have a command. If the template gives no command, localspace looks in the directory of the service and finds how it runs. This is discovery.

Discovery reads these files, in this order. The first match decides.

  1. composer.json with laravel/framework: Laravel.
  2. package.json: Node.
  3. go.mod: Go.
  4. pyproject.toml, requirements.txt or manage.py: Python.
  5. Gemfile: Ruby.
  6. composer.json: PHP.

If no file matches and the source has a Dockerfile, discovery reads the base images of the Dockerfile to find the language.

For Node, discovery selects the package manager from packageManager in package.json, or from the lockfile. It finds the framework from the dependencies of the package:

Framework Dependency
NestJS @nestjs/core
Strapi @strapi/strapi
Next.js next
Nuxt nuxt
SvelteKit @sveltejs/kit
Astro astro
Vite vite
Express express
Fastify fastify

For the other languages, discovery finds these frameworks: Django, Flask and FastAPI for Python, Rails for Ruby, and Laravel for PHP. A Go service with a main.go, or with one main package in cmd/, runs with go run.

Discovery finds two commands. The install step installs the dependencies, for example pnpm install --frozen-lockfile, composer install or bundle install. The start command starts the service with the port in $PORT.

The setup of the template runs after the install step, before each start. Use it for a step such as a migration. Do not write the install again.

localspace.yaml
services:
orders:
repo: orders-api
port: 8080
setup: pnpm prisma migrate deploy

If the install step of discovery does not work for a repo, set install. A command replaces the install step. install: false removes it, and setup does all the work, for example an install at the root of a monorepo.

localspace.yaml
services:
web:
repo: shop
dir: apps/web
install: false
setup: cd ../.. && pnpm install --frozen-lockfile

A value in the template always wins. If you set command, localspace does not use the command that discovery found. If discovery finds no command to start a process, lo new fails and tells you to set command.

lo init writes a first draft of a template. It is experimental. Review the result before you use it.

  1. Go to a directory that holds your git repositories, or that is one of them.

  2. Run the command:

    Terminal window
    lo init --name shop

    It reads compose files, .env files, the workspace files of a monorepo, and the framework of each service. It writes localspace.yaml in the first template directory.

  3. Open the file. Each guess has a # lo init: comment. Correct the guesses and delete the comments.

  4. Run lo template validate shop.

If you give no --name, the template takes the name of the current directory. Use --dry-run to print the draft and write no file. Use --force to replace a template that has the same name.