feat: render config files

This commit is contained in:
alexm committed 2026-10-11 07:32:25 -04:00
1 parent cf4bd07a8e
commit fbef79b5f9
5 files changed
+264 -1

No files matched your search

+7 -1
View File
@@ -70,7 +70,13 @@ register-host name:
capture-hardware name:
@./scripts/capture-hardware.sh -- "$1"
# Run the tests for scripts/.
# Render the Home Manager managed files (helix, git, ...) into ./rendered as plain
# files, without touching ~ or the system. Works on any machine with Nix.
# Example: just render-config --host willow --list
render-config *args:
@./scripts/render-config.sh "$@"
# Run the tests for scripts/ (no root, network, or real disks needed).
test:
@./scripts/tests/run.sh
+129
View File
@@ -0,0 +1,129 @@
#!/usr/bin/env bash
# Render the Home Manager managed config files (helix, git, ...) into a local
# directory as plain, writable files. Nothing outside that directory is touched:
# no activation, no ~/.config changes, no root.
#
# It builds <host>'s Home Manager activation package straight from the flake and
# copies its home-files tree out of the Nix store.
set -euo pipefail
SCRIPT_DIR="$(cd -- "$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")" && pwd -P)"
# shellcheck source=lib/common.sh
source "$SCRIPT_DIR/lib/common.sh"
usage() {
cat <<'EOF'
Usage: render-config.sh [options]
--host NAME nixosConfigurations entry to render (default: first host in flake.nix)
--user NAME Home Manager user (default: the current user)
--attr ATTR full flake attribute of a Home Manager activationPackage;
overrides --host/--user (e.g. homeConfigurations.alex.activationPackage)
--out DIR output directory (default: rendered)
--repo-root DIR repository root (default: current directory)
--list also print the rendered file list on stdout
-h, --help show this help
DIR is only ever replaced if it is missing, empty, or was created by this tool.
Paths come out relative to $HOME, e.g. DIR/.config/helix/config.toml, DIR/.gitconfig.
Exit codes: 0 ok, 1 error, 2 usage, 3 nix not found.
EOF
}
HOST="" USER_NAME="" ATTR="" OUT="rendered" REPO="." LIST=0
MARKER=".rendered-by-render-config"
parse_args() {
while (($#)); do
case "$1" in
--host=*) HOST="${1#*=}"; shift ;;
--user=*) USER_NAME="${1#*=}"; shift ;;
--attr=*) ATTR="${1#*=}"; shift ;;
--out=*) OUT="${1#*=}"; shift ;;
--repo-root=*) REPO="${1#*=}"; shift ;;
--host) need_value "$@"; HOST="$2"; shift 2 ;;
--user) need_value "$@"; USER_NAME="$2"; shift 2 ;;
--attr) need_value "$@"; ATTR="$2"; shift 2 ;;
--out) need_value "$@"; OUT="$2"; shift 2 ;;
--repo-root) need_value "$@"; REPO="$2"; shift 2 ;;
--list) LIST=1; shift ;;
-h|--help) usage; exit 0 ;;
*) die "unknown argument: $1 (see --help)" 2 ;;
esac
done
[[ -n "$OUT" ]] || die "--out must not be empty" 2
if [[ -n "$ATTR" ]]; then
[[ "$ATTR" =~ ^[A-Za-z0-9_.\"\'-]+$ ]] || die "invalid --attr: '$ATTR'" 2
else
[[ -z "$HOST" ]] || valid_hostname "$HOST" || die "invalid --host: '$HOST'" 2
[[ -z "$USER_NAME" || "$USER_NAME" =~ ^[A-Za-z_][A-Za-z0-9_-]*$ ]] || die "invalid --user: '$USER_NAME'" 2
fi
}
# Only replace a directory we can prove is ours (or empty): --out is user
# input and we are about to delete what's in it.
prepare_out() {
if [[ -e "$OUT" && ! -d "$OUT" ]]; then
die "$OUT exists and is not a directory" 1
fi
if [[ -d "$OUT" ]]; then
if [[ -f "$OUT/$MARKER" ]]; then
rm -rf -- "${OUT:?}"
elif [[ -z "$(ls -A -- "$OUT")" ]]; then
:
else
die "$OUT is not empty and wasn't created by render-config; refusing to delete it. Pick another --out or remove it." 1
fi
fi
}
main() {
parse_args "$@"
command -v nix >/dev/null 2>&1 || die "nix not found. Install Nix first (https://nixos.org/download)." 3
[[ -d "$REPO" ]] || die "repo root not found: $REPO" 1
cd -- "$REPO"
[[ -f flake.nix ]] || die "no flake.nix in $PWD" 1
if [[ -z "$ATTR" ]]; then
if [[ -z "$HOST" ]]; then
local hosts
hosts="$("$SCRIPT_DIR/list-hosts.py" flake.nix)" || die "could not read hosts from flake.nix" 1
HOST="${hosts%%$'\n'*}"
[[ -n "$HOST" ]] || die "no hosts found in flake.nix; pass --host or --attr" 1
fi
USER_NAME="${USER_NAME:-$(id -un)}"
ATTR="nixosConfigurations.${HOST}.config.home-manager.users.${USER_NAME}.home.activationPackage"
fi
log "Building .#${ATTR}"
local store_path
store_path="$(nix --extra-experimental-features 'nix-command flakes' \
build --no-link --print-out-paths ".#${ATTR}")" \
|| die "nix build failed. Flakes only see git-tracked files: did you \`git add\` new files? Does user/host exist?" 1
store_path="${store_path%%$'\n'*}"
[[ -d "$store_path/home-files" ]] || die "build output has no home-files directory: $store_path" 1
prepare_out
umask 077 # rendered configs may hold tokens
mkdir -p -- "$OUT"
# -L copies file contents instead of store symlinks; store files are
# read-only, so make the copies writable.
cp -RL -- "$store_path/home-files/." "$OUT/" || { rm -rf -- "${OUT:?}"; die "copy failed" 1; }
chmod -R u+w -- "$OUT"
: > "$OUT/$MARKER"
local count
count="$(find "$OUT" -type f ! -name "$MARKER" | wc -l | tr -d ' ')"
log "Rendered ${count} file(s) into ${OUT}/"
if ((LIST)); then
(cd -- "$OUT" && find . -type f ! -name "$MARKER" | sed 's|^\./||' | LC_ALL=C sort)
fi
if command -v git >/dev/null 2>&1 && git rev-parse --is-inside-work-tree >/dev/null 2>&1 \
&& ! git check-ignore -q -- "$OUT/$MARKER" 2>/dev/null; then
warn "$OUT/ is not git-ignored; add '/${OUT%/}/' to .gitignore so it isn't committed."
fi
}
main "$@"
+1
View File
@@ -26,6 +26,7 @@ need python3 python3 && run_suite python python3 -m unittest -q test_python
need fetch curl python3 flock && run_suite shell/fetch-iso ./test_fetch_iso.sh
run_suite shell/gen-keys ./test_gen_keys.sh
run_suite shell/capture-hardware ./test_capture_hardware.sh
run_suite shell/render-config ./test_render_config.sh
need docker gpg python3 && run_suite shell/docker-creds ./test_docker_creds.sh
need usb script cmp findmnt && run_suite shell/make-usb ./test_make_usb.sh
+15
View File
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
# Fake nix: `nix build --no-link --print-out-paths .#ATTR` -> fabricates a store-like dir.
echo "nix $*" >> "${FAKE_NIX_LOG:-/dev/null}"
[[ -z "${FAKE_NIX_FAIL:-}" ]] || { echo "error: attribute missing" >&2; exit 1; }
attr="${@: -1}"
out="${FAKE_NIX_STORE:?}/abc-home-manager-generation"
mkdir -p "$out/home-files/.config/helix" "$out/home-files/.config/git" "$out/store"
echo 'theme = "base16"' > "$out/store/helix.toml"; chmod 444 "$out/store/helix.toml"
ln -sfn "$out/store/helix.toml" "$out/home-files/.config/helix/config.toml" # like real HM: symlink into store
echo '[user]
name = alex' > "$out/store/gitconfig"; chmod 444 "$out/store/gitconfig"
ln -sfn "$out/store/gitconfig" "$out/home-files/.gitconfig"
[[ -z "${FAKE_NIX_NOHOMEFILES:-}" ]] || rm -rf "$out/home-files"
echo "$attr" > "$out/attr"
echo "$out"
+112
View File
@@ -0,0 +1,112 @@
#!/usr/bin/env bash
set -u
# shellcheck source=harness.sh
source "$(dirname "$0")/harness.sh"
RC_="$SCRIPTS/render-config.sh"
setup() {
export PATH="$SHIMS:$PATH" FAKE_NIX_STORE="$T/store" FAKE_NIX_LOG="$T/nix.log"
REPO="$T/repo"; mkdir -p "$REPO"; cd "$REPO" || exit 1
cat > flake.nix <<'EOF'
{ outputs = { nixpkgs, ... }: { nixosConfigurations = {
whitegrizzly = nixpkgs.lib.nixosSystem { };
willow = nixpkgs.lib.nixosSystem { };
}; }; }
EOF
}
test_renders_plain_writable_files() {
setup
run "$RC_" --user alex
assert_rc 0; assert_err_has "Rendered 2 file(s)"
assert_file rendered/.config/helix/config.toml
[[ ! -L rendered/.config/helix/config.toml ]] || _fail "still a store symlink"
assert_file_eq rendered/.config/helix/config.toml 'theme = "base16"'
[[ -w rendered/.gitconfig ]] || _fail "not writable"
assert_mode rendered/.gitconfig 600
}
test_default_host_is_first_in_flake_and_attr_is_correct() {
setup
run "$RC_" --user alex; assert_rc 0
assert_eq ".#nixosConfigurations.whitegrizzly.config.home-manager.users.alex.home.activationPackage" "$(<"$T/store/abc-home-manager-generation/attr")"
run "$RC_" --host willow --user bob --out o2; assert_rc 0
assert_eq ".#nixosConfigurations.willow.config.home-manager.users.bob.home.activationPackage" "$(<"$T/store/abc-home-manager-generation/attr")"
}
test_attr_override() {
setup
run "$RC_" --attr homeConfigurations.alex.activationPackage; assert_rc 0
assert_eq ".#homeConfigurations.alex.activationPackage" "$(<"$T/store/abc-home-manager-generation/attr")"
}
test_list_prints_relative_sorted_paths() {
setup
run "$RC_" --user alex --list; assert_rc 0
assert_eq ".config/helix/config.toml
.gitconfig" "$OUT"
}
test_rerun_replaces_stale_files() {
setup
run "$RC_" --user alex; assert_rc 0
echo stale > rendered/.stale; echo local-edit > rendered/.gitconfig
run "$RC_" --user alex; assert_rc 0
assert_no_path rendered/.stale
assert_file_eq rendered/.gitconfig "$(printf '[user]\nname = alex')"
}
test_refuses_to_delete_unrelated_directory() {
setup
mkdir -p precious; echo data > precious/file
run "$RC_" --user alex --out precious
assert_rc 1; assert_err_has "refusing to delete"
assert_file_eq precious/file data
mkdir empty; run "$RC_" --user alex --out empty; assert_rc 0 # empty dir is fine
echo x > afile; run "$RC_" --user alex --out afile; assert_rc 1
}
test_build_failure_leaves_previous_render_intact() {
setup
run "$RC_" --user alex; assert_rc 0
FAKE_NIX_FAIL=1 run "$RC_" --user alex
assert_rc 1; assert_err_has "git add"
assert_file rendered/.gitconfig
}
test_output_without_home_files_is_an_error() {
setup
FAKE_NIX_NOHOMEFILES=1 run "$RC_" --user alex
assert_rc 1; assert_err_has "no home-files"
assert_no_path rendered
}
test_input_validation() {
setup
local a
for a in "--host Bad" "--host ../x" "--user a;b" "--user 1a" "--attr a;b" "--attr \$(x)" "--bogus"; do
# shellcheck disable=SC2086
run "$RC_" $a; [[ $RC == 2 ]] || _fail "'$a' -> exit $RC"
done
run "$RC_" --out ""; assert_rc 2
[[ ! -e "$FAKE_NIX_LOG" ]] || _fail "nix ran on invalid input"
}
test_environment_errors() {
setup
local bin="$T/bin"; mkdir -p "$bin"
local c; for c in bash dirname readlink env cat; do ln -s "$(command -v $c)" "$bin/$c"; done
PATH="$bin" run "$RC_"; assert_rc 3
rm flake.nix; run "$RC_" --user alex; assert_rc 1; assert_err_has "no flake.nix"
run "$RC_" --repo-root /nonexistent; assert_rc 1
}
test_warns_when_not_gitignored() {
setup
git init -q .; run "$RC_" --user alex; assert_err_has "not git-ignored"
echo "/rendered/" > .gitignore; run "$RC_" --user alex; assert_err_lacks "not git-ignored"
}
test_help() { setup; run "$RC_" -h; assert_rc 0; assert_out_has "Usage:"; }
run_all "${1:-}"