Jonny Irwin

Setting GTK theme on Flatpak apps

April 13, 2020

Whenever possible I like to install apps on my linux desktop as flatpaks. The reason for this is that they are installed in containers, keeping everything isolated. Doing this means that it is easy to get a consistent installation and running experience across different linux distributions.

The one issue that I have with flatpaks is that flatpak applications by default do not have access to your desktops GTK theme settings and try to default to using Adwaita. As someone who uses a dark theme for everything, having applications open with a retina-burning light theme is painful. I have found that setting the GTK theme for flatpak apps is in general a painful experience - checkout this github issue for further details.

If you use a common GTK theme on your desktop that is available as a flatpak app (check using flatpak remote-ls | grep org.gtk.Gtk3theme), as long as you install the same theme as a flatpak (e.g. flatpak install org.gtk.Gtk3theme.Adwaita-dark), then this theme should be picked up.

I, however, do not use a common GTK theme on my desktop, having created my own using Oomox. I want to be able to use a GTK theme, as close as possible to my own colour scheme to create a consistent environment. To do this I install the GTK theme I want from flathub, ensure that each flatpak has a settings.ini file in place in the gtk-3.0 folder that sets the desired GTK theme.

To automate this as much as possible i have written the following Ansible playbook:

---
- hosts: localhost
  connection: local
  
  vars:
    flatpak_gtk_package: org.gtk.Gtk3theme.Qogir-dark
    gtk_theme_name: Qogir-dark
    flatpak_apps_dir: "{{ ansible_env.HOME }}/.var/app"
  
  tasks:
    - name: Install flatpak GTK theme
      flatpak:
        name: "{{ flatpak_gtk_package }}"
        state: present

    - name: Get flatpak app directories
      find:
        paths: "{{ flatpak_apps_dir }}"
        file_type: directory 
      register: "app_dirs"

    - name: Ensure gtk-3.0 settings.ini file exists
      lineinfile:
        path: "{{ item.path }}/config/gtk-3.0/settings.ini"
        regexp: '\[Settings\]'
        line: "[Settings]"
        create: yes				
      loop:  "{{ app_dirs.files }}"

    - name: Set GTK theme
      lineinfile:
        path: "{{ item.path }}/config/gtk-3.0/settings.ini"
        regexp: "gtk-theme-name="
        line: "gtk-theme-name={{ gtk_theme_name }}"
      loop: "{{ app_dirs.files }}"

This ansible playbook can be run multiple times (after each new flatpak is installed).

This is generally giving my a consistent experience but I know it is far from perfect:

  • It will create a GTK settings.ini even if the flatpak does not use GTK
  • Flatpak applications can use different runtimes which may need different versions of themes - This isn’t causing me many issues at the minute, I will look at it again if/when this becomes a problem.

Jonny Irwin

Full-stack software developer living in the Isle of Man. Interests in Testing, Accessibility & DevOps - using Javascript, C#, Python, Linux & more.