Commit 96a402bc722a

Vincent Demeester <vincent@sbr.pm>
2026-01-13 10:14:17
feat(nixpkgs-consolidate): auto-detect nixpkgs revision from home flake
Add automatic detection of nixpkgs revision from home flake.lock to use as the consolidation base branch. This ensures the consolidated branch is based on the exact same nixpkgs commit as the home configuration, making testing more reliable. Features: - Extracts nixpkgs revision from $HOME_FLAKE_PATH/flake.lock using jq - Falls back to nix flake metadata if jq extraction fails - Falls back to configured base branch if detection fails - Specifically targets main "nixpkgs" input (not nixpkgs-master, etc.) New environment variable: - HOME_FLAKE_PATH: Path to home flake (default: ~/src/home) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 41d8519
Changed files (2)
tools
tools/nixpkgs-consolidate/nixpkgs-consolidate.sh
@@ -14,6 +14,7 @@ NIXPKGS_FORK_URL="${NIXPKGS_FORK_URL:-git@github.com:vdemeester/nixpkgs.git}"
 NIXPKGS_UPSTREAM_URL="${NIXPKGS_UPSTREAM_URL:-https://github.com/NixOS/nixpkgs.git}"
 NIXPKGS_CONSOLIDATED_BRANCH="${NIXPKGS_CONSOLIDATED_BRANCH:-wip-consolidated}"
 NIXPKGS_CONFIG_FILE="${NIXPKGS_CONFIG_FILE:-$HOME/.config/nixpkgs-automation/branches.conf}"
+HOME_FLAKE_PATH="${HOME_FLAKE_PATH:-/home/vincent/src/home}"
 NTFY_SERVER="${NTFY_SERVER:-https://ntfy.sbr.pm}"
 NTFY_TOPIC="${NTFY_TOPIC:-git-builds}"
 NTFY_TOKEN_FILE="${NTFY_TOKEN_FILE:-/run/agenix/ntfy-token}"
@@ -90,6 +91,39 @@ Logs: $LOG_FILE"
 
 trap cleanup EXIT
 
+detect_home_nixpkgs_rev() {
+  if [ ! -f "$HOME_FLAKE_PATH/flake.lock" ]; then
+    return 1
+  fi
+
+  # Try to extract the main "nixpkgs" input revision from flake.lock using jq
+  # This specifically targets the "nixpkgs" node which should track nixos-unstable
+  if command -v jq >/dev/null 2>&1; then
+    local rev
+    # Get the revision for the nixpkgs node (not nixpkgs-master, nixpkgs-wip-consolidated, etc.)
+    rev=$(jq -r '.nodes.nixpkgs.locked.rev // empty' "$HOME_FLAKE_PATH/flake.lock" 2>/dev/null)
+
+    if [ -n "$rev" ]; then
+      echo "$rev"
+      return 0
+    fi
+  fi
+
+  # Fallback: try nix flake metadata (slower but more reliable)
+  if command -v nix >/dev/null 2>&1; then
+    local rev
+    rev=$(nix flake metadata "$HOME_FLAKE_PATH" --json 2>/dev/null | \
+          jq -r '.locks.nodes.nixpkgs.locked.rev // empty' 2>/dev/null)
+
+    if [ -n "$rev" ]; then
+      echo "$rev"
+      return 0
+    fi
+  fi
+
+  return 1
+}
+
 parse_config() {
   log "Reading configuration from $NIXPKGS_CONFIG_FILE"
 
@@ -109,7 +143,7 @@ parse_config() {
     # Extract base branch (line starting with "base:")
     if [[ "$line" =~ ^base:[[:space:]]*(.*) ]]; then
       BASE_BRANCH="${BASH_REMATCH[1]}"
-      log "Base branch: $BASE_BRANCH"
+      log "Base branch from config: $BASE_BRANCH"
       continue
     fi
 
@@ -126,6 +160,18 @@ parse_config() {
     exit 1
   fi
 
+  # Try to detect and use home flake nixpkgs revision
+  log "Attempting to detect nixpkgs revision from home flake at $HOME_FLAKE_PATH"
+  local detected_rev
+  if detected_rev=$(detect_home_nixpkgs_rev); then
+    log "✓ Detected nixpkgs revision from home flake: $detected_rev"
+    log "Using detected revision as base (overriding config: $BASE_BRANCH)"
+    BASE_BRANCH="$detected_rev"
+  else
+    log "Could not detect nixpkgs revision from home flake"
+    log "Using configured base branch: $BASE_BRANCH"
+  fi
+
   if [ ${#WIP_BRANCHES[@]} -eq 0 ]; then
     log "WARNING: No WIP branches defined in config"
     log "Nothing to consolidate, exiting"
tools/nixpkgs-consolidate/README.md
@@ -67,6 +67,33 @@ Add to your system configuration (e.g., `systems/aomi/extra.nix`):
 
 ## Configuration
 
+### Automatic Base Branch Detection
+
+**NEW:** The script automatically detects the nixpkgs revision from your home flake!
+
+When running, the script will:
+1. Read the configured base branch from `branches.conf` (e.g., `nixos-unstable`)
+2. Check your home flake at `$HOME_FLAKE_PATH/flake.lock`
+3. Extract the exact nixpkgs commit hash from the `nixpkgs` input
+4. Use that commit hash as the base instead of the configured branch
+
+**Benefits:**
+- Consolidated branch is based on the exact same nixpkgs as your home configuration
+- No manual hash updates needed - it follows your flake.lock
+- More reliable testing of packages in your actual NixOS environment
+- Falls back to configured base if detection fails
+
+**Example:**
+```
+# branches.conf contains:
+base: nixos-unstable
+
+# But your flake.lock has nixpkgs pinned to:
+31926ce9afb1c915fa4190b77ca9be389ccaf18e
+
+# Script will use the commit hash from flake.lock as the base
+```
+
 ### Setup
 
 1. **Create configuration directory:**
@@ -118,6 +145,7 @@ Override defaults with environment variables:
 | `NIXPKGS_FORK_REMOTE` | `origin` | Your fork remote name |
 | `NIXPKGS_CONSOLIDATED_BRANCH` | `wip-consolidated` | Output branch name |
 | `NIXPKGS_CONFIG_FILE` | `~/.config/nixpkgs-automation/branches.conf` | Config file path |
+| `HOME_FLAKE_PATH` | `~/src/home` | Path to home flake for auto-detecting nixpkgs revision |
 | `NTFY_SERVER` | `https://ntfy.sbr.pm` | ntfy server URL |
 | `NTFY_TOPIC` | `git-builds` | ntfy notification topic |
 | `NTFY_TOKEN_FILE` | `/run/agenix/ntfy-token` | ntfy auth token file |