Docs

A recipe is a YAML file declaring the state you want on a machine. strapped compile turns it into one deterministic POSIX /bin/sh script that you can review, copy to the target, and run without installing strapped there.

The recipe file

The optional strapped block sets recipe metadata and compile-time guards. Every other top-level key is a primitive, handlers, or the name of a local strap. Steps run in file order, and unknown keys fail compilation.

strapped:
  name: homelab          # label in --help; defaults to filename stem
  target: linux          # linux, darwin, or any (default any)
  straps_dir: straps     # local strap templates, relative to the recipe
  env_required:          # variables the script refuses to run without
    - REGISTRY_TOKEN

packages:
  - curl
KeyMeaning
nameLabel shown in the script's --help usage line. Defaults to the recipe filename without its extension.
targetlinux or darwin emits a uname -s guard that exits 1 on any other OS. any (default) adds no guard.
straps_dirDirectory of local strap templates, relative to the recipe file. Defaults to straps.
env_requiredEnvironment variables checked before step one; exits 1 if any is unset or empty. See Secret guardrail.

Every entry accepts an optional notify field. Default step names for --only are package:<name>, service:<name>, dir:<path>, file:<path>, line:<path>, symlink:<dest>, git:<dest>, or the strap's name. steps requires name; dirs, files, lines, symlinks, git, and local strap invocations accept an optional name to override the default, and duplicate names get #2, #3, and so on. Paths starting with ~/ expand to $HOME on the target.

Primitives

Each entry compiles to a step with a check and an apply. The script runs the check first and applies only when it fails.

packages

Checks and installs packages with whichever of apt-get, apk, dnf, pacman, or brew the host has, failing if none is present (it never falls back to command -v). An entry is a package name or a mapping with a default name and per-manager overrides. On Debian and Ubuntu hosts, the first apt-get install in a run refreshes the package index once with apt-get update -qq and installs with --no-install-recommends; --check, --dry-run, and already-installed packages never invoke apt-get update.

packages:
  - curl
  - name: nfs-kernel-server
    dnf: nfs-utils
    pacman: nfs-utils
  - name: ripgrep
    brew: rg
FieldMeaning
namePackage name used by any manager without an override.
apt, apk, dnf, pacman, brewPackage name override for that manager.

dirs

dirs:
  - path: /srv/media
    mode: "0755"
    owner: media
FieldMeaning
pathRequired. Created with mkdir -p.
modeOctal mode as a quoted string, such as "0750".
ownerUser or user:group applied with chown.

files

Writes a whole file atomically via a temporary file and mv, only when its bytes, mode, or owner differ. Content is always embedded as literal text in a single-quoted heredoc; environment variables and backticks are never expanded. When mode is omitted, an existing destination keeps its permissions and a new file uses 0666 & ~umask.

files:
  - path: /etc/motd
    content: |
      runner: built by strapped.sh
  - path: ~/.config/app.conf
    src: files/app.conf
    mode: "0644"
FieldMeaning
pathRequired. Destination path.
contentInline literal content. Supply this or src.
srcLocal file read at compile time, relative to the recipe. Fails the secret guardrail if it contains a token or private key.
modeOptional octal mode string such as "0644". Omit to preserve existing permissions or use 0666 & ~umask on new files.
ownerOptional user or user:group applied with chown.

lines

Ensures one exact literal line is present in a file (checked with grep -Fqx), creating parent directories and appending the line if absent. Multi-line values fail compilation; other lines in the file are left alone.

lines:
  - path: /etc/exports
    line: "/srv/media 192.168.1.0/24(ro,no_subtree_check)"
symlinks:
  - src: /srv/media
    dest: /home/media

dest is the symlink and src is its target. Parent directories of dest are created as needed, and an existing symlink is replaced with ln -sfn.

git

git:
  - repo: https://github.com/you/homelab-compose.git
    dest: /srv/compose
    ref: main

Clones repo into dest if no checkout exists there. When ref is set, the check compares HEAD against that branch, tag, or commit, and apply fetches and checks it out.

services

services:
  - name: smartd
    state: enabled

Manages systemd and OpenRC services. state is running (default), started, stopped, enabled, or disabled. enabled ensures the service is both enabled at boot and running; disabled ensures neither.

steps

Escape hatch for anything the other seven primitives do not cover. Your check and apply shell bodies are copied into the script verbatim; check stdout is silenced while stderr stays visible.

steps:
  - name: app-user
    deps: [adduser]
    check: id -u app >/dev/null 2>&1
    apply: adduser -D -H -s /sbin/nologin app
FieldMeaning
nameRequired. Step name.
checkRequired. Exits 0 when the machine is already converged.
applyRequired. Converges the step.
depsCommands verified with command -v before apply runs. Missing commands fail the step with an explicit message.

Local straps

A strap is a reusable step template stored at <straps_dir>/<key>.yml alongside your recipes and referenced by filename as a top-level recipe key. strapped substitutes each {{param}} in a single pass with POSIX shell quoting (substituted values are never rescanned), so the emitted script contains plain shell and fetches nothing at run time.

