#!/bin/bash # Function to check if a package is installed is_installed() { pacman -Qi "$1" &> /dev/null } # Function to check if a package is installed is_group_installed() { pacman -Qg "$1" &> /dev/null } # Packages and Flatpaks you don't want, one name per line (# starts a comment). # install_packages and install-flatpaks.sh leave them out, so update doesn't # put back what you've removed. SKIP_PACKAGES="$HOME/.config/coredesktop/skip-packages" # Print each argument that isn't in SKIP_PACKAGES, one per line without_skipped() { local -A skip=() local name if [ -f "$SKIP_PACKAGES" ]; then while read -r name _; do [[ -n "$name" && "$name" != \#* ]] && skip["$name"]=1 done < "$SKIP_PACKAGES" fi for name in "$@"; do [ -n "${skip[$name]}" ] || echo "$name" done } # Function to install packages if not already installed install_packages() { local packages=() local to_install=() local as_deps=() mapfile -t packages < <(without_skipped "$@") # pacman -Qdq with no names would list every dependency on the system [ ${#packages[@]} -ne 0 ] || return 0 for pkg in "${packages[@]}"; do if ! is_installed "$pkg" && ! is_group_installed "$pkg"; then to_install+=("$pkg") fi done # A package first installed as another one's dependency becomes an orphan # when that one is removed, and updates.d/95-cleanup.sh removes orphans mapfile -t as_deps < <(pacman -Qdq "${packages[@]}" 2> /dev/null) if [ ${#as_deps[@]} -ne 0 ]; then sudo pacman -D --asexplicit "${as_deps[@]}" > /dev/null fi if [ ${#to_install[@]} -ne 0 ]; then echo "Installing: ${to_install[*]}" yay -S --noconfirm --answerdiff=None --answeredit=None --answerclean=None "${to_install[@]}" fi } FEDERATED_REPO_URL="https://gitea.federated.computer/api/packages/Federated_Computer/arch" FEDERATED_REPO_KEY="6AB7C080D87C74BEE3925A026329376994F18B4E" # Add Federated Computer's pacman repo, which Gitea serves and signs, and trust # its key. Does nothing if both are already set up. ensure_federated_repo() { if ! sudo pacman-key --list-keys "$FEDERATED_REPO_KEY" &> /dev/null; then curl -fsSL "$FEDERATED_REPO_URL/repository.key" | sudo pacman-key --add - || return 1 sudo pacman-key --lsign-key "$FEDERATED_REPO_KEY" > /dev/null || return 1 fi if ! grep -q '^\[federated\]' /etc/pacman.conf; then printf '\n[federated]\nSigLevel = Required DatabaseRequired\nServer = %s/$repo/$arch\n' \ "$FEDERATED_REPO_URL" | sudo tee -a /etc/pacman.conf > /dev/null || return 1 fi } # Function to install pipx applications if not already installed install_pipx_apps() { local installed app status=0 installed="$(pipx list --short 2> /dev/null | cut -d' ' -f1)" for app in "$@"; do if ! grep -qx "$app" <<< "$installed"; then echo "Installing $app with pipx..." pipx install "$app" || status=1 fi done return $status } # Reinstall gext if missing or broken. Its pipx venv goes stale when Arch # upgrades Python, and migrations run before updates.d/60-pipx.sh rebuilds it. ensure_gext() { ~/.local/bin/gext --version &> /dev/null && return 0 echo "gext missing or broken, reinstalling..." pipx uninstall gnome-extensions-cli &> /dev/null || true pipx install --python "$(which python3)" gnome-extensions-cli --system-site-packages } # Turn on the GNOME extension $1 for this user by adding it to # enabled-extensions, keeping the others. Unlike gnome-extensions enable, this # works for an extension the running shell hasn't found yet, such as one a # package has just installed; it loads at the next login. enable_gnome_extension() { local uuid="$1" enabled enabled="$(gsettings get org.gnome.shell enabled-extensions)" || return 1 [[ "$enabled" == *"'$uuid'"* ]] && return 0 enabled="${enabled#@as }" if [ "$enabled" = "[]" ]; then enabled="['$uuid']" else enabled="${enabled%]}, '$uuid']" fi gsettings set org.gnome.shell enabled-extensions "$enabled" } # Function to remove pacman/AUR packages if installed remove_packages() { local packages=("$@") local to_remove=() # Match the exact name: pacman -Q also finds a package that provides it, such # as herdr-bin for herdr, which pacman -R won't remove by that name # An AUR package built with makepkg's debug option comes with pkg-debug, # which nothing depends on, so -Rns leaves it behind, and its files conflict # with the replacement's (voxtype-debug with voxtype-bin-debug) for pkg in "${packages[@]}"; do if [ "$(pacman -Qq "$pkg" 2> /dev/null)" = "$pkg" ]; then to_remove+=("$pkg") fi if [ "$(pacman -Qq "$pkg-debug" 2> /dev/null)" = "$pkg-debug" ]; then to_remove+=("$pkg-debug") fi done if [ ${#to_remove[@]} -ne 0 ]; then echo "Removing: ${to_remove[*]}" sudo pacman -Rns --noconfirm "${to_remove[@]}" fi } # Function to remove flatpak applications if installed remove_flatpaks() { local flatpaks=("$@") local to_remove=() for pak in "${flatpaks[@]}"; do if flatpak info --system "$pak" &> /dev/null; then to_remove+=("$pak") fi done if [ ${#to_remove[@]} -ne 0 ]; then echo "Removing flatpaks: ${to_remove[*]}" sudo flatpak uninstall --system --noninteractive "${to_remove[@]}" fi } # Function to enable systemd services if not already enabled # With --now, also start the services enabled here, after making systemd read # the unit files that packages just installed enable_services() { local now=() if [ "$1" = "--now" ]; then now=(--now) shift sudo systemctl daemon-reload fi for service in "$@"; do # audit records the services you turn off; leave those off if grep -qsx "system $service" "$HOME/.config/coredesktop/services-off"; then echo "$service is turned off (see audit)." elif ! systemctl is-enabled "$service" &> /dev/null; then echo "Enabling $service..." sudo systemctl enable "${now[@]}" "$service" else echo "$service is already enabled." fi done } # Run each script in migrations/ that hasn't run on this machine yet, oldest # first. A migration is recorded as done only when it succeeds, and the first # failure stops the run so a later migration never runs ahead of an earlier one. run_migrations() { local dir="${COREDESKTOP:-$HOME/.local/share/coredesktop}/migrations" local state="$HOME/.local/state/coredesktop/migrations" local file name mkdir -p "$state" for file in "$dir"/*.sh; do [ -f "$file" ] || continue name="$(basename "$file")" [ -f "$state/$name" ] && continue echo "Running migration $name..." if ! bash "$file"; then echo "Migration $name failed; it will run again on the next update." return 1 fi touch "$state/$name" done } # True if a newer kernel was installed since this one booted: the running # kernel's modules are gone, or are only kernel-modules-hook's copy of them, # which no package owns newer_kernel_installed() { local modules modules="${COREDESKTOP_MODULES_DIR:-/usr/lib/modules}/$(uname -r)" [ ! -d "$modules" ] || ! pacman -Qqo "$modules" &> /dev/null } # Output of the last update's pacman -Su, written by updates.d/10-pacman.sh PACMAN_UPDATE_LOG="$HOME/.local/state/coredesktop/pacman-update.log" # Lines that start a new initramfs build in the log: pacman's hook, a rebuild # by rebuild_initramfs, or a failure carried over by new_pacman_log INITRAMFS_BUILD_START='Updating linux initcpios|==> CoreDesktop:' # The log of the last initramfs build, if the log has one initramfs_build_log() { [ -f "$PACMAN_UPDATE_LOG" ] || return 1 awk -v start="$INITRAMFS_BUILD_START" '$0 ~ start { s = ""; found = 1 } found { s = s $0 "\n" } END { printf "%s", s; exit !found }' "$PACMAN_UPDATE_LOG" } # True if the last initramfs build (an update's, or rebuild_initramfs') failed. # Rebooting then can leave the machine unbootable. initramfs_build_failed() { local build built succeeded build="$(initramfs_build_log)" || return 1 grep -q "==> ERROR" <<< "$build" && return 0 # Every image built must report success. Older mkinitcpio says "==> Image # generation successful", newer says "==> Initcpio image generation successful". built="$(grep -c "==> Building image from preset" <<< "$build")" succeeded="$(grep -cE "==> (Initcpio i|I)mage generation successful" <<< "$build")" (( succeeded == 0 || succeeded < built )) } # Empty the pacman log for a new upgrade. A failed build in the old one is # carried over, so it's still rebuilt and checked if this upgrade builds none. new_pacman_log() { local failed="" initramfs_build_failed && failed=1 mkdir -p "$(dirname "$PACMAN_UPDATE_LOG")" : > "$PACMAN_UPDATE_LOG" if [ -n "$failed" ]; then echo "==> CoreDesktop: ==> ERROR: the initramfs failed to build last time" >> "$PACMAN_UPDATE_LOG" fi } # Build every initramfs again (mkinitcpio -P), adding the output to the log for # initramfs_build_failed rebuild_initramfs() { mkdir -p "$(dirname "$PACMAN_UPDATE_LOG")" echo "==> CoreDesktop: rebuilding the initramfs (mkinitcpio -P)" | tee -a "$PACMAN_UPDATE_LOG" sudo mkinitcpio -P 2>&1 | tee -a "$PACMAN_UPDATE_LOG" return "${PIPESTATUS[0]}" } # Free space on /boot, in MB boot_free_mb() { df --output=avail -BM /boot 2> /dev/null | tail -n 1 | tr -dc '0-9' } # Say why the last build failed, as far as the log and /boot tell explain_initramfs_failure() { local build free hook module build="$(initramfs_build_log)" echo "What went wrong:" grep -F "==> ERROR" <<< "$build" | grep -vF "CoreDesktop:" | tr -d '\r' | sort -u | sed 's/^/ /' free="$(boot_free_mb)" if grep -qi "no space left on device" <<< "$build" || { [ -n "$free" ] && (( free < 64 )); }; then echo "/boot is nearly full (${free:-?} MB free), so the new images don't fit. See what's there with" echo " du -sh /boot/*" echo "and remove what you don't need, e.g. images for kernels that are no longer installed." fi while read -r hook; do [ -n "$hook" ] || continue echo "HOOKS lists '$hook', but no installed package provides it. Install the package that does, or" echo "remove it from HOOKS in /etc/mkinitcpio.conf (or the file in /etc/mkinitcpio.conf.d that adds it)." done < <(grep -oP "Hook '\K[^']+(?=' cannot be found)" <<< "$build" | sort -u) while read -r module; do [ -n "$module" ] || continue echo "MODULES lists '$module', which isn't installed for this kernel. Install it, or remove it from" echo "MODULES in /etc/mkinitcpio.conf (or the file in /etc/mkinitcpio.conf.d that adds it)." done < <(grep -oP "module not found: '?\K[^' ]+" <<< "$build" | sort -u) echo "Then run: sudo mkinitcpio -P, and update to check it. The full log is $PACMAN_UPDATE_LOG." } # If the last initramfs build failed, build it once more; if that fails too, # say what's wrong. False while it's still broken. check_initramfs() { initramfs_build_failed || return 0 echo -e "\e[33mThe initramfs failed to build. Trying again...\e[0m" rebuild_initramfs if ! initramfs_build_failed; then echo -e "\e[32mThe initramfs built this time.\e[0m" return 0 fi echo -e "\e[31mThe initramfs still doesn't build. Don't reboot until it's fixed: the computer might not start.\e[0m" explain_initramfs_failure return 1 } # Where unowned_conflicts' files are moved, under their full paths, so they # can be put back. Outside the directories they came from: a copy left in # /etc/sddm.conf.d or /usr/lib/systemd/system-sleep would still be live. REPLACED_DIR="${REPLACED_DIR:-/var/lib/coredesktop/replaced}" # Print the files and directories pacman refused to install over, one per line, # when the upgrade in log $1 stopped on conflicting files that no package owns # (left by pip, make install or an earlier installer). Prints nothing, and # fails, when any conflicting path is owned by a package: moving the rest # wouldn't let the upgrade through. unowned_conflicts() { local log="$1" lines=() line path paths=() [ -f "$log" ] || return 1 grep -q 'failed to commit transaction (conflicting files)' "$log" || return 1 mapfile -t lines < <(tr -d '\r' < "$log" | grep -E '^[^ :]+: /.* exists in filesystem( \(owned by [^)]*\))?$') (( ${#lines[@]} > 0 )) || return 1 for line in "${lines[@]}"; do [[ "$line" == *"(owned by "* ]] && return 1 path="${line#*: }" path="${path% exists in filesystem}" # Checked again: pacman's message may be from before something changed pacman -Qo "$path" &> /dev/null && return 1 paths+=("$path") done printf '%s\n' "${paths[@]}" | sort -u } # Move each path given to the same path under REPLACED_DIR, keeping any copy # already there (numbered backups) move_aside() { local path for path in "$@"; do sudo mkdir -p "$REPLACED_DIR$(dirname "$path")" || return 1 sudo mv -T --backup=numbered "$path" "$REPLACED_DIR$path" || return 1 echo " $path -> $REPLACED_DIR$path" done } # Put back each path given that move_aside moved and nothing has replaced # since. -L too: -e is false for a dangling symlink. put_back() { local path for path in "$@"; do if [[ ! -e "$path" && ! -L "$path" ]] && [[ -e "$REPLACED_DIR$path" || -L "$REPLACED_DIR$path" ]]; then sudo mv -T "$REPLACED_DIR$path" "$path" && echo " $path" fi done } # pacman downloads from the first mirror in this list (system/mirrors.sh keeps # it fresh). Overridable for the tests. MIRRORLIST="${MIRRORLIST:-/etc/pacman.d/mirrorlist}" # A mirror further behind Arch than this is stale; most sync every few hours. # Used by version and updates.d/10-pacman.sh. # shellcheck disable=SC2034 MIRROR_STALE_HOURS=24 # The first mirror in MIRRORLIST, without its /$repo/os/$arch first_mirror() { sed -nE 's/^[[:space:]]*Server[[:space:]]*=[[:space:]]*//p' "$MIRRORLIST" 2> /dev/null \ | head -n 1 | sed -E 's#/\$repo.*##' } # Print how many hours ago the first mirror last synced with Arch, from the # timestamp mirrors publish in lastsync. Fails if it couldn't tell. mirror_age_hours() { local mirror lastsync mirror="$(first_mirror)" [ -n "$mirror" ] || return 1 lastsync="$(curl -sf --max-time 10 "$mirror/lastsync" 2> /dev/null)" || return 1 [[ "$lastsync" =~ ^[0-9]+$ ]] || return 1 echo $(( ($(date +%s) - lastsync) / 3600 )) } # Install a script atomically (copy to a temp file, then rename over the target) # so a copy of it that is running right now keeps reading the old file install_script() { local tmp tmp="$(mktemp "$2.XXXXXX")" && cp "$1" "$tmp" && chmod 755 "$tmp" && mv -f "$tmp" "$2" }