Files
NixOsConfig/pkgs/secret-from-file/secret-from-file.sh
T
Claude 7dd6853124 Normalize file modes to 644
All tracked files were committed as 755; only Nix reads them.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JZ16yf4acmaSiHq81vyQ6Z
2026-10-12 20:41:48 +00:00

167 lines
4.2 KiB
Bash

#!/usr/bin/env bash
# secret-from-file: encrypt an existing file (or stdin) as secrets/<name>.age
# for the recipients listed in secrets/secrets.nix. Refuses to overwrite
# without --force.
set -euo pipefail
FLAKE_ARG=
FORCE=0
IDENTITY=
NAME=
SOURCE=
if [ -t 2 ]; then
RED=$'\033[1;31m'
GRN=$'\033[1;32m'
RST=$'\033[0m'
else
RED=
GRN=
RST=
fi
ok() { printf '%s ok%s %s\n' "$GRN" "$RST" "$*" >&2; }
die() {
printf '%serror:%s %s\n' "$RED" "$RST" "$*" >&2
exit 1
}
usage() {
cat <<'EOF'
Usage: secret-from-file NAME.age SOURCE|- [options]
Encrypts SOURCE (a file, or - for stdin) to secrets/NAME.age for the public
keys that secrets/secrets.nix lists under "NAME.age".publicKeys.
-i, --identity FILE after encrypting, check that FILE (a private key that
is a recipient) can decrypt the result
--flake DIR flake directory (default: the repository you are in)
-f, --force replace an existing secrets/NAME.age
-h, --help show this help
Examples:
secret-from-file ssh-id_ed25519.age ~/.ssh/id_ed25519 -i ~/.ssh/id_ed25519
cp ~/.ssh/id_ed25519.pub secrets/ssh-id_ed25519.pub
EOF
}
while [ "$#" -gt 0 ]; do
case "$1" in
-i | --identity)
[ "$#" -ge 2 ] || die "$1 needs a value"
IDENTITY=$2
shift 2
;;
--flake)
[ "$#" -ge 2 ] || die "--flake needs a value"
FLAKE_ARG=$2
shift 2
;;
-f | --force)
FORCE=1
shift
;;
-h | --help)
usage
exit 0
;;
-)
if [ -z "$SOURCE" ]; then SOURCE=-; else die "unexpected argument: -"; fi
shift
;;
-*) die "unknown option: $1 (see --help)" ;;
*)
if [ -z "$NAME" ]; then
NAME=$1
elif [ -z "$SOURCE" ]; then
SOURCE=$1
else
die "unexpected argument: $1"
fi
shift
;;
esac
done
[ -n "$NAME" ] && [ -n "$SOURCE" ] || {
usage >&2
exit 2
}
case "$NAME" in
*.age) ;;
*) NAME=$NAME.age ;;
esac
# The name ends up inside a Nix expression: keep it boring.
case "$NAME" in
*[!A-Za-z0-9._-]* | .*) die "secret name may only contain letters, digits, '.', '_' and '-': $NAME" ;;
esac
if [ "$SOURCE" != - ] && [ ! -f "$SOURCE" ]; then
die "no such file: $SOURCE"
fi
if [ -n "$IDENTITY" ] && [ ! -f "$IDENTITY" ]; then
die "identity file not found: $IDENTITY"
fi
find_flake() {
local d
if [ -n "$FLAKE_ARG" ]; then
d=$FLAKE_ARG
elif d=$(git rev-parse --show-toplevel 2>/dev/null) && [ -f "$d/flake.nix" ]; then
:
elif [ -f "$PWD/flake.nix" ]; then
d=$PWD
else
die "no flake found: run inside the repository or pass --flake DIR"
fi
[ -f "$d/flake.nix" ] || die "no flake.nix in $d"
readlink -f "$d"
}
FLAKE=$(find_flake)
SECRETS="$FLAKE/secrets"
RULES="$SECRETS/secrets.nix"
OUT="$SECRETS/$NAME"
[ -f "$RULES" ] || die "$RULES not found"
if [ -e "$OUT" ] && [ "$FORCE" -eq 0 ]; then
die "$OUT already exists (use --force to replace it)"
fi
umask 077
WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT
if ! KEYS_JSON=$(nix --extra-experimental-features 'nix-command flakes' eval --json \
--file "$RULES" --apply "r: r.\"$NAME\".publicKeys" 2>"$WORK/eval.err"); then
cat "$WORK/eval.err" >&2
die "no rule for \"$NAME\" in $RULES. Add:
\"$NAME\".publicKeys = recipients;"
fi
if jq -e 'any(.[]; test("PLACEHOLDER"))' <<<"$KEYS_JSON" >/dev/null; then
die "$RULES still contains PLACEHOLDER keys: replace them first (create-keys prints the host key)"
fi
jq -r '.[]' <<<"$KEYS_JSON" >"$WORK/recipients"
[ -s "$WORK/recipients" ] || die "\"$NAME\" has no recipients in $RULES"
if [ "$SOURCE" = - ]; then
cat >"$WORK/plain"
SOURCE=$WORK/plain
fi
age --encrypt --armor -R "$WORK/recipients" -o "$WORK/out.age" "$SOURCE" ||
die "encryption failed: check that every key in $RULES is valid"
if [ -n "$IDENTITY" ]; then
age --decrypt -i "$IDENTITY" "$WORK/out.age" | cmp -s - "$SOURCE" ||
die "$IDENTITY cannot decrypt the result: it is not one of the recipients; nothing was written"
ok "verified with $IDENTITY"
fi
install -m 0644 "$WORK/out.age" "$OUT"
ok "wrote $OUT"
if git -C "$FLAKE" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
if git -C "$FLAKE" add -- "secrets/$NAME"; then
ok "git add secrets/$NAME (flakes only see tracked files)"
fi
fi