Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removal of no longer managed entries fail if already manually removed. #126

Open
thomasfinstad opened this issue Dec 21, 2024 · 0 comments

Comments

@thomasfinstad
Copy link

This is kind of an issue for those of us that like me prefer to configure my flatpaks interactively, then dump it into a nix file so my desired state is declarative.

During the removal of [packages] (

${pkgs.flatpak}/bin/flatpak uninstall --${installation} -y $APP_ID
) the module assumes that the saved state is perfect, and will fail if the user have manually uninstalled the package beforehand. A simple check beforehand to make sure it is installed, something like ${pkgs.flatpak}/bin/flatpak info "$APP_ID" && ${pkgs.flatpak}/bin/flatpak uninstall --${installation} -y $APP_ID would fix this.

It seems the remotes already have this style of check implemented, just a tiny bit differently than what I did above.

if ${pkgs.flatpak}/bin/flatpak --${installation} remotes --columns=name | grep -q "^$REMOTE_NAME$"; then
${pkgs.flatpak}/bin/flatpak remote-delete ${if uninstallUnmanaged then " --force " else " " } --${installation} $REMOTE_NAME
else
echo "Remote '$REMOTE_NAME' not found in flatpak remotes"
fi

PS: not sure if anyone cares, but this is currently my setup to "save" the live flatpaks to nix:

flatpak.nix
{
  home.packages = with pkgs; [
    flatpak

    (pkgs.writeShellApplication {
      name = "zzz-save-flatpak2nix";
      runtimeInputs = with pkgs; [
        flatpak
        gnused
        coreutils
        nixfmt
      ];

      derivationArgs = {
        nativeBuildInputs = [ pkgs.installShellFiles ];
      };
      excludeShellChecks = [ "SC2145" ];
      text = builtins.readFile ./save-flatpak2nix.sh;
    })
  ];

...

  # Use the shell script zzz-save-flatpak2nix to update desired remotes and packages from live system.
  services.flatpak = import ./flatpak2nix.nix // {
    enable = true;
...
  };
...
save-flatpak2nix.sh
#!/usr/bin/env bash

# Start
export LC_ALL=C
OUTPUT+="# Generated by $0\n"
OUTPUT+="{\n"

###
# Remotes
OUTPUT+="remotes = [\n"

OUTPUT+="$(flatpak remotes --columns=name,url | sort | sed -E 's/(.*)\t(.*)/{\nname="\1";\nlocation="\2";\n}\n/')"

OUTPUT+="];\n"

###
# Packages
OUTPUT+="packages = [\n"
readarray -t apps < <(flatpak list --app --columns=origin,ref)
readarray -t runtimes < <(flatpak list --runtime --columns=origin,ref)

OUTPUT+="\n###\n# Apps\n"
app_output=""
for app in "${apps[@]}"; do
  readarray -t -d $'\t' fpkg < <(echo -n -e "$app")

  if [ "${fpkg[0]}" == "flathub" ]; then
  	app_output+="\"${fpkg[1]}\"\n"
  else
  	app_output+="{origin=\"${fpkg[0]}\";appId=\"${fpkg[1]}\";}\n"
  fi
done
OUTPUT+="$(echo -e "$app_output" | sort -r)\n"

OUTPUT+="\n###\n# Runtimes that directly extends an app\n"
runtime_output=""
for runtime in "${runtimes[@]}"; do
  readarray -t -d $'\t' fpkg < <(echo -n -e "$runtime")
  extends="$(flatpak info -m "${fpkg[1]}" | sed -n -E -e '/ExtensionOf/,/^$/s/ref=((app|runtime)\/)?(.*)/\3/p')"

  if [ -n "$extends" ] && grep "$extends" <<<"${apps[*]}" >/dev/null; then
  	if [ "${fpkg[0]}" == "flathub" ]; then
  		runtime_output+="\"${fpkg[1]}\"\n"
  	else
  		runtime_output+="{origin=\"${fpkg[0]}\";appId=\"${fpkg[1]}\";}\n"
  	fi
  	#else
  	# if [ "${fpkg[0]}" == "flathub" ]; then
  	# 	runtime_output+="#\"${fpkg[1]}\"\n"
  	# else
  	# 	runtime_output+="#{origin=\"${fpkg[0]}\";appId=\"${fpkg[1]}\";}\n"
  	# fi
  fi
done
OUTPUT+="$(echo -e "$runtime_output" | sort -r)\n"
OUTPUT+="];\n"

# Fin
OUTPUT+="}"
echo -e "$OUTPUT" | tee "$HOME/code/nixos-config/home/$USER/apps/flatpak2nix.nix"
sync
nixfmt "$HOME/code/nixos-config/home/$USER/apps/flatpak2nix.nix"
example snippet of flatpak2nix.nix
{
  remotes = [
    {
      name = "Slicer";
      location = "https://rafaelpalomar.github.io/org.slicer.Slicer-flatpak-repository";
    }
    {
      name = "flathub";
      location = "https://dl.flathub.org/repo/";
    }
  ];
  packages = [

    ###
    # Apps
    {
      origin = "Slicer";
      appId = "org.slicer.Slicer/x86_64/master";
    }
    "xyz.tytanium.DoorKnocker/x86_64/stable"
    "rocks.koreader.KOReader/x86_64/stable"
    "rest.insomnia.Insomnia/x86_64/stable"
...

    ###
    # Runtimes that directly extends an app
...
    "io.github.ungoogled_software.ungoogled_chromium.Codecs/x86_64/stable"
    "com.jeffser.Alpaca.Plugins.AMD/x86_64/stable"
  ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant