About me: My name is Solène Rapenne, pronouns she/her. I like learning and sharing knowledge. Hobbies: '(NixOS BSD OpenBSD Lisp cmdline gaming internet-stuff). I love percent and lambda characters. OpenBSD developer solene@.

Contact me: solene+www at dataswamp dot org or @solene@bsd.network (mastodon). If for some reason you want to support my work, this is my paypal address: donate@perso.pw.

Consider sponsoring me on Patreon to help me writing this blog and contributing to Free Software as my daily job.

How to setup a local network cache for Flatpak

Written by Solène, on 05 April 2023.
Tags: #linux #flatpak #efficiency

Comments on Fediverse/Mastodon

1. Introduction §

As you may have understood by now, I like efficiency on my systems, especially when it comes to network usage due to my poor slow ADSL internet connection.

Flatpak is nice, I like it for many reasons, and what's cool is that it can download only updated files instead of the whole package again.

Unfortunately, when you start using more and more packages that are updated daily, and which require subsystems like NVIDIA drivers, MESA etc... this adds up to quitea lot of daily downloads, and multiply that by a few computers and you gets a lot of network traffic.

But don't worry, you can cache it on your LAN to download updates only once.

2. Setup §

As usual for this kind of job, we will use Nginx on a local server on the network, and configure it to act as a reverse proxy to the flatpak repositories.

This requires modifying the URL of each flatpak repository on the machines, it's a one time operation.

Here is the configuration you need on your Nginx to proxy Flathub:

map $status $cache_header {
    200     "public";
    302     "public";
    default "no-cache";

server {
    listen; # you may want to listen on port 80, or add TLS
    server_name my-cache.local; # replace this with your hostname, or system IP

    # flathub cache
    set $flathub_cache https://dl.flathub.org;

    location /flathub/ {
        rewrite  ^/flathub/(.*) /$1 break;
        proxy_cache flathub;
        proxy_cache_key     "$request_filename";
        add_header Cache-Control $cache_header always;
        proxy_cache_valid   200 302     300d;
        expires max;
        proxy_pass  $flathub_cache;

proxy_cache_path    /var/cache/nginx/flathub/cache levels=1:2

This will cause nginx to proxy requests to the flathub server, but keep files in a 20 GB cache.

You will certainly need to create the /var/cache/nginx/flathub directory, and make sure it has the correct ownership for your system configuration.

If you want to support another flatpak repository (like Fedora's), you need to create a new location, and new cache in your nginx config.

3. Client configuration §

On each client, you need to change the URL to reach flathub, in the example above, the URL is http://my-cache.local:8080/flathub/repo/.

You can change the URL with the following command:

flatpak remote-modify flathub --url=http://my-cache.local:8080/flathub/repo/`

Please note that if you add flathub repo, you must use the official URL to have the correct configuration, and then you can change its URL with the above command.

4. Revert the changes §

If you don't want to use the cache anymore , just revert the flathub url to its original value:

flatpak remote-modify flathub --url=https://dl.flathub.org/repo/

5. Conclusion §

Our dear nginx is still super useful as a local caching server, it's super fun to see some updates going at 100 MB/s from my NAS now.