#!/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 # Writes and .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 # 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}'" }