particle-os-modules/modules/default-flatpaks/system-flatpak-setup
fiftydinar 9b66c64563
feat(default-flatpaks): install & uninstall notifs (#109)
* feat(default-flatpaks): Support for notifications that inform the user before flatpak install/uninstall procedure begins

This can be implemented to be more readable maybe, but this should work.

* chore(default-flatpaks): There is no need to call notifications true statement twice

* chore(default-flatpaks): Make starting notifications consistent with finished ones

* chore(default-flatpaks): Use "listed" term rather than "some"

* chore(default-flatpaks): Use "some" notification only for uninstalling flatpaks
2024-01-30 17:06:25 +00:00

112 lines
4.9 KiB
Bash
Executable file

#!/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)
# Installed flatpaks
FLATPAK_LIST=$(flatpak list --system --columns=application)
# Flatpak list files
INSTALL_LIST_FILE="/etc/flatpak/system/install"
REMOVE_LIST_FILE="/etc/flatpak/system/remove"
function notify-send-pre-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" "Started install of system flatpaks" --app-name="Flatpak Installer" -u NORMAL
}
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-pre-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" "Started uninstall of some system flatpaks" --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
INSTALL_LIST=$(echo "$FLATPAK_LIST" | grep -vf - "$INSTALL_LIST_FILE")
else
INSTALL_LIST=$(cat $INSTALL_LIST_FILE)
fi
if [[ -n $INSTALL_LIST ]] && [[ ! $NOTIFICATIONS == "true" ]]; then
flatpak install --system --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]}
elif [[ -n $INSTALL_LIST ]] && [[ $NOTIFICATIONS == "true" ]]; then
notify-send-pre-install
flatpak install --system --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]}
notify-send-install
fi
fi
# Remove flatpaks in list
if [[ -f $REMOVE_LIST_FILE ]]; then
REMOVE_LIST=$(echo "$FLATPAK_LIST" | grep -o -f - "$REMOVE_LIST_FILE")
if [[ -n $REMOVE_LIST ]] && [[ ! $NOTIFICATIONS == "true" ]]; then
flatpak uninstall --system --noninteractive ${REMOVE_LIST[@]}
elif [[ -n $REMOVE_LIST ]] && [[ $NOTIFICATIONS == "true" ]]; then
notify-send-pre-uninstall
flatpak uninstall --system --noninteractive ${REMOVE_LIST[@]}
notify-send-uninstall
fi
fi