Shell aliases for Flatpak applications

16 Dec 2019

Although I gave up on tuning AwesomeWM configuration years ago and switched completely to GNOME, I still spend most of my time in the terminal. Instead of navigating to photos directory in Nautilus, I instinctively spawn a new terminal window with Super + Enter and type eog ~/pics/filename.jpg. This has become harder as I replaced almost all desktop applications provided by my distribution of choice with packages from Flathub.

Modern-day Flatpak generates simple shell wrappers at /var/lib/flatpak/exports/bin. To make it more secure, it uses application ID as the command name. It would not be fun to have sudo shadowed by a rogue application if bin directory had a higher priority in $PATH.

While it is easy to guess reverse DNS used for GNOME and KDE applications, guessing IDs of other applications may be burdensome. I am not only lazy so I wrote a script to generate shell aliases based on command defined by applications.

shopt -s nullglob

declare -A aliases

for bin in /var/lib/flatpak/exports/bin/* ~/.local/share/flatpak/exports/bin/*; do
  appid="$(basename $bin)"
  cmd="$(flatpak info -m $appid | awk -F= '/^command=/ {print $2}')"

  [[ -z $cmd ]] && continue
  aliases[$appid]="$(basename ${cmd##*/})"

  for appid in "${!aliases[@]}"; do
    echo "alias ${aliases[$appid]}=$appid"
) > ~/.cache/flatpak-aliases

This approach is not ideal as command can be set arbitrarily. Some applications use helper scripts as the entry point, for example org.keepassxc.KeePassXC ends up aliased as I consider it good enough though.