Reduce size of Qt5 App Snap

Hello everyone,

I have a Lazarus/Free Pascal project called Cantara which uses Qt5. I finally managed to snap it with this snapcraft.yaml which I placed into the Github repository.

The building works fine and I even managed to give my app a native look and feel with including the following stage-packages:

[...]
- light-themes
- ttf-ubuntu-font-family
- locales-all
- plasma-integration
- frameworkintegration
- kde-style-breeze
- breeze-icon-theme
- fonts-freefont-ttf
- qt5-style-plugins

However, I still struggle with the size of the snap. It is more than 100 MB – compared to classical packages like .deb which are all less then 10 MB. Due to the fact that Qt5 is installed on most Linux distributions, I am wondering whether I can reduce the size of my Snap while allowing it to access parts of the system (e.g. the Qt themes etc.) – or if it is just something I have to deal with when using a containerized package system.

Looking forward to your suggestions!

Well, as long as you’re using the kde-neon extension, your snap does not need to include Qt stage-packages. Looking at your snap in edge, you’re shipping Qt libraries (that are most likely all included in the KDE content snap), libLLVM, and tons of icons (e.g. Breeze is included in the content snap, some are included from the gtk-common-themes snap). That means you can probably get rid of a good chunk of your snap size.

Also look into using a cleanup part for getting rid of duplicate files: https://github.com/FreeCAD/FreeCAD-snap/blob/41fe7038fec0a5261cdb860c1f6d9908c36de746/snap/snapcraft.yaml#L277

Thanks for your fast reply! Actually I have tried before to build the snap without the Qt libraries and style packages: It resulted in errors when starting the snap and if the snap started, it looked not native (very old default design). Maybe I am doing something wrong with the stage part?

What is the content snap and how should I include it?

The cleanup part is something which I had tried before – all resulting in errors as well due to libraries which weren’t found after startup. In addition, I don’t use a snap like kde-frameworks-5-91-qt-5-15-3-core20 where I could do some cleanup…

Actually, you do. Due to your usage of the kde-neon extension, this gets added to your snapcraft.yaml. You can check by doing snapcraft expand-extensions:

[...]
plugs:
  desktop:
    mount-host-font-cache: false
  icon-themes:
    default-provider: gtk-common-themes
    interface: content
    target: $SNAP/data-dir/icons
  kde-frameworks-5-91-qt-5-15-3-core20:
    content: kde-frameworks-5-91-qt-5-15-3-core20-all
    default-provider: kde-frameworks-5-91-qt-5-15-3-core20
    interface: content
    target: $SNAP/kf5
  sound-themes:
    default-provider: gtk-common-themes
    interface: content
    target: $SNAP/data-dir/sounds
environment:
  SNAP_DESKTOP_RUNTIME: $SNAP/kf5
[...]

The Qt app should look decent with the kde-neon extension; not perfectly integrated into a Gtk desktop environment, but okay. Have you looked at other Qt/KDE apps, e.g. kdenlive or FreeCAD? Do they look bad to you, too?

Thanks for the explanations! Does that mean that I have to stage the directories kf5, data-dir/icons and data-dir/sounds as well in order to really make use of the kde-neon extension?

No, those get mounted into your snap from the content snap at runtime. Because of that, you do not need to ship them and every snap that uses those content snaps can share those resources, which means there’s less to download and better performance.

Also, you shouldn’t usually stage dev packages; they are meant for the building stage.

Just as a very quick & dirty experiment, this version of your packaging gives me a 7 MB snap instead of your 284 MB package:

name: cantara
version: '2.2.3'
summary: A Song Presentation Software.
description: |
  A simple, leightweight but powerful program for song presentation in churches, small groups or at karaoke parties.
confinement: strict
icon: Cantara.png
base: core20
grade: stable

architectures:
  - build-on: amd64

