NixOS/scripts/push-to-cache.sh

126 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Script to push NixOS builds to Atticd cache
set -euo pipefail
# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Configuration
CACHE_NAME="servidos:nixos"
CACHE_URL="https://cache.servidos.lat"
echo -e "${BLUE}=========================================${NC}"
echo -e "${BLUE}NixOS Build Cache Pusher${NC}"
echo -e "${BLUE}=========================================${NC}"
echo ""
# Check if attic is available
if ! command -v attic &> /dev/null; then
echo -e "${YELLOW}Error: attic is not installed or not in PATH${NC}"
exit 1
fi
# Check if ATTIC_TOKEN is set
if [ -z "${ATTIC_TOKEN:-}" ]; then
echo -e "${YELLOW}Warning: ATTIC_TOKEN environment variable is not set${NC}"
echo "Please set it with: export ATTIC_TOKEN=your_token_here"
exit 1
fi
# Login to cache
echo -e "${GREEN}→ Logging into cache...${NC}"
attic login servidos "$CACHE_URL" "$ATTIC_TOKEN"
# Determine what to push
if [ $# -eq 0 ]; then
# No arguments - push all result* symlinks
if ls result* 1> /dev/null 2>&1; then
echo -e "${GREEN}→ Found result symlinks, pushing to cache...${NC}"
attic push "$CACHE_NAME" result*
echo -e "${GREEN}✓ Successfully pushed all results to cache!${NC}"
else
echo -e "${YELLOW}No result symlinks found. Build something first!${NC}"
echo ""
echo "Usage examples:"
echo " 1. Push all results: $0"
echo " 2. Push specific path: $0 /nix/store/xxxxx-nixos-system-xxx"
echo " 3. Push current config: $0 --current [hostname]"
exit 1
fi
elif [ "$1" = "--current" ]; then
# Push current system configuration
HOSTNAME="${2:-$(hostname)}"
echo -e "${GREEN}→ Building current configuration for $HOSTNAME...${NC}"
nix build ".#nixosConfigurations.$HOSTNAME.config.system.build.toplevel" --print-out-paths | \
attic push "$CACHE_NAME" --stdin
echo -e "${GREEN}✓ Successfully pushed $HOSTNAME configuration to cache!${NC}"
elif [ "$1" = "--all-schemes" ]; then
# Build and push all schemes
HOSTNAME="${2:-server}"
# Get current directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
cd "$PROJECT_ROOT"
# Get available schemes
SCHEMES=$(nix eval --raw --impure --expr '
let
pkgs = import <nixpkgs> {};
inputs = {};
utils = import ./scripts/scheme-utils.nix { inherit pkgs inputs; };
in
builtins.concatStringsSep " " utils.availableSchemes
')
echo -e "${GREEN}→ Building and pushing all schemes for $HOSTNAME...${NC}"
echo -e "Schemes: $SCHEMES"
echo ""
# Store original scheme
ORIGINAL_SCHEME=$(grep -oP "scheme = schemesFile\.schemes\.\K\w+" config/stylix.nix)
echo -e "Original scheme: $ORIGINAL_SCHEME"
echo ""
# Build and push each scheme
for scheme in $SCHEMES; do
echo -e "${BLUE}→ Processing $scheme...${NC}"
# Update stylix.nix
sed -i "s/scheme = schemesFile\.schemes\.\w\+;/scheme = schemesFile.schemes.$scheme;/" config/stylix.nix
# Build and push
nix build ".#nixosConfigurations.$HOSTNAME.config.system.build.toplevel" \
--out-link "./result-$scheme"
attic push "$CACHE_NAME" "./result-$scheme"
echo -e "${GREEN}✓ Completed $scheme${NC}"
echo ""
done
# Restore original scheme
echo -e "${BLUE}→ Restoring original scheme: $ORIGINAL_SCHEME${NC}"
sed -i "s/scheme = schemesFile\.schemes\.\w\+;/scheme = schemesFile.schemes.$ORIGINAL_SCHEME;/" config/stylix.nix
echo -e "${GREEN}✓ All schemes pushed successfully!${NC}"
else
# Push specific store path(s)
echo -e "${GREEN}→ Pushing specified path(s) to cache...${NC}"
for path in "$@"; do
echo " - $path"
echo "$path" | attic push "$CACHE_NAME" --stdin
done
echo -e "${GREEN}✓ Successfully pushed to cache!${NC}"
fi
echo ""
echo -e "${BLUE}=========================================${NC}"
echo -e "${GREEN}Cache push completed!${NC}"
echo -e "${BLUE}=========================================${NC}"