130 lines
5.3 KiB
Bash
Executable File
130 lines
5.3 KiB
Bash
Executable File
#!/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 "$@"
|