Archived
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2661f6d271 | ||
|
|
0532c3a282 | ||
|
|
ac8c9a20e3 | ||
|
|
7e51168d1b | ||
|
|
c939454983 |
No files matched your search
@@ -117,6 +117,16 @@ Beyond `codex-setup.sh`/`codex-maintenance.sh` above, `scripts/` also has:
|
|||||||
for the manual steps `sync-host-keys.sh`/`create-proxmox-resource.sh`
|
for the manual steps `sync-host-keys.sh`/`create-proxmox-resource.sh`
|
||||||
print when they bootstrap a brand-new, not-yet-trusted key on a machine
|
print when they bootstrap a brand-new, not-yet-trusted key on a machine
|
||||||
with no prior admin access.
|
with no prior admin access.
|
||||||
|
- `scripts/backup-admin-key.sh <dest-path> [--key-file <path>] [--force]
|
||||||
|
[--dry-run]` — copies the local sops age key (source resolution matches
|
||||||
|
sops/age itself: `$SOPS_AGE_KEY` inline, then `--key-file`, then
|
||||||
|
`$SOPS_AGE_KEY_FILE`, then the XDG default) to an arbitrary destination
|
||||||
|
path with `0600` permissions, validating it's a real age identity and
|
||||||
|
round-tripping the public key before and after the write. Refuses to
|
||||||
|
overwrite an existing `<dest-path>` without `--force`. Purely a local
|
||||||
|
filesystem copy — never touches `.sops.yaml`/`secrets/*.yaml` or the
|
||||||
|
repo at all. The resulting file is exactly what `rotate-admin-key.sh`
|
||||||
|
expects as its backup-key argument.
|
||||||
|
|
||||||
`sync-host-keys.sh`, `create-proxmox-resource.sh`, and
|
`sync-host-keys.sh`, `create-proxmox-resource.sh`, and
|
||||||
`rotate-admin-key.sh` genuinely mutate real state when run for real (not
|
`rotate-admin-key.sh` genuinely mutate real state when run for real (not
|
||||||
@@ -125,6 +135,9 @@ containers, real revocation of decrypt access. They require the
|
|||||||
operator's own SSH/sops access, which an agent session doesn't have — but
|
operator's own SSH/sops access, which an agent session doesn't have — but
|
||||||
don't suggest running any of them non-dry-run without the operator's
|
don't suggest running any of them non-dry-run without the operator's
|
||||||
explicit go-ahead even if it becomes technically reachable.
|
explicit go-ahead even if it becomes technically reachable.
|
||||||
|
`backup-admin-key.sh` only writes a key copy to a path the operator gives
|
||||||
|
it — lower-stakes than the others, but it still handles a real private
|
||||||
|
key, so treat its destination path choice as the operator's call too.
|
||||||
|
|
||||||
## Architecture
|
## Architecture
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,16 @@
|
|||||||
_:
|
_:
|
||||||
|
|
||||||
{
|
{
|
||||||
|
imports = [ ./enable-service.nix ];
|
||||||
|
|
||||||
services.tailscale = {
|
services.tailscale = {
|
||||||
enable = true;
|
# Enables the sysctl forwarding settings exit nodes/subnet routers need;
|
||||||
|
# without this, --advertise-exit-node has no effect.
|
||||||
|
useRoutingFeatures = "server";
|
||||||
|
|
||||||
|
# Lets peers reach this node directly over the tailscale UDP port
|
||||||
|
# instead of relaying through DERP.
|
||||||
|
openFirewall = true;
|
||||||
|
|
||||||
# extraSetFlags (tailscale set, via the always-on tailscaled-set
|
# extraSetFlags (tailscale set, via the always-on tailscaled-set
|
||||||
# service), not extraUpFlags -- extraUpFlags is only ever applied by
|
# service), not extraUpFlags -- extraUpFlags is only ever applied by
|
||||||
|
|||||||
Executable
+148
@@ -0,0 +1,148 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Backs up the local sops age key (the private key that decrypts
|
||||||
|
# secrets/*.yaml -- normally the one trusted as &admin) to an arbitrary
|
||||||
|
# destination path, e.g. a USB drive or other offline storage, so it can
|
||||||
|
# later be restored and handed to rotate-admin-key.sh if this machine's
|
||||||
|
# copy is ever lost, or to run either script from a different machine.
|
||||||
|
#
|
||||||
|
# Usage:
|
||||||
|
# scripts/backup-admin-key.sh <dest-path> [--key-file <path>] [--force] [--dry-run]
|
||||||
|
#
|
||||||
|
# Source key resolution matches sops/age's own default order:
|
||||||
|
# $SOPS_AGE_KEY (inline identity text) if set, else
|
||||||
|
# --key-file if given, else
|
||||||
|
# $SOPS_AGE_KEY_FILE if set, else
|
||||||
|
# ${XDG_CONFIG_HOME:-$HOME/.config}/sops/age/keys.txt
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
repo_root="$(cd "$(dirname "$0")/.." && pwd)"
|
||||||
|
sops_yaml="${repo_root}/.sops.yaml"
|
||||||
|
|
||||||
|
# shellcheck source=env.sh
|
||||||
|
source "${repo_root}/scripts/env.sh"
|
||||||
|
|
||||||
|
# Pin cwd for the same reason rotate-admin-key.sh does: age/sops calls
|
||||||
|
# below should never depend on wherever the caller's shell happened to be.
|
||||||
|
cd "$repo_root"
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
cat <<EOF
|
||||||
|
Usage: $0 <dest-path> [--key-file <path>] [--force] [--dry-run]
|
||||||
|
|
||||||
|
<dest-path> Where to write the backup. Parent directories are
|
||||||
|
created as needed. Written with 0600 permissions.
|
||||||
|
--key-file <path> Read the key from here instead of the default
|
||||||
|
sops/age resolution (\$SOPS_AGE_KEY_FILE, then
|
||||||
|
\${XDG_CONFIG_HOME:-\$HOME/.config}/sops/age/keys.txt).
|
||||||
|
Ignored if \$SOPS_AGE_KEY is set (that always wins,
|
||||||
|
same precedence sops/age itself uses).
|
||||||
|
--force Overwrite <dest-path> if it already exists.
|
||||||
|
--dry-run Print what would happen; write nothing.
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
dry_run=0
|
||||||
|
force=0
|
||||||
|
key_file="${SOPS_AGE_KEY_FILE:-${XDG_CONFIG_HOME:-$HOME/.config}/sops/age/keys.txt}"
|
||||||
|
args=()
|
||||||
|
|
||||||
|
while [[ $# -gt 0 ]]; do
|
||||||
|
case "$1" in
|
||||||
|
--dry-run)
|
||||||
|
dry_run=1
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
--force)
|
||||||
|
force=1
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
--key-file)
|
||||||
|
key_file="${2:?--key-file requires a path}"
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
-h | --help)
|
||||||
|
usage
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
--*)
|
||||||
|
echo "Unknown option: $1" >&2
|
||||||
|
usage >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
args+=("$1")
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ "${#args[@]}" -ne 1 ]]; then
|
||||||
|
usage >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
dest="${args[0]}"
|
||||||
|
|
||||||
|
nix_extra_opts
|
||||||
|
|
||||||
|
if [[ -n "${SOPS_AGE_KEY:-}" ]]; then
|
||||||
|
echo "==> Source: \$SOPS_AGE_KEY (inline identity from the environment)."
|
||||||
|
src_content="$SOPS_AGE_KEY"
|
||||||
|
else
|
||||||
|
[[ -s "$key_file" ]] || {
|
||||||
|
echo "ERROR: no key found. \$SOPS_AGE_KEY is unset and ${key_file} doesn't exist or is empty." >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
echo "==> Source: ${key_file}"
|
||||||
|
src_content="$(cat "$key_file")"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Round-trip through a private scratch file (rather than trusting the
|
||||||
|
# source string as-is) so age-keygen -y validates it's a real identity
|
||||||
|
# before anything is written to <dest-path>.
|
||||||
|
scratch="$(mktemp)"
|
||||||
|
trap 'rm -f "$scratch"' EXIT
|
||||||
|
( umask 077; printf '%s\n' "$src_content" > "$scratch" )
|
||||||
|
|
||||||
|
src_pub="$(nix-shell "${NIX_OPTS[@]}" -p age --run "age-keygen -y '$scratch'")" || {
|
||||||
|
echo "ERROR: source doesn't look like a valid age identity (age-keygen -y failed)." >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
echo " public key: ${src_pub}"
|
||||||
|
|
||||||
|
current_admin_pub="$(grep -E '^ - &admin age1' "$sops_yaml" 2>/dev/null | awk '{print $NF}' || true)"
|
||||||
|
if [[ -n "$current_admin_pub" && "$current_admin_pub" != "$src_pub" ]]; then
|
||||||
|
echo "NOTE: this key does not match .sops.yaml's current &admin entry (${current_admin_pub})."
|
||||||
|
echo " Backing it up anyway -- this script doesn't require it to be the admin key."
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -e "$dest" && "$force" -ne 1 ]]; then
|
||||||
|
echo "ERROR: ${dest} already exists. Pass --force to overwrite." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$dry_run" -eq 1 ]]; then
|
||||||
|
echo
|
||||||
|
echo "[dry-run] would write $(wc -c <"$scratch" | tr -d ' ') bytes to ${dest} (mode 0600)"
|
||||||
|
[[ -e "$dest" ]] && echo "[dry-run] would overwrite existing file (--force given)"
|
||||||
|
echo "[dry-run] Nothing was written. Re-run without --dry-run to apply this."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
mkdir -p "$(dirname "$dest")"
|
||||||
|
install -m 600 "$scratch" "$dest"
|
||||||
|
|
||||||
|
dest_pub="$(nix-shell "${NIX_OPTS[@]}" -p age --run "age-keygen -y '$dest'")"
|
||||||
|
if [[ "$dest_pub" != "$src_pub" ]]; then
|
||||||
|
echo "ERROR: ${dest} was written but its public key doesn't match the source -- investigate before relying on this backup." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
cat <<EOF
|
||||||
|
|
||||||
|
Done. Backed up to: ${dest}
|
||||||
|
public key: ${dest_pub}
|
||||||
|
|
||||||
|
This is a private key -- store it somewhere offline/secure, not in this
|
||||||
|
repo or anywhere it'd get committed. Restore it with:
|
||||||
|
scripts/rotate-admin-key.sh ${dest}
|
||||||
|
EOF
|
||||||
@@ -1,22 +1,14 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
export NIX_CONFIG="${NIX_CONFIG:-}
|
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
experimental-features = nix-command flakes
|
# shellcheck source=lib/nix-bootstrap.sh
|
||||||
accept-flake-config = false
|
source "${script_dir}/lib/nix-bootstrap.sh"
|
||||||
warn-dirty = false
|
# shellcheck source=lib/nix-eval.sh
|
||||||
"
|
source "${script_dir}/lib/nix-eval.sh"
|
||||||
|
|
||||||
MODE="${1:-validate}"
|
MODE="${1:-validate}"
|
||||||
|
|
||||||
ensure_nix_profile() {
|
|
||||||
if [ -f /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ]; then
|
|
||||||
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
|
|
||||||
elif [ -f "$HOME/.nix-profile/etc/profile.d/nix.sh" ]; then
|
|
||||||
. "$HOME/.nix-profile/etc/profile.d/nix.sh"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
ensure_nix_profile
|
ensure_nix_profile
|
||||||
|
|
||||||
if ! command -v nix >/dev/null 2>&1; then
|
if ! command -v nix >/dev/null 2>&1; then
|
||||||
@@ -24,8 +16,7 @@ if ! command -v nix >/dev/null 2>&1; then
|
|||||||
exit 127
|
exit 127
|
||||||
fi
|
fi
|
||||||
|
|
||||||
hosts_json="$(nix eval --json --no-use-registries --no-accept-flake-config .#nixosConfigurations --apply builtins.attrNames)"
|
hosts="$(list_flake_targets .)"
|
||||||
hosts="$(echo "$hosts_json" | jq -r '.[]')"
|
|
||||||
|
|
||||||
echo "Hosts:"
|
echo "Hosts:"
|
||||||
echo "$hosts"
|
echo "$hosts"
|
||||||
@@ -44,17 +35,17 @@ fi
|
|||||||
|
|
||||||
echo
|
echo
|
||||||
echo "Checking Nix formatting with nixpkgs-fmt..."
|
echo "Checking Nix formatting with nixpkgs-fmt..."
|
||||||
nix run --no-use-registries --no-accept-flake-config github:NixOS/nixpkgs/nixos-25.11#nixpkgs-fmt -- --check .
|
nix run "${NIX_EVAL_FLAGS[@]}" github:NixOS/nixpkgs/nixos-25.11#nixpkgs-fmt -- --check .
|
||||||
|
|
||||||
echo
|
echo
|
||||||
echo "Running statix lint..."
|
echo "Running statix lint..."
|
||||||
nix run --no-use-registries --no-accept-flake-config github:NixOS/nixpkgs/nixos-25.11#statix -- check .
|
nix run "${NIX_EVAL_FLAGS[@]}" github:NixOS/nixpkgs/nixos-25.11#statix -- check .
|
||||||
|
|
||||||
echo
|
echo
|
||||||
echo "Evaluating host toplevel derivations..."
|
echo "Evaluating host toplevel derivations..."
|
||||||
for host in $hosts; do
|
for host in $hosts; do
|
||||||
echo "==> $host"
|
echo "==> $host"
|
||||||
nix eval --raw --no-use-registries --no-accept-flake-config ".#nixosConfigurations.${host}.config.system.build.toplevel.drvPath"
|
nix eval --raw "${NIX_EVAL_FLAGS[@]}" ".#nixosConfigurations.${host}.config.system.build.toplevel.drvPath"
|
||||||
|
|
||||||
# lxc-* hosts deploy via a directly pct-restore-able tarball instead of
|
# lxc-* hosts deploy via a directly pct-restore-able tarball instead of
|
||||||
# nixos-install (see docs/auto-installer.md); proxmox-* hosts can
|
# nixos-install (see docs/auto-installer.md); proxmox-* hosts can
|
||||||
@@ -64,22 +55,21 @@ for host in $hosts; do
|
|||||||
case "$host" in
|
case "$host" in
|
||||||
lxc-*)
|
lxc-*)
|
||||||
echo "==> $host (tarball)"
|
echo "==> $host (tarball)"
|
||||||
nix eval --raw --no-use-registries --no-accept-flake-config ".#nixosConfigurations.${host}.config.system.build.tarball.drvPath"
|
nix eval --raw "${NIX_EVAL_FLAGS[@]}" ".#nixosConfigurations.${host}.config.system.build.tarball.drvPath"
|
||||||
;;
|
;;
|
||||||
proxmox-*)
|
proxmox-*)
|
||||||
echo "==> $host (diskoImagesScript)"
|
echo "==> $host (diskoImagesScript)"
|
||||||
nix eval --raw --no-use-registries --no-accept-flake-config ".#nixosConfigurations.${host}.config.system.build.diskoImagesScript.drvPath"
|
nix eval --raw "${NIX_EVAL_FLAGS[@]}" ".#nixosConfigurations.${host}.config.system.build.diskoImagesScript.drvPath"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
echo
|
echo
|
||||||
echo "Evaluating buildable packages..."
|
echo "Evaluating buildable packages..."
|
||||||
packages_json="$(nix eval --json --no-use-registries --no-accept-flake-config .#packages.x86_64-linux --apply builtins.attrNames)"
|
packages="$(nix eval --json "${NIX_EVAL_FLAGS[@]}" .#packages.x86_64-linux --apply builtins.attrNames | jq -r '.[]')"
|
||||||
packages="$(echo "$packages_json" | jq -r '.[]')"
|
|
||||||
for pkg in $packages; do
|
for pkg in $packages; do
|
||||||
echo "==> packages.x86_64-linux.${pkg}"
|
echo "==> packages.x86_64-linux.${pkg}"
|
||||||
nix eval --raw --no-use-registries --no-accept-flake-config ".#packages.x86_64-linux.${pkg}"
|
nix eval --raw "${NIX_EVAL_FLAGS[@]}" ".#packages.x86_64-linux.${pkg}"
|
||||||
done
|
done
|
||||||
|
|
||||||
if [[ "$MODE" == "dry-run" ]]; then
|
if [[ "$MODE" == "dry-run" ]]; then
|
||||||
@@ -87,16 +77,16 @@ if [[ "$MODE" == "dry-run" ]]; then
|
|||||||
echo "Running dry-run builds for all hosts. This will not create result symlinks."
|
echo "Running dry-run builds for all hosts. This will not create result symlinks."
|
||||||
for host in $hosts; do
|
for host in $hosts; do
|
||||||
echo "==> Dry-run build: $host"
|
echo "==> Dry-run build: $host"
|
||||||
nix build --dry-run --no-link --no-use-registries --no-accept-flake-config ".#nixosConfigurations.${host}.config.system.build.toplevel"
|
nix build --dry-run --no-link "${NIX_EVAL_FLAGS[@]}" ".#nixosConfigurations.${host}.config.system.build.toplevel"
|
||||||
|
|
||||||
case "$host" in
|
case "$host" in
|
||||||
lxc-*)
|
lxc-*)
|
||||||
echo "==> Dry-run build: $host (tarball)"
|
echo "==> Dry-run build: $host (tarball)"
|
||||||
nix build --dry-run --no-link --no-use-registries --no-accept-flake-config ".#nixosConfigurations.${host}.config.system.build.tarball"
|
nix build --dry-run --no-link "${NIX_EVAL_FLAGS[@]}" ".#nixosConfigurations.${host}.config.system.build.tarball"
|
||||||
;;
|
;;
|
||||||
proxmox-*)
|
proxmox-*)
|
||||||
echo "==> Dry-run build: $host (diskoImagesScript)"
|
echo "==> Dry-run build: $host (diskoImagesScript)"
|
||||||
nix build --dry-run --no-link --no-use-registries --no-accept-flake-config ".#nixosConfigurations.${host}.config.system.build.diskoImagesScript"
|
nix build --dry-run --no-link "${NIX_EVAL_FLAGS[@]}" ".#nixosConfigurations.${host}.config.system.build.diskoImagesScript"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
@@ -105,7 +95,7 @@ if [[ "$MODE" == "dry-run" ]]; then
|
|||||||
echo "Running dry-run builds for all packages."
|
echo "Running dry-run builds for all packages."
|
||||||
for pkg in $packages; do
|
for pkg in $packages; do
|
||||||
echo "==> Dry-run build: packages.x86_64-linux.${pkg}"
|
echo "==> Dry-run build: packages.x86_64-linux.${pkg}"
|
||||||
nix build --dry-run --no-link --no-use-registries --no-accept-flake-config ".#packages.x86_64-linux.${pkg}"
|
nix build --dry-run --no-link "${NIX_EVAL_FLAGS[@]}" ".#packages.x86_64-linux.${pkg}"
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
+7
-15
@@ -1,19 +1,11 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
export NIX_CONFIG="${NIX_CONFIG:-}
|
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
experimental-features = nix-command flakes
|
# shellcheck source=lib/nix-bootstrap.sh
|
||||||
accept-flake-config = false
|
source "${script_dir}/lib/nix-bootstrap.sh"
|
||||||
warn-dirty = false
|
# shellcheck source=lib/nix-eval.sh
|
||||||
"
|
source "${script_dir}/lib/nix-eval.sh"
|
||||||
|
|
||||||
ensure_nix_profile() {
|
|
||||||
if [ -f /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ]; then
|
|
||||||
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
|
|
||||||
elif [ -f "$HOME/.nix-profile/etc/profile.d/nix.sh" ]; then
|
|
||||||
. "$HOME/.nix-profile/etc/profile.d/nix.sh"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
install_nix_if_missing() {
|
install_nix_if_missing() {
|
||||||
if command -v nix >/dev/null 2>&1; then
|
if command -v nix >/dev/null 2>&1; then
|
||||||
@@ -79,13 +71,13 @@ if ! command -v jq >/dev/null 2>&1; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Available NixOS hosts:"
|
echo "Available NixOS hosts:"
|
||||||
hosts="$(nix eval --json --no-use-registries --no-accept-flake-config .#nixosConfigurations --apply builtins.attrNames | jq -r '.[]')"
|
hosts="$(list_flake_targets .)"
|
||||||
echo "$hosts"
|
echo "$hosts"
|
||||||
|
|
||||||
echo "Evaluating all host toplevel derivations..."
|
echo "Evaluating all host toplevel derivations..."
|
||||||
for host in $hosts; do
|
for host in $hosts; do
|
||||||
echo "==> Evaluating $host"
|
echo "==> Evaluating $host"
|
||||||
nix eval --raw --no-use-registries --no-accept-flake-config ".#nixosConfigurations.${host}.config.system.build.toplevel.drvPath"
|
nix eval --raw "${NIX_EVAL_FLAGS[@]}" ".#nixosConfigurations.${host}.config.system.build.toplevel.drvPath"
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "Codex setup complete."
|
echo "Codex setup complete."
|
||||||
@@ -26,6 +26,8 @@ set -euo pipefail
|
|||||||
repo_root="$(cd "$(dirname "$0")/.." && pwd)"
|
repo_root="$(cd "$(dirname "$0")/.." && pwd)"
|
||||||
# shellcheck source=env.sh
|
# shellcheck source=env.sh
|
||||||
source "${repo_root}/scripts/env.sh"
|
source "${repo_root}/scripts/env.sh"
|
||||||
|
# shellcheck source=lib/nix-eval.sh
|
||||||
|
source "${repo_root}/scripts/lib/nix-eval.sh"
|
||||||
|
|
||||||
sync_keys="${repo_root}/scripts/sync-host-keys.sh"
|
sync_keys="${repo_root}/scripts/sync-host-keys.sh"
|
||||||
|
|
||||||
@@ -240,15 +242,21 @@ platform_prefix="lxc"
|
|||||||
[[ -z "$memory" ]] && memory="$PROXMOX_DEFAULT_MEMORY_MB"
|
[[ -z "$memory" ]] && memory="$PROXMOX_DEFAULT_MEMORY_MB"
|
||||||
|
|
||||||
# --- discover / resolve the flake target from --host --------------------
|
# --- discover / resolve the flake target from --host --------------------
|
||||||
|
# Emits "<target>\t<hostName>" pairs for every ${platform_prefix}-* flake
|
||||||
|
# target -- the one source both --list and the --host lookup below read
|
||||||
|
# from, so they can never see a different set of targets from each other.
|
||||||
|
targets_for_platform() {
|
||||||
|
local target
|
||||||
|
for target in $(list_flake_targets "$repo_root" 2>/dev/null | grep -- "^${platform_prefix}-"); do
|
||||||
|
printf '%s\t%s\n' "$target" "$(flake_target_hostname "$repo_root" "$target")"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
list_hosts() {
|
list_hosts() {
|
||||||
local target hostname
|
local target hostname
|
||||||
for target in $(nix eval --json --no-use-registries --no-accept-flake-config \
|
while IFS=$'\t' read -r target hostname; do
|
||||||
"${repo_root}#nixosConfigurations" --apply builtins.attrNames 2>/dev/null \
|
|
||||||
| jq -r --arg p "${platform_prefix}-" '.[] | select(startswith($p))'); do
|
|
||||||
hostname="$(nix eval --raw --no-use-registries --no-accept-flake-config \
|
|
||||||
"${repo_root}#nixosConfigurations.${target}.config.networking.hostName" 2>/dev/null)"
|
|
||||||
printf ' %-12s -> %s\n' "$hostname" "$target"
|
printf ' %-12s -> %s\n' "$hostname" "$target"
|
||||||
done
|
done < <(targets_for_platform)
|
||||||
}
|
}
|
||||||
|
|
||||||
if [[ "$do_list" -eq 1 ]]; then
|
if [[ "$do_list" -eq 1 ]]; then
|
||||||
@@ -263,16 +271,12 @@ if [[ -z "$host" ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
flake_target=""
|
flake_target=""
|
||||||
for target in $(nix eval --json --no-use-registries --no-accept-flake-config \
|
while IFS=$'\t' read -r target hostname; do
|
||||||
"${repo_root}#nixosConfigurations" --apply builtins.attrNames \
|
if [[ "$hostname" == "$host" ]]; then
|
||||||
| jq -r --arg p "${platform_prefix}-" '.[] | select(startswith($p))'); do
|
|
||||||
hn="$(nix eval --raw --no-use-registries --no-accept-flake-config \
|
|
||||||
"${repo_root}#nixosConfigurations.${target}.config.networking.hostName")"
|
|
||||||
if [[ "$hn" == "$host" ]]; then
|
|
||||||
flake_target="$target"
|
flake_target="$target"
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
done
|
done < <(targets_for_platform)
|
||||||
|
|
||||||
if [[ -z "$flake_target" ]]; then
|
if [[ -z "$flake_target" ]]; then
|
||||||
echo "ERROR: no ${platform_prefix}-* target has hostName '${host}'." >&2
|
echo "ERROR: no ${platform_prefix}-* target has hostName '${host}'." >&2
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Shared Nix bootstrap for scripts/codex-setup.sh and
|
||||||
|
# scripts/codex-maintenance.sh: the nix.conf settings both need in effect
|
||||||
|
# before a single `nix` command runs (flakes enabled, never honor a flake
|
||||||
|
# input's own nixConfig, no "dirty tree" warning spam), plus a helper to
|
||||||
|
# pull an already-installed Nix's daemon/profile script onto PATH if it
|
||||||
|
# isn't there yet. Source this instead of copying it -- see CLAUDE.md.
|
||||||
|
export NIX_CONFIG="${NIX_CONFIG:-}
|
||||||
|
experimental-features = nix-command flakes
|
||||||
|
accept-flake-config = false
|
||||||
|
warn-dirty = false
|
||||||
|
"
|
||||||
|
|
||||||
|
ensure_nix_profile() {
|
||||||
|
if [ -f /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ]; then
|
||||||
|
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
|
||||||
|
elif [ -f "$HOME/.nix-profile/etc/profile.d/nix.sh" ]; then
|
||||||
|
. "$HOME/.nix-profile/etc/profile.d/nix.sh"
|
||||||
|
fi
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Shared flake-introspection helpers for scripts/*.sh. Source alongside
|
||||||
|
# env.sh:
|
||||||
|
# source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib/nix-eval.sh"
|
||||||
|
#
|
||||||
|
# NIX_EVAL_FLAGS: --no-use-registries so a call here never resolves through
|
||||||
|
# the user's global flake registry (every call targets this repo's own
|
||||||
|
# flake, or an explicit github: ref, not a registry alias); --no-accept-flake-config
|
||||||
|
# so a flake input's own nixConfig (e.g. a dependency's substituters) is
|
||||||
|
# never honored -- matches accept-flake-config = false already set repo-wide
|
||||||
|
# (see lib/nix-bootstrap.sh / CLAUDE.md). Reuse this array rather than
|
||||||
|
# retyping the two flags at each call site.
|
||||||
|
declare -a NIX_EVAL_FLAGS=(--no-use-registries --no-accept-flake-config)
|
||||||
|
|
||||||
|
# list_flake_targets <flake_ref>
|
||||||
|
# Prints the attribute names under <flake_ref>#nixosConfigurations, one per
|
||||||
|
# line, e.g.:
|
||||||
|
# list_flake_targets . # from inside the repo
|
||||||
|
# list_flake_targets "$repo_root" # from anywhere
|
||||||
|
list_flake_targets() {
|
||||||
|
local flake_ref="$1"
|
||||||
|
nix eval --json "${NIX_EVAL_FLAGS[@]}" \
|
||||||
|
"${flake_ref}#nixosConfigurations" --apply builtins.attrNames \
|
||||||
|
| jq -r '.[]'
|
||||||
|
}
|
||||||
|
|
||||||
|
# flake_target_hostname <flake_ref> <target>
|
||||||
|
# Prints one nixosConfigurations target's config.networking.hostName.
|
||||||
|
# Empty (not an error under set -e) if the target doesn't exist or the
|
||||||
|
# eval otherwise fails -- callers that need to distinguish "empty" from
|
||||||
|
# "eval failed" should check $? themselves instead of relying on this.
|
||||||
|
flake_target_hostname() {
|
||||||
|
local flake_ref="$1" target="$2"
|
||||||
|
nix eval --raw "${NIX_EVAL_FLAGS[@]}" \
|
||||||
|
"${flake_ref}#nixosConfigurations.${target}.config.networking.hostName" 2>/dev/null
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Shared SSH-host-key / age-conversion helpers for scripts/sync-host-keys.sh
|
||||||
|
# and scripts/prepare-host-key.sh -- both generate the same kind of key
|
||||||
|
# (ed25519, no passphrase, the sops-nix age-derivation input) and convert it
|
||||||
|
# to an age recipient the same way; kept in one place so the two can't
|
||||||
|
# drift apart.
|
||||||
|
#
|
||||||
|
# Uses NIX_OPTS (an array of extra `nix-shell` options -- see env.sh's
|
||||||
|
# nix_extra_opts) if the caller has already set it, so a decision to avoid
|
||||||
|
# an unreachable nix-cache is reused here instead of probed again. Falls
|
||||||
|
# back to no extra options if the caller never sourced env.sh.
|
||||||
|
if ! declare -p NIX_OPTS >/dev/null 2>&1; then
|
||||||
|
declare -a NIX_OPTS=()
|
||||||
|
fi
|
||||||
|
|
||||||
|
# generate_host_ed25519_key <hostname> <keyfile>
|
||||||
|
# Writes <keyfile> and <keyfile>.pub. Caller is responsible for refusing to
|
||||||
|
# overwrite an existing keyfile -- this always runs ssh-keygen fresh.
|
||||||
|
generate_host_ed25519_key() {
|
||||||
|
local hostname="$1" keyfile="$2"
|
||||||
|
nix-shell "${NIX_OPTS[@]}" -p openssh --run \
|
||||||
|
"ssh-keygen -t ed25519 -N '' -C '${hostname}' -f '${keyfile}'" >/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
|
# ssh_pubkey_to_age <pubkeyfile>
|
||||||
|
# Prints the age public key derived from an ed25519 SSH public key file.
|
||||||
|
ssh_pubkey_to_age() {
|
||||||
|
local pubkeyfile="$1"
|
||||||
|
nix-shell "${NIX_OPTS[@]}" -p ssh-to-age --run "ssh-to-age -i '${pubkeyfile}'"
|
||||||
|
}
|
||||||
@@ -23,6 +23,10 @@
|
|||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
repo_root="$(cd "$(dirname "$0")/.." && pwd)"
|
repo_root="$(cd "$(dirname "$0")/.." && pwd)"
|
||||||
|
# shellcheck source=env.sh
|
||||||
|
source "${repo_root}/scripts/env.sh"
|
||||||
|
# shellcheck source=lib/ssh-host-keys.sh
|
||||||
|
source "${repo_root}/scripts/lib/ssh-host-keys.sh"
|
||||||
|
|
||||||
hostname="${1:?usage: scripts/prepare-host-key.sh <hostname>}"
|
hostname="${1:?usage: scripts/prepare-host-key.sh <hostname>}"
|
||||||
sops_yaml="${repo_root}/.sops.yaml"
|
sops_yaml="${repo_root}/.sops.yaml"
|
||||||
@@ -41,9 +45,10 @@ if [[ -f "$keyfile" ]]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
nix-shell -p openssh --run "ssh-keygen -t ed25519 -N '' -C '${hostname}' -f '${keyfile}'" >/dev/null
|
nix_extra_opts
|
||||||
|
generate_host_ed25519_key "$hostname" "$keyfile"
|
||||||
|
|
||||||
age_pub="$(nix-shell -p ssh-to-age --run "ssh-to-age -i '${keyfile}.pub'")"
|
age_pub="$(ssh_pubkey_to_age "${keyfile}.pub")"
|
||||||
|
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,10 @@ editor="${repo_root}/scripts/lib/sync-host-keys-edit-sops.py"
|
|||||||
|
|
||||||
# shellcheck source=env.sh
|
# shellcheck source=env.sh
|
||||||
source "${repo_root}/scripts/env.sh"
|
source "${repo_root}/scripts/env.sh"
|
||||||
|
# shellcheck source=lib/nix-eval.sh
|
||||||
|
source "${repo_root}/scripts/lib/nix-eval.sh"
|
||||||
|
# shellcheck source=lib/ssh-host-keys.sh
|
||||||
|
source "${repo_root}/scripts/lib/ssh-host-keys.sh"
|
||||||
|
|
||||||
mkdir -p "$keydir"
|
mkdir -p "$keydir"
|
||||||
|
|
||||||
@@ -118,12 +122,10 @@ EOF
|
|||||||
}
|
}
|
||||||
|
|
||||||
discover_targets() {
|
discover_targets() {
|
||||||
nix eval --json --no-use-registries --no-accept-flake-config \
|
|
||||||
"${repo_root}#nixosConfigurations" --apply builtins.attrNames \
|
|
||||||
| jq -r '.[] | select(. != "installer")'
|
|
||||||
# installer is the one nixosConfigurations target that doesn't import
|
# installer is the one nixosConfigurations target that doesn't import
|
||||||
# sops-nix at all (see CLAUDE.md's "Security Notes" -- hardcoded login
|
# sops-nix at all (see CLAUDE.md's "Security Notes" -- hardcoded login
|
||||||
# password instead) -- config.sops.secrets doesn't exist for it.
|
# password instead) -- config.sops.secrets doesn't exist for it.
|
||||||
|
list_flake_targets "$repo_root" | grep -v '^installer$'
|
||||||
}
|
}
|
||||||
|
|
||||||
locally_managed_hosts() {
|
locally_managed_hosts() {
|
||||||
@@ -159,7 +161,7 @@ queue_host_sync() {
|
|||||||
echo "[dry-run] ${host}: would generate host key"
|
echo "[dry-run] ${host}: would generate host key"
|
||||||
else
|
else
|
||||||
echo "==> ${host}: generating host key"
|
echo "==> ${host}: generating host key"
|
||||||
nix-shell "${NIX_OPTS[@]}" -p openssh --run "ssh-keygen -t ed25519 -N '' -C '${host}' -f '${keyfile}'" >/dev/null
|
generate_host_ed25519_key "$host" "$keyfile"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo "==> ${host}: host key already present"
|
echo "==> ${host}: host key already present"
|
||||||
@@ -170,7 +172,7 @@ queue_host_sync() {
|
|||||||
if [[ "$dry_run" -eq 1 ]]; then
|
if [[ "$dry_run" -eq 1 ]]; then
|
||||||
age_pub="dry-run-placeholder-not-a-real-key"
|
age_pub="dry-run-placeholder-not-a-real-key"
|
||||||
else
|
else
|
||||||
age_pub="$(nix-shell "${NIX_OPTS[@]}" -p ssh-to-age --run "ssh-to-age -i '${keyfile}.pub'")"
|
age_pub="$(ssh_pubkey_to_age "${keyfile}.pub")"
|
||||||
fi
|
fi
|
||||||
add_keys_json="$(jq --arg host "$host" --arg key "$age_pub" \
|
add_keys_json="$(jq --arg host "$host" --arg key "$age_pub" \
|
||||||
'. + [{host: $host, age_key: $key}]' <<<"$add_keys_json")"
|
'. + [{host: $host, age_key: $key}]' <<<"$add_keys_json")"
|
||||||
|
|||||||
Reference in new issue
Block a user