fix(default-flatpaks): Install/uninstall system flatpaks regardless i… (#100)

* fix(default-flatpaks): Install/uninstall system flatpaks regardless if current user has permissions or not

Fixes: https://github.com/ublue-os/bling/pull/93

To make notify-send working for --system systemd units, I have to thank @gerblesh for his code & idea in ublue-updater.

I tested this in Silverblue & it works.
However, I would like this to be tested in other DEs like Kinoite too, to see if it's working correctly.

Feel free to improve code if you find some space for that.

@lorduskordus

* Use json parser instead as a more reliable solution on fetching values

* There is no need for echo when parsing json

* Fetch DISPLAY environment variable from printenv

* Make functions for notify-sends, to make script clearer

* Whiteline fixes
This commit is contained in:
fiftydinar 2024-01-20 17:38:51 +01:00 committed by GitHub
parent 17bacbe3da
commit fe9cd68c6a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 60 additions and 67 deletions

View file

@ -1,42 +0,0 @@
#!/usr/bin/env bash
# Opt out of and remove Fedora's flatpak repo
if grep -qz 'fedora' <<< "$(flatpak remotes)"; then
/usr/bin/gnome-software --quit
/usr/lib/fedora-third-party/fedora-third-party-opt-out
/usr/bin/fedora-third-party disable
flatpak remote-delete fedora --force
flatpak remote-delete fedora-testing --force
# Remove flatpak apps from origin fedora
FEDORA_FLATPAKS=$(flatpak list --app --columns=application,origin | grep -w 'fedora' | awk '{print $1}')
flatpak remove --system --noninteractive ${FEDORA_FLATPAKS[@]}
# Remove flatpak runtimes from origin fedora
FEDORA_FLATPAKS=$(flatpak list --runtime --columns=application,arch,branch,origin | grep -w 'fedora' | awk '{print $1"/"$2"/"$3}')
flatpak remove --system --noninteractive ${FEDORA_FLATPAKS[@]}
fi
REPO_INFO="/etc/flatpak/system/repo-info.yml"
REPO_URL=$(yq '.repo-url' $REPO_INFO)
REPO_NAME=$(yq '.repo-name' $REPO_INFO)
REPO_TITLE=$(yq '.repo-title' $REPO_INFO)
# Set up system-wide Flatpak repository
if [[ ! $REPO_URL == "null" && ! $REPO_NAME == "null" ]]; then
flatpak remote-add --if-not-exists --system "$REPO_NAME" "$REPO_URL"
echo "Adding system-wide remote $REPO_NAME from $REPO_URL"
fi
# If configured remote is flathub, enable it here.
# Flathub is already installed on Fedora, but not enabled by default,
# so the above command won't add it again
if [[ $REPO_NAME == "flathub" ]]; then
flatpak remote-modify --system "$REPO_NAME" --enable
fi
# Change repository title to configured title, if not null
if [[ ! $REPO_TITLE == "null" ]]; then
flatpak remote-modify --system "$REPO_NAME" --title="$REPO_TITLE"
echo "Setting title $REPO_TITLE for remote $REPO_NAME"
fi

View file

@ -1,7 +1,45 @@
#!/usr/bin/env bash
# Opt out of and remove Fedora's flatpak repo
if grep -qz 'fedora' <<< "$(flatpak remotes)"; then
/usr/bin/gnome-software --quit
/usr/lib/fedora-third-party/fedora-third-party-opt-out
/usr/bin/fedora-third-party disable
flatpak remote-delete fedora --force
flatpak remote-delete fedora-testing --force
# Remove flatpak apps from origin fedora
FEDORA_FLATPAKS=$(flatpak list --app --columns=application,origin | grep -w 'fedora' | awk '{print $1}')
flatpak remove --system --noninteractive ${FEDORA_FLATPAKS[@]}
# Remove flatpak runtimes from origin fedora
FEDORA_FLATPAKS=$(flatpak list --runtime --columns=application,arch,branch,origin | grep -w 'fedora' | awk '{print $1"/"$2"/"$3}')
flatpak remove --system --noninteractive ${FEDORA_FLATPAKS[@]}
fi
REPO_INFO="/etc/flatpak/system/repo-info.yml"
REPO_URL=$(yq '.repo-url' $REPO_INFO)
REPO_NAME=$(yq '.repo-name' $REPO_INFO)
REPO_TITLE=$(yq '.repo-title' $REPO_INFO)
# Set up system-wide Flatpak repository
if [[ ! $REPO_URL == "null" && ! $REPO_NAME == "null" ]]; then
flatpak remote-add --if-not-exists --system "$REPO_NAME" "$REPO_URL"
echo "Adding system-wide remote $REPO_NAME from $REPO_URL"
fi
# If configured remote is flathub, enable it here.
# Flathub is already installed on Fedora, but not enabled by default,
# so the above command won't add it again
if [[ $REPO_NAME == "flathub" ]]; then
flatpak remote-modify --system "$REPO_NAME" --enable
fi
# Change repository title to configured title, if not null
if [[ ! $REPO_TITLE == "null" ]]; then
flatpak remote-modify --system "$REPO_NAME" --title="$REPO_TITLE"
echo "Setting title $REPO_TITLE for remote $REPO_NAME"
fi
# Notifications config
NOTIFICATIONS=$(cat /etc/flatpak/notifications)
@ -13,6 +51,22 @@ FLATPAK_LIST=$(flatpak list --system --columns=application)
INSTALL_LIST_FILE="/etc/flatpak/system/install"
REMOVE_LIST_FILE="/etc/flatpak/system/remove"
function notify-send-install {
user_name=$(loginctl list-sessions --output=json | jq -r '.[].user')
uid=$(loginctl list-sessions --output=json | jq -r '.[].uid')
xdg_runtime_path="/run/user/$uid"
display_var=$(printenv DISPLAY)
sudo -u "$user_name" DBUS_SESSION_BUS_ADDRESS=unix:path="$xdg_runtime_path"/bus DISPLAY="$display_var" notify-send "Flatpak Installer" "Finished install of system flatpaks:\n$INSTALL_LIST" --app-name="Flatpak Installer" -u NORMAL
}
function notify-send-uninstall {
user_name=$(loginctl list-sessions --output=json | jq -r '.[].user')
uid=$(loginctl list-sessions --output=json | jq -r '.[].uid')
xdg_runtime_path="/run/user/$uid"
display_var=$(printenv DISPLAY)
sudo -u "$user_name" DBUS_SESSION_BUS_ADDRESS=unix:path="$xdg_runtime_path"/bus DISPLAY="$display_var" notify-send "Flatpak Installer" "Finished uninstall of system flatpaks:\n$REMOVE_LIST" --app-name="Flatpak Installer" -u NORMAL
}
# Install flatpaks in list
if [[ -f $INSTALL_LIST_FILE ]]; then
if [[ -n $FLATPAK_LIST ]]; then
@ -23,7 +77,7 @@ if [[ -f $INSTALL_LIST_FILE ]]; then
if [[ -n $INSTALL_LIST ]]; then
flatpak install --system --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]}
if [[ $NOTIFICATIONS == "true" ]]; then
notify-send "Flatpak Installer" "Finished install of system flatpaks:\n$INSTALL_LIST" --app-name="Flatpak Installer" -u NORMAL
notify-send-install
fi
fi
fi
@ -34,7 +88,7 @@ if [[ -f $REMOVE_LIST_FILE ]]; then
if [[ -n $REMOVE_LIST ]]; then
flatpak uninstall --system --noninteractive ${REMOVE_LIST[@]}
if [[ $NOTIFICATIONS == "true" ]]; then
notify-send "Flatpak Installer" "Finished uninstall of system flatpaks:\n$REMOVE_LIST" --app-name="Flatpak Installer" -u NORMAL
notify-send-uninstall
fi
fi
fi

