Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions arch/etc/NetworkManager/dispatcher.d/90-timezone
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/sh
set -eu

PATH=/usr/bin:/bin

IFACE="${1:-}"
ACTION="${2:-}"

case "$ACTION" in
up|connectivity-change) ;;
*) exit 0 ;;
esac

if [ "$IFACE" = "lo" ]; then
exit 0
fi

STAMP=/var/lib/nm-timezone/last-attempt
MIN_INTERVAL=600

# Rate limit on attempts, not successes, so a flapping link cannot hammer the API.
if [ -f "$STAMP" ]; then
age=$(( $(date +%s) - $(stat -c %Y "$STAMP") ))
if [ "$age" -lt "$MIN_INTERVAL" ]; then
exit 0
fi
fi
mkdir -p "$(dirname "$STAMP")"
: > "$STAMP"

lookup() {
curl -fsS --max-time 5 "$1" 2>/dev/null | tr -d '[:space:]'
}

tz=$(lookup https://ipapi.co/timezone) || tz=""
if [ -z "$tz" ]; then
tz=$(lookup https://ipinfo.io/timezone) || tz=""
fi

# Reject anything that is not a bare Region/City before it reaches the filesystem.
case "$tz" in
*/*) ;;
*) exit 0 ;;
esac
case "$tz" in
*[!A-Za-z0-9/_+-]* | /* | */ | */../*) exit 0 ;;
esac
if [ ! -f "/usr/share/zoneinfo/$tz" ]; then
exit 0
fi

current=$(timedatectl show -p Timezone --value)
if [ "$tz" = "$current" ]; then
exit 0
fi

timedatectl set-timezone "$tz"
logger -t nm-timezone "timezone $current -> $tz"
5 changes: 5 additions & 0 deletions arch/install-arch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ install_system_configs() {
"$DOTFILES/arch/etc/udev/rules.d/90-backlight-on-power.rules" \
/etc/udev/rules.d/90-backlight-on-power.rules
sudo udevadm control --reload-rules
# Follows the laptop across continents: resolves the public IP to a timezone
# on every NetworkManager connect and sets it only when it differs.
sudo install -D -m 0755 -o root -g root \
"$DOTFILES/arch/etc/NetworkManager/dispatcher.d/90-timezone" \
/etc/NetworkManager/dispatcher.d/90-timezone
sudo install -D -m 0644 -o root -g root \
"$DOTFILES/arch/etc/tmpfiles.d/mglru.conf" \
/etc/tmpfiles.d/mglru.conf
Expand Down
Loading