summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbuild_files/build.sh4
-rw-r--r--system_files/usr/lib/systemd/system/aerocore-flatpak-mirror.service11
-rwxr-xr-xsystem_files/usr/libexec/aerocore-configure-flatpak-mirror21
-rwxr-xr-xtests/test-flatpak-runtime-mirror.sh62
4 files changed, 98 insertions, 0 deletions
diff --git a/build_files/build.sh b/build_files/build.sh
index 2d43e33..23e86cb 100755
--- a/build_files/build.sh
+++ b/build_files/build.sh
@@ -14,6 +14,10 @@ esac
# Copy the contents of system_files/ of the git repo to /
cp -avf "/ctx/system_files"/. /
+# Apply configured Flatpak mirrors to existing installations as well as new
+# remotes created from flathub.flatpakrepo during first boot.
+systemctl enable aerocore-flatpak-mirror.service
+
# Keep Bazzite's game mode news hook unchanged; select the matching feed branch.
NEWS_HOOK_CONFIG=/etc/gamemode-news-hook.conf
if [[ ! -f "${NEWS_HOOK_CONFIG}" ]]; then
diff --git a/system_files/usr/lib/systemd/system/aerocore-flatpak-mirror.service b/system_files/usr/lib/systemd/system/aerocore-flatpak-mirror.service
new file mode 100644
index 0000000..8aed8cb
--- /dev/null
+++ b/system_files/usr/lib/systemd/system/aerocore-flatpak-mirror.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=Apply configured AeroCore Flatpak mirror
+After=flatpak-add-fedora-repos.service bazzite-flatpak-manager.service
+Wants=flatpak-add-fedora-repos.service bazzite-flatpak-manager.service
+
+[Service]
+Type=oneshot
+ExecStart=/usr/libexec/aerocore-configure-flatpak-mirror
+
+[Install]
+WantedBy=multi-user.target
diff --git a/system_files/usr/libexec/aerocore-configure-flatpak-mirror b/system_files/usr/libexec/aerocore-configure-flatpak-mirror
new file mode 100755
index 0000000..42b1d84
--- /dev/null
+++ b/system_files/usr/libexec/aerocore-configure-flatpak-mirror
@@ -0,0 +1,21 @@
+#!/usr/bin/bash
+
+set -u
+
+config_file=/etc/environment.d/99-aerocore-mirrors.conf
+[[ -r "${config_file}" ]] || exit 0
+
+FLATPAK_REMOTE_URL="$(sed -n 's/^FLATPAK_REMOTE_URL=//p' "${config_file}" | tail -n 1)"
+[[ -n "${FLATPAK_REMOTE_URL}" ]] || exit 0
+[[ "${FLATPAK_REMOTE_URL}" =~ ^https?://[^[:space:]]+$ ]] || {
+ echo "Ignoring invalid FLATPAK_REMOTE_URL: ${FLATPAK_REMOTE_URL}" >&2
+ exit 0
+}
+
+mirror_url="${FLATPAK_REMOTE_URL%/}/"
+if flatpak remotes --system --columns=name | grep -Fxq flathub; then
+ flatpak remote-modify --system flathub --url="${mirror_url}"
+else
+ flatpak remote-add --system --if-not-exists flathub /etc/flatpak/remotes.d/flathub.flatpakrepo
+ flatpak remote-modify --system flathub --url="${mirror_url}"
+fi
diff --git a/tests/test-flatpak-runtime-mirror.sh b/tests/test-flatpak-runtime-mirror.sh
new file mode 100755
index 0000000..bd0fc86
--- /dev/null
+++ b/tests/test-flatpak-runtime-mirror.sh
@@ -0,0 +1,62 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+
+project_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
+source_script="${project_root}/system_files/usr/libexec/aerocore-configure-flatpak-mirror"
+test_root="$(mktemp -d)"
+trap 'rm -rf "${test_root}"' EXIT
+
+assert_eq() {
+ local expected="$1" actual="$2" message="$3"
+ if [[ "${expected}" != "${actual}" ]]; then
+ printf 'FAIL: %s\nexpected: <%s>\nactual: <%s>\n' "${message}" "${expected}" "${actual}" >&2
+ exit 1
+ fi
+}
+
+run_case() {
+ local name="$1" exists="$2" url="$3"
+ local root="${test_root}/${name}"
+ mkdir -p "${root}/etc/environment.d" "${root}/etc/flatpak/remotes.d" "${root}/usr/bin"
+ printf 'FLATPAK_REMOTE_URL=%s\n' "${url}" > "${root}/etc/environment.d/99-aerocore-mirrors.conf"
+ : > "${root}/etc/flatpak/remotes.d/flathub.flatpakrepo"
+ printf '%s\n' "${exists}" > "${root}/remote-exists"
+ : > "${root}/flatpak.calls"
+
+ cat > "${root}/usr/bin/flatpak" <<'FLATPAK'
+#!/usr/bin/env bash
+printf '%s\n' "$*" >> "${TEST_ROOT}/flatpak.calls"
+if [[ "$1 $2 $3" == 'remotes --system --columns=name' ]]; then
+ [[ "$(cat "${TEST_ROOT}/remote-exists")" == true ]] && echo flathub
+fi
+if [[ "$1 $2 $3" == 'remote-add --system --if-not-exists' ]]; then
+ printf true > "${TEST_ROOT}/remote-exists"
+fi
+FLATPAK
+ chmod +x "${root}/usr/bin/flatpak"
+
+ sed -e "s|/etc/environment.d|${root}/etc/environment.d|g" \
+ -e "s|/etc/flatpak|${root}/etc/flatpak|g" \
+ "${source_script}" > "${root}/configure-mirror"
+ chmod +x "${root}/configure-mirror"
+ export TEST_ROOT="${root}" PATH="${root}/usr/bin:${PATH}"
+ bash "${root}/configure-mirror"
+ cat "${root}/flatpak.calls" > "${root}/calls.actual"
+}
+
+run_case existing_remote true https://mirror.example/flathub
+assert_eq 'remotes --system --columns=name
+remote-modify --system flathub --url=https://mirror.example/flathub/' "$(cat "${test_root}/existing_remote/calls.actual")" 'existing remote is updated'
+
+run_case missing_remote false https://mirror.example/flathub/
+expected_missing_calls="$(printf 'remotes --system --columns=name\nremote-add --system --if-not-exists flathub %s\nremote-modify --system flathub --url=https://mirror.example/flathub/' "${test_root}/missing_remote/etc/flatpak/remotes.d/flathub.flatpakrepo")"
+assert_eq "${expected_missing_calls}" "$(cat "${test_root}/missing_remote/calls.actual")" 'missing remote is added then updated'
+
+run_case empty_url true ''
+assert_eq '' "$(cat "${test_root}/empty_url/calls.actual")" 'empty setting leaves remotes unchanged'
+
+run_case invalid_url true 'not-a-url'
+assert_eq '' "$(cat "${test_root}/invalid_url/calls.actual")" 'invalid setting leaves remotes unchanged'
+
+echo "Flatpak runtime mirror behavior checks passed."