testsAndMisc/linux_configuration/hosts/guard/install_pacman_hooks.sh
Krzysztof kuhy Rudnicki f84135f6d7 linux_configuration: WIP digital_wellbeing + pacman hosts-guard updates
Pre-existing local changes from a prior session, committed together for
cleanup. Touches:

- hosts/guard/: pacman pre-unlock / post-relock hook tweaks and the
  shared hosts-guard-common.sh helper.
- scripts/digital_wellbeing/: block_compulsive_opening, music_parallelism,
  setup_midnight_shutdown, setup_pc_startup_monitor refinements.
- scripts/digital_wellbeing/pacman/pacman_wrapper.sh: substantial rewrite.
- scripts/lib/common.sh: shared helpers expanded.
- tests/test_hosts_guard_pacman_integration.sh: new integration test.
2026-05-06 21:41:07 +02:00

51 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
require_root() { if [[ $EUID -ne 0 ]]; then exec sudo -E bash "$0" "$@"; fi; }
require_root "$@"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HOOKS_DIR="/etc/pacman.d/hooks"
install -d -m 755 "$HOOKS_DIR"
# Pre-transaction hook
cat > "$HOOKS_DIR/10-unlock-etc-hosts.hook" << 'HOOK'
[Trigger]
Operation = Upgrade
Operation = Install
Operation = Remove
Type = Package
Target = *
[Action]
Description = Temporarily unlocking /etc/hosts for transaction
When = PreTransaction
Exec = /bin/bash /usr/local/share/hosts-guard/pacman-pre-unlock-hosts.sh
NeedsTargets
HOOK
# Post-transaction hook
cat > "$HOOKS_DIR/90-relock-etc-hosts.hook" << 'HOOK'
[Trigger]
Operation = Upgrade
Operation = Install
Operation = Remove
Type = Package
Target = *
[Action]
Description = Re-locking /etc/hosts after transaction
When = PostTransaction
Exec = /bin/bash /usr/local/share/hosts-guard/pacman-post-relock-hosts.sh
NeedsTargets
HOOK
# Place helper scripts into a shared location
install -d -m 755 /usr/local/share/hosts-guard
install -m 755 "$SCRIPT_DIR/pacman-hooks/hosts-guard-common.sh" /usr/local/share/hosts-guard/
install -m 755 "$SCRIPT_DIR/pacman-hooks/pacman-pre-unlock-hosts.sh" /usr/local/share/hosts-guard/
install -m 755 "$SCRIPT_DIR/pacman-hooks/pacman-post-relock-hosts.sh" /usr/local/share/hosts-guard/
echo "Pacman hooks installed into $HOOKS_DIR."