图片来源:@Lifeline    88401620
什么是Flathub?
- Flathub之于Flatpak,正如Snapcraft之于Snap
- 类似于“官方Repo”,但是由社区维护
 
- 与AUR不同,Flathub存储了软件的二进制文件,而不只是构建文件
 
- Flathub有着自己的大型CI集群Flathub Buildbot
 
 
- 安装发行版上的Flatpak时,不一定默认启用Flathub,需要手动查看和添加
 
启用Flathub
- 如果没有
flathub源,就需要手动添加 
- 添加Flathub源
 
1
   | flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
   | 
 
搭建Flathub镜像
- Flatpak Repo的本质就是一个OSTree Repo,Flathub也不例外
 
- Flathub的OSTree Repo地址可从flatpakrepo文件中找到:
https://dl.flathub.org/repo/ 
- 使用该地址可以进行部分镜像或全量镜像
 
编外:RSYNC同步全量镜像
操作流程:
- 安装
rsync 
- 下载脚本
 
- 创建文件夹
 
- 运行
rsync-repos脚本 
1 2 3 4 5 6 7 8
   |  sudo dnf install rsync
  wget https://raw.githubusercontent.com/ostreedev/ostree-releng-scripts/master/rsync-repos
  mkdir repo/
  python3 ./rsync-repos --src rsync://abc.com/ --dst ./repo/ 
 
  | 
 
OSTree同步镜像
- 可以自己选择同步哪些软件包(以及哪些架构和版本),也可以自行对软件包的历史进行控制
 
- 需要的存储空间和同步时间不定,全量镜像可能需要3TB以上存储空间
 
- 直接使用
ostree,需要编写脚本 
操作流程:
- 安装
ostree和flatpak 
- 创建本地的OSTree Repo
 
- 给本地的OSTree Repo添加Flathub的Remote
 
- 添加并信任Flathub的GPGKey
 
- 编写脚本,选择Ref进行同步
 
- 删除无用的OSTree Repo Commit历史
 
- 生成新的OSTree Summary和Flatpak Repo Summary
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
   |  sudo dnf install flatpak ostree
  mkdir repo
  ostree init --repo=./repo --mode=archive --collection-id=org.flathub.Stable
  ostree remote add --repo=./repo flathub https://dl.flathub.org/repo/
 
  flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
  gpg --import /var/lib/flatpak/repo/flathub.trustedkeys.gpg echo -e "4\ny\n" | gpg --command-fd 0 --expert --edit-key flathub@flathub.org trust
  ostree --repo=./repo remote gpg-import -k /var/lib/flatpak/repo/flathub.trustedkeys.gpg flathub
 
  | 
 
然后编写同步脚本并运行,脚本示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
   |  REPO="./repo"
  declare -a FLATHUB_REFS declare -a PATTERNS declare -A SYNC_LIST
 
 
  PATTERNS+=( \     "runtime/org.gnome.Sdk" \     "runtime/org.gnome.Platform" \     "runtime/org.kde.Platform" \     "runtime/org.kde.Sdk" \     "runtime/org.kde.Platform" \     "runtime/org.freedesktop.Platform" \     "runtime/org.freedesktop.Sdk" \     "runtime/io.elementary.Platform" \     "runtime/io.elementary.Sdk" \ );
  PATTERNS+=( \     "appstream/" \     "appstream2/" \ );
  PATTERNS+=( \     "org.blender.Blender.Codecs" \ );
  PATTERNS+=( \     "org.blender.Blender" \     "com.usebottles.bottles" \     "com.github.tchx84.Flatseal" \     "com.moonlight_stream.Moonlight" \     "com.obsproject.Studio" \     "io.typora.Typora" \     "org.kde.krita" \     "org.winehq.Wine" \     "org.electronjs.Electron2.BaseApp" \ );
 
  filter_list() {     local SYNC_LIST_TMP+=( $(printf "%s\n" "${FLATHUB_REFS[@]}" | grep $1) );     for item in "${SYNC_LIST_TMP[@]}"     do         SYNC_LIST[$item]=1;     done }
 
  main() {          FLATHUB_REFS+=( $(ostree remote refs --repo=${REPO} flathub) );               for pat in "${PATTERNS[@]}"     do         filter_list $pat;     done               for item in "${!SYNC_LIST[@]}"     do         echo "Syncing: ${item}";         ostree --repo=$REPO pull --mirror $item;     done }
  main;
 
  | 
 
脚本运行完毕后:
1 2 3 4 5
   |  ostree --repo=./repo prune --keep-younger-than="7 day ago"
  ostree summary --repo=./repo --update flatpak build-update-repo ./repo
 
  | 
 
使Flathub镜像对外服务
- 使用Caddy自动配置HTTPS,并分发Flatpak Repo的文件:
 
1 2 3 4
   | abc.com
  root * /home/abc/repo file_server
   | 
 
1 2 3 4 5 6 7
   | [Flatpak Repo] Title=Flathub Mirror Url=https://abc.com/ Homepage=https://abc.com/ Comment=Flathub Mirror Description=Flathub Mirror Icon=
   | 
 
- 可以将
.flatpakrepo文件保存在本地,也可保存在Flathub Mirror Repo下面,如:https://abc.com/repo.flatpakrepo 
- 在需要使用Flathub镜像的Linux主机上,使用如下命令配置Flatpak:
 
1
   | flatpak remote-add --no-gpg-verify flathub-mirror https://abc.com/repo.flatpakrepo
   | 
 
- 随后即可使用Flathub Mirror。此时可以删除Flathub官方源。
 
也可以对Flathub Mirror进行GPG签名,可以参照flatpak build-update-repo命令文档中的--gpg-sign和--gpg-homedir参数,并在.flatpakrepo文件中加入GPG字段。