particle-os-modules/modules/default-flatpaks/default-flatpaks.sh
fiftydinar 1e1197dd0b
refactor(default-flatpaks): Switch to standardized BlueBuild location, implement useful docs into files & make flatpak detection + comparison more robust (#122)
* chore(default-flatpaks): Switch to standardized BlueBuild location

Use

`/usr/etc/bluebuild/default-flatpaks`

location instead of

`/usr/etc/flatpaks`

If possible, we should ideally use this location for system modifications:

`/usr/share/bluebuild/default-flatpaks`

While having user modifications in:

`/usr/etc/bluebuild/default-flatpaks`

But it needs to be figured out how the logic will work for separating system & user modifications this way.

I used this logic in unofficial `initramfs-setup` module & it works well, by merging both system & user files into 1 output.

However, this method can create duplicates if user specified it in it's modification, so I mentioned that user should look if the system entry has modifications they need. Perhaps, array diff can be done, which would circumvent this.

So merge this for start.

Only other module which has potential for migrating to standardized BlueBuild config is yafti (more details in other PR I'll do in some time).

* refactor(default-flatpaks): Make default-flatpaks more robust

Document in detail to user how he can change the config in files itself.

Also document what files do in `/usr/share/bluebuild/default-flatpaks` as well.

Refactor flatpak lists detection to be more reliable by excluding words starting with # symbols, whitelines & duplicate entries. Use `comm` for comparing flatpak list to existing flatpaks output instead of using `grep`, as it's easier to use & it's more reliable.

Separate user's & maintainer's modifications better by utilizing read-only `/usr/share/bluebuild/default-flatpaks` directory for maintainers, while for users we would use `(/usr)/etc/bluebuild/default-flatpaks` directory. Reverting to defaults is more reliable as it would avoid users from touching maintainer's modification directly.

I wouldn't modify repo-info.yml doc content, as we restrict it from user's modification & we wouldn't want to potentially ruin yaml parsing just for that.

Only thing that remains is to test this in a VM. And look to potentially make code cleaner.

* draft(default-flatpaks): Avoid install/remove duplicate loop scenario

I have to figure out this part

* draft(default-flatpaks): Avoid install/remove duplicate loop scenario pt.2

* draft(default-flatpaks): Avoid install/remove duplicate loop scenario pt.3

* fix(default-flatpaks): Avoid install/remove duplicate loop scenario

* chore(default-flatpaks): minor grammar adjustment

* draft(default-flatpaks): Account for scenario...

when user sets the same package in install list that is in maintainer's remove list & vice-versa

* fix(default-flatpaks): Account for scenario when user sets the same package in install list that is in maintainer's remove list & vice-versa

* fix(default-flatpaks): Typo in code for combined install list

* chore(default-flatpaks): Remove unnecessary echo

* chore(default-flatpaks): Make directory for user config

* chore(default-flatpaks): Explain user's vs maintainer's flatpak list situation better

* fix(default-flatpaks): Typo in user's configuration for system flatpaks install

* chore(default-flatpaks): There is no need to mkdir parent folder if child folder is created

* chore(default-flatpaks): Make config organization cleaner

Don't use echos for writing configs, use files instead for easier & more intuitive editing.

* fix(default-flatpaks): Copy notification file properly

Made a mistake in variable name

* docs: README fixes

- don't use ambiguous term "live-user"
- capitalize Flatpak
- grammar and phrasing changes

* docs: grammar and phrasing changes in configuration file docs

- replace "maintainer's config" with "image's default config"
- rewrite flatpak list format explanation

* docs: slightly reword local modification section again

* chore(default-flatpaks): Add a missing newline to system flatpak list

---------

Co-authored-by: xynydev
2024-02-11 09:33:05 +00:00

137 lines
5.9 KiB
Bash

#!/usr/bin/env bash
# Tell build process to exit if there are any errors.
set -euo pipefail
MODULE_DIRECTORY="${MODULE_DIRECTORY:-"/tmp/modules"}"
cp -r "$MODULE_DIRECTORY"/default-flatpaks/system-flatpak-setup /usr/bin/system-flatpak-setup
cp -r "$MODULE_DIRECTORY"/default-flatpaks/user-flatpak-setup /usr/bin/user-flatpak-setup
cp -r "$MODULE_DIRECTORY"/default-flatpaks/system-flatpak-setup.service /usr/lib/systemd/system/system-flatpak-setup.service
cp -r "$MODULE_DIRECTORY"/default-flatpaks/user-flatpak-setup.service /usr/lib/systemd/user/user-flatpak-setup.service
configure_flatpak_repo () {
CONFIG_FILE=$1
INSTALL_LEVEL=$2
REPO_INFO="/usr/share/bluebuild/default-flatpaks/$INSTALL_LEVEL/repo-info.yml"
get_yaml_array INSTALL ".$INSTALL_LEVEL.install[]" "$CONFIG_FILE"
# Checks pre-configured repo info, if exists
if [[ -f $REPO_INFO ]]; then
echo "Existing $INSTALL_LEVEL configuration found:"
cat $REPO_INFO
CONFIG_URL=$(yq ".repo-url" "$REPO_INFO")
CONFIG_NAME=$(yq ".repo-name" "$REPO_INFO")
CONFIG_TITLE=$(yq ".repo-title" "$REPO_INFO")
else
CONFIG_URL="null"
CONFIG_NAME="null"
CONFIG_TITLE="null"
fi
echo "Configuring $INSTALL_LEVEL repo in $REPO_INFO"
REPO_URL=$(echo "$CONFIG_FILE" | yq -I=0 ".$INSTALL_LEVEL.repo-url")
REPO_NAME=$(echo "$CONFIG_FILE" | yq -I=0 ".$INSTALL_LEVEL.repo-name")
REPO_TITLE=$(echo "$CONFIG_FILE" | yq -I=0 ".$INSTALL_LEVEL.repo-title")
# If repo-name isn't configured, use flathub as fallback
# Checked separately from URL to allow custom naming
if [[ $REPO_NAME == "null" && $CONFIG_NAME == "null" ]]; then
if [[ ${#INSTALL[@]} -gt 0 ]]; then
REPO_NAME="flathub"
fi
# Re-use existing config, if no new configuration was added
elif [[ $REPO_NAME == "null" && ! $CONFIG_NAME == "null" ]]; then
REPO_NAME=$CONFIG_NAME
fi
# Re-use existing config, if no new configuration was added
if [[ $REPO_TITLE == "null" && ! $CONFIG_TITLE == "null" ]]; then
REPO_TITLE=$CONFIG_TITLE
fi
if [[ $REPO_URL == "null" && $CONFIG_URL == "null" ]]; then
# If repo name is configured, or if there are Flatpaks to be installed,
# set Flathub as repo URL
if [[ ! $REPO_NAME == "null" || ${#INSTALL[@]} -gt 0 ]]; then
REPO_URL=https://dl.flathub.org/repo/flathub.flatpakrepo
fi
# Re-use existing config, if no new configuration was added
elif [[ $REPO_URL == "null" && ! $CONFIG_URL == "null" ]]; then
REPO_URL=$CONFIG_URL
fi
touch $REPO_INFO
# EOF breaks if the contents are indented,
# so the below lines are intentionally un-indented
cat > $REPO_INFO <<EOF
repo-url: "$REPO_URL"
repo-name: "$REPO_NAME"
repo-title: "$REPO_TITLE"
EOF
# Show results of repo configuration
cat $REPO_INFO
}
configure_lists () {
CONFIG_FILE=$1
INSTALL_LEVEL=$2
INSTALL_LIST="/usr/share/bluebuild/default-flatpaks/$INSTALL_LEVEL/install"
REMOVE_LIST="/usr/share/bluebuild/default-flatpaks/$INSTALL_LEVEL/remove"
get_yaml_array INSTALL ".$INSTALL_LEVEL.install[]" "$CONFIG_FILE"
get_yaml_array REMOVE ".$INSTALL_LEVEL.remove[]" "$CONFIG_FILE"
echo "Creating $INSTALL_LEVEL Flatpak install list at $INSTALL_LIST"
if [[ ${#INSTALL[@]} -gt 0 ]]; then
for flatpak in "${INSTALL[@]}"; do
echo "Adding to $INSTALL_LEVEL flatpak installs: $(printf ${flatpak})"
echo $flatpak >> $INSTALL_LIST
done
fi
echo "Creating $INSTALL_LEVEL Flatpak removals list $REMOVE_LIST"
if [[ ${#REMOVE[@]} -gt 0 ]]; then
for flatpak in "${REMOVE[@]}"; do
echo "Adding to $INSTALL_LEVEL flatpak removals: $(printf ${flatpak})"
echo $flatpak >> $REMOVE_LIST
done
fi
}
echo "Enabling flatpaks module"
mkdir -p /usr/share/bluebuild/default-flatpaks/{system,user}
mkdir -p /usr/etc/bluebuild/default-flatpaks/{system,user}
systemctl enable -f system-flatpak-setup.service
systemctl enable -f --global user-flatpak-setup.service
# Check that `system` is present before configuring. Also copy template list files before writing Flatpak IDs.
if [[ ! $(echo "$1" | yq -I=0 ".system") == "null" ]]; then
configure_flatpak_repo "$1" "system"
cp -r "$MODULE_DIRECTORY"/default-flatpaks/config/system/install /usr/share/bluebuild/default-flatpaks/system/install
cp -r "$MODULE_DIRECTORY"/default-flatpaks/config/system/remove /usr/share/bluebuild/default-flatpaks/system/remove
configure_lists "$1" "system"
fi
# Check that `user` is present before configuring. Also copy template list files before writing Flatpak IDs.
if [[ ! $(echo "$1" | yq -I=0 ".user") == "null" ]]; then
configure_flatpak_repo "$1" "user"
cp -r "$MODULE_DIRECTORY"/default-flatpaks/config/user/install /usr/share/bluebuild/default-flatpaks/user/install
cp -r "$MODULE_DIRECTORY"/default-flatpaks/config/user/remove /usr/share/bluebuild/default-flatpaks/user/remove
configure_lists "$1" "user"
fi
echo "Configuring default-flatpaks notifications"
NOTIFICATIONS=$(echo "$1" | yq -I=0 ".notify")
CONFIG_NOTIFICATIONS="/usr/share/bluebuild/default-flatpaks/notifications"
cp -r "$MODULE_DIRECTORY"/default-flatpaks/config/notifications "$CONFIG_NOTIFICATIONS"
echo "$NOTIFICATIONS" >> "$CONFIG_NOTIFICATIONS"
echo "Copying user modification template files"
cp -r "$MODULE_DIRECTORY"/default-flatpaks/user-config/system/install /usr/etc/bluebuild/default-flatpaks/system/install
cp -r "$MODULE_DIRECTORY"/default-flatpaks/user-config/system/remove /usr/etc/bluebuild/default-flatpaks/system/remove
cp -r "$MODULE_DIRECTORY"/default-flatpaks/user-config/user/install /usr/etc/bluebuild/default-flatpaks/user/install
cp -r "$MODULE_DIRECTORY"/default-flatpaks/user-config/user/remove /usr/etc/bluebuild/default-flatpaks/user/remove
cp -r "$MODULE_DIRECTORY"/default-flatpaks/user-config/notifications /usr/etc/bluebuild/default-flatpaks/notifications