parts:
  cantara:
    plugin: nil
    source: .
    source-type: local
    build-packages:
    - fpc
    - lazarus
    - lcl
    - lcl-utils
    - build-essential
    - libqt5pas-dev
    stage-packages:
    - libqt5pas1
    - libatk1.0-0
    - libgtk2.0-0
    - libxinerama1
    override-build: |
      lazbuild -B Cantara.lpi --ws=qt5
      cp -r languages $SNAPCRAFT_PART_INSTALL/
      install cantara $SNAPCRAFT_PART_INSTALL/
      install cantara.desktop $SNAPCRAFT_PART_INSTALL/
      install Cantara.ico $SNAPCRAFT_PART_INSTALL/
      install Cantara.png $SNAPCRAFT_PART_INSTALL/
    stage:
    - lib
    - usr
    - etc
    - cantara
    - languages
    - cantara.desktop
    - Cantara.ico
    - Cantara.png
  cleanup:
    after: [cantara]
    plugin: nil
    build-snaps: [kde-frameworks-5-91-qt-5-15-3-core20]
    override-prime: |
      set -eux
      for snap in "kde-frameworks-5-91-qt-5-15-3-core20"; do  # List all content-snaps you're using here
        cd "/snap/$snap/current" && find . -type f,l -exec rm -f "$SNAPCRAFT_PRIME/{}" "$SNAPCRAFT_PRIME/usr/{}" \;
      done
      for cruft in bug lintian man icons; do
        rm -rf $SNAPCRAFT_PRIME/usr/share/$cruft
      done
      find $SNAPCRAFT_PRIME/usr/share/doc/ -type f -not -name 'copyright' -delete
      find $SNAPCRAFT_PRIME/usr/share -type d -empty -delete
      find $SNAPCRAFT_PRIME/usr/lib -type f,l -name 'libQt*.so*' ! -name 'libQt5Pas*.so*' -delete

apps:
  cantara:
    extensions:
      - kde-neon
    command: cantara
    desktop: cantara.desktop
    plugs:
    - home
    - network
    - network-bind

It launches and looks okayish. Maybe some spacing issues here & there. You could try setting QT_STYLE_OVERRIDE to one of the other themes available in the content snap to see if that looks better.

Okay, I’ve updated my snapcraft.yaml now following your hints:

name: cantara
version: '2.2.3'
summary: A Song Presentation Software.
description: |
  A simple, leightweight but powerful program for song presentation in churches, small groups or at karaoke parties.
confinement: strict
icon: Cantara.png
base: core20
grade: stable

architectures:
  - build-on: amd64

parts:
  cantara:
    plugin: nil
    source: .
    source-type: local
    build-packages:
    - fpc
    - lazarus
    - lcl
    - lcl-utils
    - build-essential
    - libqt5pas-dev
    - libqt5pas1
    stage-packages:
    - libqt5pas1
    - libatk1.0-0
    - libgtk2.0-0
    - libxinerama1
    override-build: |
      lazbuild -B Cantara.lpi --ws=qt5
      cp -r languages $SNAPCRAFT_PART_INSTALL/
      install cantara $SNAPCRAFT_PART_INSTALL/
      install cantara.desktop $SNAPCRAFT_PART_INSTALL/
      install Cantara.ico $SNAPCRAFT_PART_INSTALL/
      install Cantara.png $SNAPCRAFT_PART_INSTALL/
    stage:
    - lib
    - usr
    - etc
    - cantara
    - languages
    - cantara.desktop
    - Cantara.ico
    - Cantara.png
  cleanup:
    after: [cantara]
    plugin: nil
    build-snaps: [kde-frameworks-5-91-qt-5-15-3-core20]
    override-prime: |
      set -eux
      for snap in "kde-frameworks-5-91-qt-5-15-3-core20"; do  # List all content-snaps you're using here
        cd "/snap/$snap/current" && find . -type f,l -exec rm -f "$SNAPCRAFT_PRIME/{}" "$SNAPCRAFT_PRIME/usr/{}" \;
      done
      for cruft in bug lintian man; do
        rm -rf $SNAPCRAFT_PRIME/usr/share/$cruft
      done
      find $SNAPCRAFT_PRIME/usr/share/doc/ -type f -not -name 'copyright' -delete
      find $SNAPCRAFT_PRIME/usr/share -type d -empty -delete
      find $SNAPCRAFT_PRIME/usr/lib -type f,l -name 'libQt*.so*' -delete

apps:
  cantara:
    extensions:
      - kde-neon
    command: cantara
    desktop: cantara.desktop
    plugs:
    - home
    - opengl
    - desktop
    - desktop-legacy
    - x11
    - unity7

Building complets, but when I want to start Cantara, I get the following error:

/snap/cantara/21/cantara: error while loading shared libraries: libQt5Pas.so.1: cannot open shared object file: No such file or directory

Which I can not understand – because I have added libqt5pas1 to the stage-packages…

Because of this ^

See the amended command in my post above. But that’s already fine-tuning. Those aren’t the big fish to fry.

Thank you so much! I will try out! Seems you have solved that problem :smile:

After trying out your snapcraft.yaml on Ubuntu and Arch, I can confirm that it works well and also the design issues are not there anymore. Thank you again for the quick and professional help!

1 Like