Skip to content

Commit

Permalink
Merge pull request #8 from gmodena/unistall-removed-packages
Browse files Browse the repository at this point in the history
installer: manage application removal from config.
  • Loading branch information
gmodena authored Nov 8, 2023
2 parents 0a42864 + 8b1c489 commit 66e6211
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 88 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ with homebrew on nix-darwin.
the target system state description is not exhaustive, and there's room for divergence across builds
and rollbacks.
For a number of desktop application I want to be able to track the lastet version, or allow them to auto update.
For such applications, a convergent approach is a reasonable tradeoff wrt system reproducibility. YMMW.
For such applications, a convergent approach is a reasonable tradeoff wrt system reproducibility. YMMV.

Flatpak applications are installed by systemd oneshot service triggered at system activation. Depending on
the number of applications to install, this could increase activation time significantly.
Expand Down Expand Up @@ -98,4 +98,19 @@ Auto updates trigger on system activation.

Under the hood, updates are scheduled by realtime systemd timers. `onCalendar` accepts systemd's
`update.auto.OnCalendar` expressions. Timers are persisted across sleep / resume cycles.
See https://wiki.archlinux.org/title/systemd/Timers for more information.
See https://wiki.archlinux.org/title/systemd/Timers for more information.

### Storage
Flatpaks are stored out of nix store at `/var/lib/flatpak` and `${HOME}/.local/share/flatpak/` for system
(`nixosModules`) and user (`homeManagerModules`) installation respectively.
Flatpaks isntallation are not generational: upon a system rebuild and rollbacks, changes in packages declaration
will result in downloading applications anew.

Keeping flatpaks and nix store orthogonal is an explicit design choice, dictate by my use cases:
1. I want to track the latest version of all installed applications.
2. I am happy to trade network for storage.

YMMV.

If you need generational builds, [declarative-flatpak](https://github.com/GermanBread/declarative-flatpak)
might be a better fit.
83 changes: 0 additions & 83 deletions flake.lock

This file was deleted.

2 changes: 1 addition & 1 deletion modules/home-manager.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ in
};
Service = {
Type = "oneshot";
ExecStart = "${import ./installer.nix {inherit cfg pkgs; installation = installation; }}";
ExecStart = "${import ./installer.nix {inherit cfg pkgs lib; installation = installation; }}";
};
};

Expand Down
21 changes: 20 additions & 1 deletion modules/installer.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
{ cfg, pkgs, installation ? "system", ... }:
{ cfg, pkgs, lib, installation ? "system", ... }:

let
updateApplications = cfg.update.onActivation || cfg.update.auto.enable;
applicationsToKeep = lib.strings.concatStringsSep " " (map (builtins.getAttr "appId") cfg.packages);
flatpakUninstallCmd = installation: {}: ''
APPS_TO_KEEP=("${applicationsToKeep}")
# Get a list of currently installed Flatpak application IDs
INSTALLED_APPS=$(${pkgs.flatpak}/bin/flatpak --${installation} list --app --columns=application | ${pkgs.gawk}/bin/awk '{print ''$1}')
# Iterate through the installed apps and uninstall those not present in the to keep list
for APP_ID in $INSTALLED_APPS; do
if [[ ! " ''${APPS_TO_KEEP[@]} " =~ " ''${APP_ID} " ]]; then
${pkgs.flatpak}/bin/flatpak uninstall --${installation} -y ''$APP_ID
fi
done
'';

flatpakInstallCmd = installation: update: { appId, origin ? "flathub", commit ? null, ... }: ''
${pkgs.flatpak}/bin/flatpak --${installation} --noninteractive --no-auto-pin install \
${if update && commit == null then ''--or-update'' else ''''} ${origin} ${appId}
Expand All @@ -28,6 +43,10 @@ pkgs.writeShellScript "flatpak-managed-install" ''
# Configure remotes
${mkFlatpakAddRemoteCmd installation cfg.remotes}
# Uninstall packages that have been removed from services.flatpak.packages
# since the previous activation.
${flatpakUninstallCmd installation {}}
# Install packages
${mkFlatpakInstallCmd installation updateApplications cfg.packages}
''
2 changes: 1 addition & 1 deletion modules/nixos.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ in
];
serviceConfig = {
Type = "oneshot";
ExecStart = "${import ./installer.nix {inherit cfg pkgs; installation = installation; }}";
ExecStart = "${import ./installer.nix {inherit cfg pkgs lib; installation = installation; }}";
};
};
systemd.timers."flatpak-managed-install" = lib.mkIf config.services.flatpak.update.auto.enable {
Expand Down

0 comments on commit 66e6211

Please sign in to comment.