grapevine/bin/nix-build-and-cache
Charles Hall 7fc39b1845 simplify attic call
The comment isn't really needed since the reasoning applies to the
other instances of readarray and here-strings too, and the IFS bit is
unnecessary.
2025-08-03 10:57:39 -07:00

69 lines
1.6 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
# Build and cache the specified arguments
just() {
if command -v nom &> /dev/null; then
nom build "$@"
else
nix build "$@"
fi
if [ -z ${ATTIC_TOKEN+x} ]; then
echo "\$ATTIC_TOKEN is unset, skipping uploading to the binary cache"
return
fi
nix run --inputs-from . attic#default -- \
login "$ATTIC_SERVER" "$ATTIC_ENDPOINT" "$ATTIC_TOKEN"
# Find all output paths of the installables and their build dependencies
readarray -t derivations < <(nix path-info --derivation "$@")
readarray -t cache < <(
xargs \
nix-store --query --requisites --include-outputs \
<<< "${derivations[*]}"
)
# Upload them to Attic. It seems to insist on newlines to separate the
# paths.
(
IFS=$'\n'
nix run --inputs-from . attic#default -- \
push --stdin --no-closure "$ATTIC_SERVER:$ATTIC_CACHE" \
<<< "${cache[*]}"
)
}
# Build and cache things needed for CI
ci() {
cache=(
--inputs-from .
# Keep sorted
".#devShells.x86_64-linux.default"
attic#default
nixpkgs#direnv
nixpkgs#jq
nixpkgs#nix-direnv
)
just "${cache[@]}"
}
# Build and cache all the package outputs
packages() {
readarray -t cache < <(
nix flake show --json 2> /dev/null |
nix run --inputs-from . nixpkgs#jq -- \
-r \
'.packages."x86_64-linux" | keys | map(".#" + .) | .[]'
)
just "${cache[@]}"
}
pushd "$(git rev-parse --show-toplevel)" > /dev/null
"$@"
popd > /dev/null