set -euo pipefail
die() { echo "rebuild: $*" >&2; exit 2; }
project='' release='' flake='' name='' key='' feed='' logkey='' policy='' files=()
while [ $# -gt 0 ]; do
case $1 in
--project) project=$2; shift 2 ;;
--release) release=$2; shift 2 ;;
--flake) flake=$2; shift 2 ;;
--name) name=$2; shift 2 ;;
--key) key=$2; shift 2 ;;
--feed) feed=$2; shift 2 ;;
--log-key) logkey=$2; shift 2 ;;
--witness-policy) policy=$2; shift 2 ;;
--file) files+=("$2"); shift 2 ;;
*) die "unknown argument $1 (see the top of $0)" ;;
esac
done
for v in project release flake name key feed; do
[ -n "${!v}" ] || die "--$v is required"
done
[ -f "$key" ] || die "no key at $key (cosign generate-key-pair)"
case $flake in *#*) ;; *) die "--flake needs an attribute: <ref>#<package>" ;; esac
case $flake in
path:*|.*|/*) echo "rebuild: warning: $flake is a local path; rebuild a commit or tag of the published repository, which others can rebuild too" >&2 ;;
esac
work=$(mktemp -d); trap 'rm -rf "$work"' EXIT
nixf=(--extra-experimental-features 'nix-command flakes')
echo "== building $flake"
out=$(nix "${nixf[@]}" build "$flake" --no-link --print-out-paths -L)
echo "== rebuilding it, to make sure the result is this machine's own"
nix "${nixf[@]}" build "$flake" --no-link --rebuild
if [ ${#files[@]} -eq 0 ]; then
while IFS= read -r f; do files+=("$f"); done < <(
find "$out" "$out/bin" -maxdepth 1 -type f ! -name '*.sha256' ! -name SHA256SUMS 2>/dev/null | LC_ALL=C sort)
else
files=("${files[@]/#/$out/}")
fi
[ ${#files[@]} -gt 0 ] || die "nothing to attest in $out"
echo "== attesting"
(cd "$(dirname "${files[0]}")" && sha256sum "${files[@]}")
dest=$feed/$project/$release/$name
mkdir -p "$dest"
quorum statement --release "$release" --out "$work/statement.json" "${files[@]}"
cosign signing-config create --out "$work/offline.json" >/dev/null
cosign attest-blob --statement "$work/statement.json" --key "$key" \
--signing-config "$work/offline.json" --bundle "$dest/attestation.sigstore.json" -y
echo "== wrote $dest/attestation.sigstore.json"
if [ -n "$logkey" ]; then
mkdir -p "$work/entries"
quorum log entry --rebuilder "$name" --release "$release" \
"$dest/attestation.sigstore.json" --out "$work/entries/0"
args=(--storage_dir="$feed/$project/log" --private_key="$logkey" --entries="$work/entries/*")
[ -z "$policy" ] || args+=(--witness_policy_file="$policy")
posix-oneshot "${args[@]}"
echo "== logged in $feed/$project/log"
fi