Support early build cache hints

This commit is contained in:
Timothy J. Aveni
2026-07-02 11:21:53 -07:00
parent 837dea284c
commit f8c9ef03bf
2 changed files with 57 additions and 1 deletions
+31 -1
View File
@@ -75,7 +75,7 @@ write_config() {
wantedBy = [ "multi-user.target" ];
after = [ "fetch-ec2-metadata.service" "network-online.target" "nix-daemon.socket" ];
wants = [ "fetch-ec2-metadata.service" "network-online.target" "nix-daemon.socket" ];
path = [ pkgs.coreutils pkgs.git pkgs.systemd ];
path = [ pkgs.coreutils pkgs.git pkgs.jq pkgs.nix pkgs.systemd ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
@@ -102,11 +102,41 @@ write_config() {
install -m 0644 "$user_data" /etc/nixos/flake.nix
early_build_config=/run/nixos-user-data-flake-early-build-config.json
early_build_error=/run/nixos-user-data-flake-early-build-config.err
early_nix_config=/run/nixos-user-data-flake-early-nix.conf
: > "$early_nix_config"
if nix --extra-experimental-features 'nix-command flakes' \
eval --json /etc/nixos#quixosEarlyBuildNixConfig \
> "$early_build_config" 2> "$early_build_error"; then
jq -r '(.hosts // {}) | to_entries[] | select(.key != "" and (.value | length > 0)) | [.key, (.value | join(" "))] | @tsv' "$early_build_config" |
while IFS="$(printf '\t')" read -r address names; do
if [ -n "$address" ] && [ -n "$names" ]; then
printf '%s %s # quixos early build nix cache\n' "$address" "$names" >> /etc/hosts
fi
done
substituters="$(jq -r '(.substituters // []) | join(" ")' "$early_build_config")"
trusted_public_keys="$(jq -r '(.trustedPublicKeys // []) | join(" ")' "$early_build_config")"
if [ -n "$substituters" ]; then
printf 'extra-substituters = %s\n' "$substituters" >> "$early_nix_config"
fi
if [ -n "$trusted_public_keys" ]; then
printf 'extra-trusted-public-keys = %s\n' "$trusted_public_keys" >> "$early_nix_config"
fi
else
echo "No quixosEarlyBuildNixConfig flake output found; first switch will use the AMI Nix configuration."
fi
runner=/run/nixos-user-data-flake-apply
cat > "$runner" <<RUNNER_EOF
#!$${pkgs.runtimeShell}
set -eu
export PATH="$${lib.makeBinPath [ pkgs.coreutils pkgs.git pkgs.systemd ]}"
if [ -s "$early_nix_config" ]; then
export NIX_CONFIG="\$(cat "$early_nix_config")"
fi
$${config.system.build.nixos-rebuild}/bin/nixos-rebuild switch --flake /etc/nixos
printf '%s\n' "$new_hash" > "$applied_hash_file"