YAML recipes in. One plain /bin/sh script out.
Write down what a box should look like in YAML, compile it to one plain POSIX shell script, and read the whole thing in a pull request before it touches a server. The target only needs /bin/sh.
strapped:
name: homelab
target: linux
packages:
- curl
- git
- smartmontools
- name: nfs-kernel-server
dnf: nfs-utils
pacman: nfs-utils
dirs:
- path: /srv/media
mode: "0755"
lines:
- path: /etc/exports
line: "/srv/media 192.168.1.0/24(ro)"
notify: reload-exports
symlinks:
- src: /srv/media
dest: /home/media
services:
- name: smartd
state: enabled
handlers:
reload-exports:
apply: exportfs -ra
#!/bin/sh
# Generated by strapped 2.0.0 from homelab.yml
set -eu
# … flags, linux target guard, helpers, steps 1–3
step_check_4() {
pkg_detect
case "$PKG_MGR" in
dnf) _p='nfs-utils' ;;
pacman) _p='nfs-utils' ;;
*) _p='nfs-kernel-server' ;;
esac
pkg_has "$_p"
}
step_check_5() {
[ -d '/srv/media' ] || return 1
[ "$(path_mode '/srv/media')" = '755' ]
}
step_apply_5() {
mkdir -p '/srv/media'
chmod '0755' '/srv/media'
}
if step_select 'line:/etc/exports'; then
if step_check_6 >/dev/null; then
printf '[ok] %s\n' 'line:/etc/exports'
elif step_should_apply 'line:/etc/exports'; then
step_begin 'line:/etc/exports'
step_apply_6
step_finish 'changed' 'line:/etc/exports'
NOTIFY_RELOAD_EXPORTS=1
fi
fi
if [ "$MODE" = 'apply' ]; then run_handlers; fi
if [ "$DRIFT" -ne 0 ]; then exit 2; fi
exit 0
$ sh setup.sh --check
[ok] package:curl
[ok] package:git
[drift] package:smartmontools
[ok] package:nfs-kernel-server
[ok] dir:/srv/media
[drift] line:/etc/exports
[ok] symlink:/home/media
[drift] service:smartd
$ echo $?
2
$ sudo sh setup.sh
[ok] package:curl
[ok] package:git
[changed] package:smartmontools
[ok] package:nfs-kernel-server
[ok] dir:/srv/media
[changed] line:/etc/exports
[ok] symlink:/home/media
[changed] service:smartd
[handler] reload-exports
- Lines
- 458
- eval
- none
- shellcheck -s sh
- 0 warnings
strapped:
name: runner
target: linux
env_required:
- REGISTRY_TOKEN
packages:
- ca-certificates
- tini
dirs:
- path: /app
mode: "0750"
files:
- path: /etc/motd
content: |
runner: built by strapped.sh
steps:
- name: app-user
check: id -u app >/dev/null 2>&1
apply: adduser -D -H -s /sbin/nologin app
- name: registry-auth
check: test -s /root/.registry-auth
apply: |
umask 077
printf '%s\n' "${REGISTRY_TOKEN:?missing}" \
> /root/.registry-auth
#!/bin/sh
# Generated by strapped 2.0.0 from alpine.yml
set -eu
# … flags and linux target guard
if [ -z "${REGISTRY_TOKEN:-}" ]; then
printf 'strapped: missing required environment variable: %s\n' \
'REGISTRY_TOKEN' >&2
exit 1
fi
# … helpers (pkg_*, path_mode, write_file_atomic), steps 1–3
step_content_4() {
cat <<'EOF_STRAPPED_4'
runner: built by strapped.sh
EOF_STRAPPED_4
}
step_check_4() {
[ -f '/etc/motd' ] || return 1
step_content_4 | cmp -s - '/etc/motd'
}
step_apply_4() {
step_content_4 | write_file_atomic '/etc/motd' '' ''
}
step_check_6() {
test -s /root/.registry-auth
}
step_apply_6() {
umask 077
printf '%s\n' "${REGISTRY_TOKEN:?missing}" \
> /root/.registry-auth
}
# … step calls
if [ "$DRIFT" -ne 0 ]; then exit 2; fi
exit 0
$ sh setup.sh --check
[drift] package:ca-certificates
[drift] package:tini
[drift] dir:/app
[drift] file:/etc/motd
[drift] app-user
[drift] registry-auth
$ echo $?
2
$ sh setup.sh
[changed] package:ca-certificates
[changed] package:tini
[changed] dir:/app
[changed] file:/etc/motd
[changed] app-user
[changed] registry-auth
$ sh setup.sh --check
[ok] package:ca-certificates
[ok] package:tini
[ok] dir:/app
[ok] file:/etc/motd
[ok] app-user
[ok] registry-auth
$ echo $?
0
- Lines
- 353
- eval
- none
- shellcheck -s sh
- 0 warnings
name: sysctl
deps:
- sysctl
params:
- key
- value
check: |
[ "$(sysctl -n {{key}})" = {{value}} ]
apply: |
sysctl -w {{key}}={{value}}
strapped:
name: ssh-box
target: linux
sysctl:
- key: vm.swappiness
value: "10"
- key: net.ipv4.ip_forward
value: "1"
lines:
- path: /etc/sysctl.d/99-ssh-box.conf
line: "vm.swappiness = 10"
#!/bin/sh
# Generated by strapped 2.0.0 from ssh-box.yml
set -eu
# … flags, linux target guard, append_line, require_cmd
step_check_1() {
[ "$(sysctl -n 'vm.swappiness')" = '10' ]
}
step_apply_1() {
require_cmd 'sysctl' 'sysctl'
sysctl -w 'vm.swappiness'='10'
}
step_check_2() {
[ "$(sysctl -n 'net.ipv4.ip_forward')" = '1' ]
}
step_apply_2() {
require_cmd 'sysctl' 'sysctl#2'
sysctl -w 'net.ipv4.ip_forward'='1'
}
if step_select 'sysctl'; then
if step_check_1 >/dev/null; then
printf '[ok] %s\n' 'sysctl'
elif step_should_apply 'sysctl'; then
step_begin 'sysctl'
step_apply_1
step_finish 'changed' 'sysctl'
fi
fi
# … steps 2–3 calls
if [ "$DRIFT" -ne 0 ]; then exit 2; fi
exit 0
$ sh setup.sh --check
[drift] sysctl
[drift] sysctl#2
[drift] line:/etc/sysctl.d/99-ssh-box.conf
$ echo $?
2
$ sh setup.sh --help
Usage: ssh-box [options]
Options:
--check Run checks only (exit 0 converged, 2 drift, 1 error)
--dry-run Show unconverged steps without applying (exit 2 if changes needed)
--only <step> Run only the named step
--verbose Show command output while applying
-h, --help Show this help message
Steps:
sysctl
sysctl#2
line:/etc/sysctl.d/99-ssh-box.conf
- Lines
- 210
- eval
- none
- shellcheck -s sh
- 0 warnings
Real strapped compile output, trimmed at # … to keep the steps on screen. Every full script passes sh -n and shellcheck -s sh cleanly. The Alpine transcript is from a fresh alpine:3 container; the other two show what drift looks like before applying.
Eight primitives cover most of a machine
Every entry compiles to a named step with a check and an apply. The script checks first and only changes what has drifted, so a second run leaves a converged box untouched.
| Primitive | What it sets | How it checks |
|---|---|---|
| packages | Installs packages with apt-get, apk, dnf, pacman, or brew, with per-manager name overrides when distros disagree. | Queries the package database directly: dpkg-query, apk info -e, rpm -q, pacman -Qi, brew list. |
| dirs | Creates directories and sets their mode and owner. | [ -d ], then compares octal mode and owner. |
| files | Writes whole files atomically from inline content or a local src read at compile time. Always literal; preserves existing mode or uses 0666 & ~umask when mode is omitted. | Compares bytes with cmp -s against a single-quoted heredoc in the script. |
| lines | Ensures one exact literal line is present in a file, creating parent directories and appending it if missing. | grep -Fqx |
| symlinks | Points a symlink at a target path, replacing any existing link. | readlink |
| git | Clones a repository to a path, optionally pinned to a branch, tag, or commit. | Checks for the checkout and compares HEAD against ref. |
| services | Sets a systemd or OpenRC service to running, stopped, enabled, or disabled. | systemctl is-active / is-enabled, or rc-service / rc-update. |
| steps | Anything else, written as your own POSIX shell check and apply. | Your check command, verbatim. |
Local straps, compiled in
Drop a reusable step template into straps/ beside your recipes and call it by filename. strapped substitutes each parameter with POSIX quoting in a single pass at compile time; nothing is pulled from a registry when the script runs.
# straps/sysctl.yml
name: sysctl
params: [key, value]
check: |
[ "$(sysctl -n {{key}})" = {{value}} ]
apply: |
sysctl -w {{key}}={{value}}
# ssh-box.yml
sysctl:
- key: vm.swappiness
value: "10"
Handlers run once, at the end
Steps can notify named handlers. A changed step flips a static shell flag; after the last step finishes, each flagged handler runs once in definition order. Every handler is a plain shell function, never looked up through eval.
lines:
- path: /etc/exports
line: "/srv/media 10.0.0.0/24(ro)"
notify: reload-exports
handlers:
reload-exports:
apply: exportfs -ra
Secrets stay out of recipes
Declare secret names under env_required and reference ${VAR:?missing} in steps, handlers, or strap parameters; files and lines stay literal. The script exits before step one if a variable is unset, and strapped refuses hardcoded tokens or private keys.
strapped:
env_required: [REGISTRY_TOKEN]
steps:
- name: registry-auth
check: test -s ~/.auth
apply: |
printf '%s\n' \
"${REGISTRY_TOKEN:?missing}" \
> ~/.auth
Where it fits
Use strapped.sh for machines you want to rebuild from one reviewed file: homelab servers, small VPSes, throwaway VMs, containers, or a repository's setup script. Keep your daily workstation on dotfiles and package managers built for it.
| Tool | Reach for it when | Target needs | What you review |
|---|---|---|---|
| strapped.sh | You want a reproducible box and want to read every line that will run on it. | /bin/sh and the host's own package manager. | One compiled script, diffable in a pull request. |
| mise and chezmoi | You are configuring your daily workstation: tool versions, dotfiles, per-machine templates. | mise and chezmoi installed. | Your dotfiles repository and mise.toml. strapped.sh does not try to replace them. |
| Ansible | You run a fleet: inventories, cross-host orchestration, and a large module library. | Python on each host and Ansible on the controller. | Playbooks and roles; the actual commands are resolved at run time. |
| A hand-written script | You have a one-off with five commands. | /bin/sh. | Whatever you wrote. Checking before changing, --check, and safe reruns are on you. |
Install
Install strapped on your workstation or CI runner. Compilation needs /bin/sh and YAML.sh (ysh); the target machine only runs the compiled script.
-
With Homebrew
brew install azohra/tools/strapped -
With the install script
curl -fsSL https://strapped.azohra.com/install | sh
Quickstart
-
Write a recipe.
strapped: name: box target: linux packages: - curl - htop lines: - path: /etc/hosts line: "192.168.1.20 nas.lan nas" -
Compile it, then read the output.
strapped compile box.yml -o setup.sh -
Copy it over and check for drift without changing anything.
scp setup.sh box: && ssh box sh setup.sh --check -
Apply it. Run it again anytime; converged steps print
[ok]and skip work.ssh -t box sudo sh setup.sh