From 94fe0462669ec4e884ecec0341fc5c0a3c7ca6b4 Mon Sep 17 00:00:00 2001 From: Danilo Reyes Date: Thu, 2 Oct 2025 22:28:15 -0600 Subject: [PATCH] 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. --- .github/workflows/build-schemes.yml | 64 +++++++++----- scripts/build-all-schemes.nix | 34 -------- scripts/push-to-cache.sh | 125 ++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+), 56 deletions(-) delete mode 100644 scripts/build-all-schemes.nix create mode 100755 scripts/push-to-cache.sh diff --git a/.github/workflows/build-schemes.yml b/.github/workflows/build-schemes.yml index eb53e93..7c9a482 100644 --- a/.github/workflows/build-schemes.yml +++ b/.github/workflows/build-schemes.yml @@ -35,38 +35,55 @@ jobs: echo "schemes=$SCHEMES" >> $GITHUB_OUTPUT 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 run: | # Configure attic client to use your cache server attic login servidos https://cache.servidos.lat ${{ secrets.ATTIC_TOKEN }} - - name: Push schemes to cache + - name: Build and push all schemes run: | - echo "Pushing all scheme builds to cache..." - # Push all built derivations to cache - if ls result* 1> /dev/null 2>&1; then - attic push servidos:nixos result* - fi + echo "Building and pushing all schemes..." - # 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 - 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 \ - --override-input stylix-scheme $scheme \ --print-out-paths | attic push servidos:nixos --stdin + + echo "✓ Completed $scheme" + echo "" 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 uses: actions/upload-artifact@v4 @@ -77,7 +94,10 @@ jobs: - name: Summary run: | + SCHEME_COUNT=$(echo "${{ steps.schemes.outputs.schemes }}" | wc -w) 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 "- Uploaded artifacts for backup" + echo "" + echo "You can now switch schemes quickly without waiting for builds!" diff --git a/scripts/build-all-schemes.nix b/scripts/build-all-schemes.nix deleted file mode 100644 index c5ad377..0000000 --- a/scripts/build-all-schemes.nix +++ /dev/null @@ -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; -} diff --git a/scripts/push-to-cache.sh b/scripts/push-to-cache.sh new file mode 100755 index 0000000..2474eb9 --- /dev/null +++ b/scripts/push-to-cache.sh @@ -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 {}; + 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}" +