diff --git a/dot_config/systemd/user/chezmoi-boot-sync.service b/dot_config/systemd/user/chezmoi-boot-sync.service new file mode 100644 index 0000000..b297a6a --- /dev/null +++ b/dot_config/systemd/user/chezmoi-boot-sync.service @@ -0,0 +1,12 @@ +[Unit] +Description=Chezmoi: aplica config respaldada al iniciar sesión +After=network-online.target +Wants=network-online.target + +[Service] +Type=oneshot +ExecStart=%h/.local/bin/chezmoi-boot-sync.sh +TimeoutStartSec=60 + +[Install] +WantedBy=default.target diff --git a/dot_config/systemd/user/chezmoi-shutdown-sync.service b/dot_config/systemd/user/chezmoi-shutdown-sync.service new file mode 100644 index 0000000..99bc81b --- /dev/null +++ b/dot_config/systemd/user/chezmoi-shutdown-sync.service @@ -0,0 +1,14 @@ +[Unit] +Description=Chezmoi: respalda config al cerrar sesión/apagar +After=network-online.target +Wants=network-online.target + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/bin/true +ExecStop=%h/.local/bin/chezmoi-shutdown-sync.sh +TimeoutStopSec=30 + +[Install] +WantedBy=default.target diff --git a/dot_config/systemd/user/default.target.wants/symlink_chezmoi-boot-sync.service b/dot_config/systemd/user/default.target.wants/symlink_chezmoi-boot-sync.service new file mode 100644 index 0000000..12a2b98 --- /dev/null +++ b/dot_config/systemd/user/default.target.wants/symlink_chezmoi-boot-sync.service @@ -0,0 +1 @@ +/home/VampireJSV/.config/systemd/user/chezmoi-boot-sync.service diff --git a/dot_config/systemd/user/default.target.wants/symlink_chezmoi-shutdown-sync.service b/dot_config/systemd/user/default.target.wants/symlink_chezmoi-shutdown-sync.service new file mode 100644 index 0000000..623a3a9 --- /dev/null +++ b/dot_config/systemd/user/default.target.wants/symlink_chezmoi-shutdown-sync.service @@ -0,0 +1 @@ +/home/VampireJSV/.config/systemd/user/chezmoi-shutdown-sync.service diff --git a/dot_local/bin/executable_chezmoi-boot-sync.sh b/dot_local/bin/executable_chezmoi-boot-sync.sh new file mode 100644 index 0000000..20581a2 --- /dev/null +++ b/dot_local/bin/executable_chezmoi-boot-sync.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# Aplica al arrancar la sesión la config respaldada en chezmoi. +# No debe bloquear nunca el arranque: cualquier fallo (sin red, conflicto) +# se registra y se continúa con el estado local tal cual está. +set -uo pipefail + +LOG="$HOME/.local/share/chezmoi-sync.log" +mkdir -p "$(dirname "$LOG")" +exec >>"$LOG" 2>&1 + +echo "=== boot-sync $(date -Iseconds) ===" + +if ! timeout 15 git -C "$HOME/.local/share/chezmoi" ls-remote --exit-code origin >/dev/null 2>&1; then + echo "sin conexión con el remoto, me quedo con el estado local" + exit 0 +fi + +if timeout 30 chezmoi update --force; then + echo "chezmoi update OK" +else + echo "chezmoi update falló (ver arriba), me quedo con el estado local" +fi diff --git a/dot_local/bin/executable_chezmoi-shutdown-sync.sh b/dot_local/bin/executable_chezmoi-shutdown-sync.sh new file mode 100644 index 0000000..8168e25 --- /dev/null +++ b/dot_local/bin/executable_chezmoi-shutdown-sync.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# Respalda al cerrar sesión/apagar todo lo que chezmoi ya tiene rastreado. +# Solo re-sincroniza rutas YA gestionadas (chezmoi re-add), nunca añade +# rutas nuevas por su cuenta -- eso evita que algo sensible se cuele solo. +set -uo pipefail + +LOG="$HOME/.local/share/chezmoi-sync.log" +mkdir -p "$(dirname "$LOG")" +exec >>"$LOG" 2>&1 + +echo "=== shutdown-sync $(date -Iseconds) ===" + +SRC="$HOME/.local/share/chezmoi" +cd "$SRC" || exit 0 + +chezmoi re-add +git add -A + +if git diff --cached --quiet; then + echo "sin cambios que respaldar" + exit 0 +fi + +git commit -m "Auto-backup $(date '+%Y-%m-%d %H:%M')" + +if timeout 20 git push origin main; then + echo "push OK" +else + echo "push falló (¿sin red?), el commit queda en local para el próximo intento" +fi diff --git a/run_once_after_enable-chezmoi-sync-linger.sh b/run_once_after_enable-chezmoi-sync-linger.sh new file mode 100755 index 0000000..7d9aa46 --- /dev/null +++ b/run_once_after_enable-chezmoi-sync-linger.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# Se ejecuta una sola vez tras `chezmoi apply`/`chezmoi init --apply` en una +# máquina nueva. Habilita "linger" para que la instancia systemd --user +# arranque con el sistema (no solo al hacer login) y sobreviva al logout, +# necesario para que chezmoi-boot-sync/chezmoi-shutdown-sync se disparen +# en el arranque/apagado reales y no solo en la sesión gráfica. +set -euo pipefail + +if command -v loginctl >/dev/null 2>&1; then + loginctl enable-linger "$USER" || true +fi