View file

@ -5,7 +5,7 @@ After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/bin/system-flatpak-presetup
ExecStart=/usr/bin/system-flatpak-setup
Restart=on-failure
RestartSec=30
StartLimitInterval=0

View file

@ -1,14 +0,0 @@
[Unit]
Description=Configure Flatpaks for the system
Wants=network-online.target
After=system-flatpak-presetup.service
[Service]
Type=simple
ExecStart=/usr/bin/system-flatpak-setup
Restart=on-failure
RestartSec=30
StartLimitInterval=0
[Install]
WantedBy=default.target

View file

@ -6,17 +6,15 @@ Flatpaks can either be installed system-wide or per-user, though per-user flatpa
The module uses the following scripts to handle flatpak setup:
- `/usr/bin/system-flatpak-presetup`
- `/usr/bin/system-flatpak-setup`
- `/usr/bin/user-flatpak-setup`
The scripts are run on every boot by these services:
- `/usr/lib/systemd/system/system-flatpak-presetup.service`
- `/usr/lib/systemd/system/system-flatpak-setup.service`
- `/usr/lib/systemd/user/user-flatpak-setup-service`
`system-flatpak-presetup` uninstalls Fedora flatpaks & replaces Fedora repos with your system repos (needs root, hence why it's presetup). `system-flatpak-setup` checks the flatpak install/remove lists created by the module & performs the install/uninstall operation according to that. `user-flatpak-setup` functions the same for user flatpaks, with additional "user" replacement repo operation.
`system-flatpak-setup` uninstalls Fedora flatpaks, replaces Fedora repos with your repo choice, checks the flatpak install/remove lists created by the module & performs the install/uninstall operation according to that. `user-flatpak-setup` functions the same for user flatpaks.
This module stores the Flatpak remote configuration and Flatpak install/remove lists in `/etc/flatpak/`. There are two subdirectories, `user` and `system` corresponding with the install level of the Flatpaks and repositories. Each directory has text files containing the IDs of flatpaks to `install` and `remove`, plus a `repo-info.yml` containing the details of the Flatpak repository.

View file

@ -5,11 +5,9 @@ set -euo pipefail
BLING_DIRECTORY="${BLING_DIRECTORY:-"/tmp/bling"}"
cp -r "$BLING_DIRECTORY"/files/usr/bin/system-flatpak-presetup /usr/bin/system-flatpak-presetup
cp -r "$BLING_DIRECTORY"/files/usr/bin/system-flatpak-setup /usr/bin/system-flatpak-setup
cp -r "$BLING_DIRECTORY"/files/usr/bin/user-flatpak-setup /usr/bin/user-flatpak-setup
cp -r "$BLING_DIRECTORY"/files/usr/lib/systemd/system/system-flatpak-presetup.service /usr/lib/systemd/system/system-flatpak-presetup.service
cp -r "$BLING_DIRECTORY"/files/usr/lib/systemd/user/system-flatpak-setup.service /usr/lib/systemd/user/system-flatpak-setup.service
cp -r "$BLING_DIRECTORY"/files/usr/lib/systemd/system/system-flatpak-setup.service /usr/lib/systemd/system/system-flatpak-setup.service
cp -r "$BLING_DIRECTORY"/files/usr/lib/systemd/user/user-flatpak-setup.service /usr/lib/systemd/user/user-flatpak-setup.service
configure_flatpak_repo () {
@ -106,8 +104,7 @@ configure_lists () {
echo "Enabling flatpaks module"
mkdir -p /usr/etc/flatpak/{system,user}
systemctl enable -f system-flatpak-presetup.service
systemctl enable -f --global system-flatpak-setup.service
systemctl enable -f system-flatpak-setup.service
systemctl enable -f --global user-flatpak-setup.service
# Check that `system` is present before configuring