Files
nixos-4screen/scripts/04_nixos_recon.sh

111 lines
3.8 KiB
Bash

#!/bin/bash
#
# Phase 2: Deep System Reconnaissance for NixOS Migration
# This script gathers detailed information about installed software, services,
# configurations, and development environments on the current Ubuntu system.
# All output is logged to a file for later analysis.
# --- Configuration ---
LOG_FILE="logs/04_nixos_recon.log"
USER_HOME=$(eval echo ~${SUDO_USER:-$USER})
# --- Helper Functions ---
log() {
echo -e "$1" | tee -a "$LOG_FILE"
}
log_header() {
log "\n"
log "========================================================================"
log "=== $1"
log "========================================================================"
}
run_and_log() {
log "--- Running command: $1 ---"
eval "$1" 2>>"$LOG_FILE" | tee -a "$LOG_FILE"
log "--- Command finished ---\n"
}
# --- Main Execution ---
# Initialize log file
echo "NixOS Migration - Deep Reconnaissance Log - $(date)" > "$LOG_FILE"
echo "----------------------------------------------------" >> "$LOG_FILE"
log "User Home Directory: $USER_HOME"
# 1. Software Inventory (APT & Snap)
log_header "SOFTWARE INVENTORY"
if command -v dpkg &> /dev/null; then
run_and_log "dpkg --get-selections | grep -v deinstall"
else
log "dpkg command not found. Skipping APT package scan."
fi
if command -v snap &> /dev/null; then
run_and_log "snap list"
else
log "snap command not found. Skipping Snap package scan."
fi
# 2. Systemd Services & Timers
log_header "SYSTEMD SERVICES & TIMERS"
log "--- Active System Services ---"
run_and_log "systemctl list-units --type=service --state=running"
log "--- All System Timers ---"
run_and_log "systemctl list-timers --all"
log "\n--- Active User Services (if any) ---"
# Check for user session bus to run user commands
if [ -n "$XDG_RUNTIME_DIR" ]; then
run_and_log "systemctl --user list-units --type=service --state=running"
log "--- All User Timers (if any) ---"
run_and_log "systemctl --user list-timers --all"
else
log "Could not connect to user session bus. Skipping user services/timers."
fi
# 3. Docker Environment
log_header "DOCKER ENVIRONMENT"
if command -v docker &> /dev/null; then
run_and_log "docker --version"
run_and_log "docker info"
run_and_log "docker ps -a --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'"
run_and_log "docker images --format 'table {{.Repository}}\t{{.Tag}}\t{{.Size}}'"
run_and_log "docker volume ls"
log "--- Searching for docker-compose files in home directory ---"
run_and_log "find \"$USER_HOME\" -name \"*docker-compose.yml\" -o -name \"*compose.yml\" 2>/dev/null"
else
log "docker command not found. Skipping Docker scan."
fi
# 4. Command-Line Environment & Scripts
log_header "COMMAND-LINE TOOLS & SCRIPTS"
log "--- Top 50 Most Used Commands from History ---"
# This gives an idea of frequently used, un-packaged CLI tools
if [ -f "$USER_HOME/.bash_history" ]; then
run_and_log "cat $USER_HOME/.bash_history | sed 's/sudo //g' | awk '{print $1}' | sort | uniq -c | sort -rn | head -n 50"
elif [ -f "$USER_HOME/.zsh_history" ]; then
run_and_log "cat $USER_HOME/.zsh_history | sed 's/sudo //g' | awk '{print $1}' | sort | uniq -c | sort -rn | head -n 50"
else
log "Shell history file not found."
fi
log "--- User Cron Jobs (crontab) ---"
run_and_log "crontab -l"
log "--- Manually Installed Scripts & Binaries ---"
log "Searching in /usr/local/bin, ~/bin, and ~/.local/bin..."
run_and_log "ls -lA /usr/local/bin"
if [ -d "$USER_HOME/bin" ]; then
run_and_log "ls -lA \"$USER_HOME/bin\""
fi
if [ -d "$USER_HOME/.local/bin" ]; then
run_and_log "ls -lA \"$USER_HOME/.local/bin\""
fi
log_header "RECONNAISSANCE COMPLETE"
log "Log file saved to: $LOG_FILE"
log "This file provides a detailed snapshot of the system's software and configuration."
log "Review it carefully to plan your configuration.nix and home-manager setup."