Refactor CI workflow to build and push NixOS schemes, replacing parallel builds with sequential processing for better cache management. Introduce a new script for pushing builds to Attic cache and remove the obsolete build-all-schemes script.

This commit is contained in:
Danilo Reyes 2025-10-02 22:28:15 -06:00
parent c238a15f31
commit 94fe046266
3 changed files with 167 additions and 56 deletions

View File

@ -35,38 +35,55 @@ jobs:
echo "schemes=$SCHEMES" >> $GITHUB_OUTPUT echo "schemes=$SCHEMES" >> $GITHUB_OUTPUT
echo "Available schemes: $SCHEMES" echo "Available schemes: $SCHEMES"
- name: Build all schemes
run: |
for scheme in ${{ steps.schemes.outputs.schemes }}; do
echo "Building scheme: $scheme"
nix build .#nixosConfigurations.${HOSTNAME}.config.system.build.toplevel \
--override-input stylix-scheme $scheme \
--out-link ./result-$scheme &
done
wait
echo "All schemes built successfully!"
- name: Configure Attic cache - name: Configure Attic cache
run: | run: |
# Configure attic client to use your cache server # Configure attic client to use your cache server
attic login servidos https://cache.servidos.lat ${{ secrets.ATTIC_TOKEN }} attic login servidos https://cache.servidos.lat ${{ secrets.ATTIC_TOKEN }}
- name: Push schemes to cache - name: Build and push all schemes
run: | run: |
echo "Pushing all scheme builds to cache..." echo "Building and pushing all schemes..."
# Push all built derivations to cache
if ls result* 1> /dev/null 2>&1; then
attic push servidos:nixos result*
fi
# Push each scheme build individually for better cache granularity # Store original scheme
ORIGINAL_SCHEME=$(grep -oP "scheme = schemesFile\.schemes\.\K\w+" config/stylix.nix)
echo "Original scheme: $ORIGINAL_SCHEME"
# Build and push each scheme
for scheme in ${{ steps.schemes.outputs.schemes }}; do for scheme in ${{ steps.schemes.outputs.schemes }}; do
echo "Pushing scheme $scheme to cache..." echo "========================================="
echo "Processing scheme: $scheme"
echo "========================================="
# Update stylix.nix to use this scheme
sed -i "s/scheme = schemesFile\.schemes\.\w\+;/scheme = schemesFile.schemes.$scheme;/" config/stylix.nix
# Verify the change
grep "scheme = schemesFile.schemes" config/stylix.nix
# Build the configuration
echo "Building $scheme..."
nix build .#nixosConfigurations.${HOSTNAME}.config.system.build.toplevel \
--out-link ./result-$scheme
# Push to cache
echo "Pushing $scheme to cache..."
attic push servidos:nixos ./result-$scheme
# Also push using print-out-paths for better cache coverage
nix build .#nixosConfigurations.${HOSTNAME}.config.system.build.toplevel \ nix build .#nixosConfigurations.${HOSTNAME}.config.system.build.toplevel \
--override-input stylix-scheme $scheme \
--print-out-paths | attic push servidos:nixos --stdin --print-out-paths | attic push servidos:nixos --stdin
echo "✓ Completed $scheme"
echo ""
done done
echo "All schemes pushed to cache successfully!"
# Restore original scheme
echo "Restoring original scheme: $ORIGINAL_SCHEME"
sed -i "s/scheme = schemesFile\.schemes\.\w\+;/scheme = schemesFile.schemes.$ORIGINAL_SCHEME;/" config/stylix.nix
echo "========================================="
echo "All schemes built and pushed successfully!"
echo "========================================="
- name: Upload build artifacts - name: Upload build artifacts
uses: actions/upload-artifact@v4 uses: actions/upload-artifact@v4
@ -77,7 +94,10 @@ jobs:
- name: Summary - name: Summary
run: | run: |
SCHEME_COUNT=$(echo "${{ steps.schemes.outputs.schemes }}" | wc -w)
echo "✅ Color scheme builds completed successfully!" echo "✅ Color scheme builds completed successfully!"
echo "- Built all ${{ steps.schemes.outputs.schemes }} schemes" echo "- Built $SCHEME_COUNT schemes: ${{ steps.schemes.outputs.schemes }}"
echo "- Pushed all builds to Atticd cache" echo "- Pushed all builds to Atticd cache"
echo "- Uploaded artifacts for backup" echo "- Uploaded artifacts for backup"
echo ""
echo "You can now switch schemes quickly without waiting for builds!"

View File

@ -1,34 +0,0 @@
{
pkgs,
inputs,
hostname ? "server",
}:
let
utils = import ./scheme-utils.nix { inherit pkgs inputs; };
schemes = utils.availableSchemes;
mkBuildScript =
scheme:
pkgs.writeShellScript "build-${scheme}" ''
echo "Building NixOS configuration with scheme: ${scheme}"
nix build .#nixosConfigurations.${hostname}.config.system.build.toplevel \
--override-input stylix-scheme ${scheme} \
--out-link ./result-${scheme}
echo "Build completed for scheme: ${scheme}"
'';
buildAllScript = pkgs.writeShellScript "build-all-schemes" ''
echo "Building all ${toString (builtins.length schemes)} schemes in parallel..."
echo "Schemes: ${builtins.concatStringsSep " " schemes}"
${builtins.concatStringsSep "\n" (map (scheme: "${mkBuildScript scheme} &") schemes)}
wait
echo "All schemes built successfully!"
echo "Results available in ./result-* directories"
'';
in
{
inherit schemes buildAllScript;
buildScripts = builtins.listToAttrs (
map (scheme: pkgs.lib.nameValuePair "build-${scheme}" (mkBuildScript scheme)) schemes
);
inherit (utils) lightSchemes darkSchemes availableColors;
}

125
scripts/push-to-cache.sh Executable file
View File

@ -0,0 +1,125 @@
#!/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}"