# straps/sysctl.yml
name: sysctl
deps: [sysctl]
params: [key, value]
check: |
  [ "$(sysctl -n {{key}})" = {{value}} ]
apply: |
  sysctl -w {{key}}={{value}}
# ssh-box.yml
sysctl:
  - key: vm.swappiness
    value: "10"
  - key: net.ipv4.ip_forward
    value: "1"

That compiles to:

step_check_1() {
    [ "$(sysctl -n 'vm.swappiness')" = '10' ]
}

step_apply_1() {
    require_cmd 'sysctl' 'sysctl'
    sysctl -w 'vm.swappiness'='10'
}
FieldMeaning
nameRequired. Default step name; repeated uses get #2, #3, and so on (overridable per invocation with name).
check, applyRequired. Shell templates using {{param}}.
paramsRequired parameter names. Every listed parameter must be supplied, and unknown parameters fail compilation.
raw_paramsParameters allowed for unquoted {{raw:param}} splicing. Reserve for trusted shell fragments.
depsCommands verified with command -v before applying.

A strap key accepts one parameter mapping or a list of them. When a parameter's entire value is ${VAR} or ${VAR:?...} and VAR is listed in strapped.env_required, it compiles to "${VAR}" so the value is read from the environment at run time. Strap files go through the same secret guardrail as recipes.

Handlers and notify

Handlers run once at the end of an apply pass when at least one notifying step changed something: reloading a daemon, re-exporting NFS shares, or regenerating an index. Any step can set notify to one handler name or a list. A changed step sets a static NOTIFY_<ID>=1 flag; after every step finishes, each flagged handler runs once in definition order.

lines:
  - path: /etc/exports
    line: "/srv/media 10.0.0.0/24(ro)"
    notify: reload-exports

handlers:
  reload-exports:
    apply: exportfs -ra

Each handler compiles to its own function and flag variable, avoiding dynamic dispatch and eval. Referencing an undefined handler fails compilation; defined handlers with no callers are validated and omitted from output. Handlers never run under --check or --dry-run.

Secret guardrail

Secrets belong in the target's environment, never baked into a recipe or compiled script. List required variable names under strapped.env_required and reference them as ${VAR:?missing} in steps, handlers, or local strap parameters. files and lines always treat their content as literal text and never expand environment variables.

strapped:
  env_required: [REGISTRY_TOKEN]

steps:
  - name: registry-auth
    check: test -s ~/.auth
    apply: |
      printf '%s\n' "${REGISTRY_TOKEN:?missing}" > ~/.auth

Before step one, the script checks each listed variable with if [ -z "${VAR:-}" ]; then ... exit 1; fi so an unset or empty secret stops the run immediately. At compile time, strapped refuses any recipe, strap, or files[].src file containing a secret-like key ending in password, passwd, secret, token, api_key, apikey, access_key, or private_key with a literal value, or any value matching a GitHub, OpenAI, AWS, or Slack token or a PEM private key.

$ strapped compile leaky.yml
strapped: secret guardrail: refusing hardcoded value under key 'api_token' (declare it in strapped.env_required and reference ${VAR:?missing} in steps, handlers, or strap params; files and lines are literal)

The strapped command

CommandBehaviour
strapped compile <recipe.yml> [-o setup.sh]Compiles the recipe to deterministic POSIX /bin/sh. Writes to stdout unless -o names a file (written atomically with mode 0755).
strapped diff <recipe.yml> <setup.sh>Compares an existing script against current compiled output. Exits 0 when they match, 1 when they differ (printing a unified diff), and 2 on error.
strapped lint <recipe.yml>Compiles the recipe and checks syntax with sh -n, then runs shellcheck -s sh if installed (warns on stderr if absent).
strapped --versionPrints the version (-V, -v, --version).
strapped --helpPrints CLI usage (-h, --help).

The compiler itself is POSIX sh plus YAML.sh (ysh), with ysh's environment, file, and eval operators disabled. Neither is needed on the target.

The emitted script

Emitted scripts are strict POSIX /bin/sh: set -eu, no eval, every value POSIX-quoted, embedded files in single-quoted heredocs, and only the helper functions the recipe uses. Every generated script passes sh -n and shellcheck -s sh with zero warnings.

FlagBehaviour
--checkRuns every check without changing anything. Exits 0 when converged, 2 on drift, 1 on error.
--dry-runLists steps that would apply without modifying the machine. Exits 2 if any step has drifted.
--only <step>Runs one step by name (--help lists every step name). Exits 1 if no step matches.
--verboseStreams command output while applying. By default, output is captured and printed only if a step fails.
-h, --helpPrints usage and the full step list.

Each step prints one line:

LineMeaning
[ok] <step>Already converged; nothing changed.
[drift] <step>Not converged (--check).
[would-run] <step>Would apply (--dry-run).
[changed] <step>Applied.
[handler] <name>Notified handler ran.
[error] <step>Step failed; printed to stderr with captured output, then exits 1.