Title: How to setup a local network cache for Flatpak | |
Author: Solène | |
Date: 05 April 2023 | |
Tags: linux flatpak efficiency | |
Description: In this article, you will learn how to setup a LAN cache | |
for Flatpak to speed up updates | |
# 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. | |
# 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: | |
```nginx | |
map $status $cache_header { | |
200 "public"; | |
302 "public"; | |
default "no-cache"; | |
} | |
server { | |
listen 0.0.0.0:8080; # 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 | |
keys_zone=flathub:5m | |
max_size=20g | |
inactive=60d | |
use_temp_path=off; | |
``` | |
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. | |
# 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: | |
```shell 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. | |
# Revert the changes | |
If you don't want to use the cache anymore , just revert the flathub | |
url to its original value: | |
```shell command | |
flatpak remote-modify flathub --url=https://dl.flathub.org/repo/ | |
``` | |
# 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. |