Confusion about proper way to setup Flathub

I’m a totally new NixOS user, I am currently experimenting with it to see if I am able to switch to it.
I read a few things about Nix, and watched some videos about it.

One of the things I wanted to do was setting up flatpak and flathub since I use quite a few flatpak-distributed apps. Here’s a wiki article about flatpak. The first part seems completely fine.I need to enable flatpak with a line in my configuration.nix. However, adding Flathub repository is shown as follows:

flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo

Like I said, I’m completely new to Nix, but I find it confusing that in order to add the flatpak repo, you need to manually execute the above command. I’d expect it would be a part of configuration.nix, to keep the reproducibility thing going. Right now I’m not sure how to interpret that instruction. Does it mean that NixOS cannot actually be fully defined via *.nix files? Do I still have to maintain .sh scripts to set up my environment, like I do it now with Fedora or other distros?

1 Like

This is normal for Flatpak. If you install another barebones distro and then install flatpak yourself, you will still have to manually add the flathub repository. You can also add the flathub repository by installing a flatpak file downloaded from the flathub website, this way you don’t have to look for the command or memorize it every time.

I’m not aware of a way to manage flatpak declaratively. Maybe there is one, but I’m not sure. In a way, it’s similar to launching standalone binaries and appimages that you downloaded yourself from a website, it wouldn’t be declarative.

Thanks @spacebanana. Ok, I actually expected it to be a bit different. With NixOS, to keep my configuration documented, I’d need to keep both configuration.nix and shell scripts to configure/install things that configuration.nix does not support.

Is it something expected with NixOS, or do NixOS users typically install all their apps from nixpkgs? The issue I see with nixpkgs is that packages there (at least those I use) are in old versions, or simply are not there at all.

NixOS has the unstable branch for a rolling-release style, but some packages even in that branch can be weirdly outdated, some even missing yes.

I have heard that Nix has an AUR equivalent called NUR but I have yet to use it.

I try to avoid using anything but nixpkgs but sometimes flatpaks or even binaries/appimages are a necessary “evil”

which NixOS config are you using that flatpak still works?

Well, I didn’t actually yet get to using it, I’ve been reading the docs, and I wanted to consult the forum here before doing any actual changes to my system.

Yes in general it would make more sense to get the packages from nixpkgs rather than a Flatpak since it integrates better with the configuration.
If you require a package to be the latest version you can install it from unstable.

There are of course uses cases for Flatpak, e.g. if the package isn’t in nixpkgs yet or is outdated for whatever reason.

From what I’ve seen it looks like Flatpak doesn’t really have a configuration file?
I reckon this makes it harder to configure it., at least that was my experience with Gnome.
Home-manager executed a script every rebuild but you then could change the settings in the Gnome interface and changes could get lost.
I guess there would be a similar behaviour with Flatpak if it really stores its config in a DB.
However maybe a bit less problematic than Gnome.

I was considering switching from Silverblue to NixOS, but I found this odd as well. My interest is in having a completely reproducible build. I have one less requirement in that I only want flathub setup, but I don’t want any of the installed flatpak’s to be part of the configuration.nix. I was going to allow flatpak through gnome-software to be a good way for users to install their own software that I don’t need to keep as part of the versioned configuration.

Even trying to use it this way seems problematic though, since running flatpak add-remote is required after setting up the system. Could I create a flake for running this command?

The next thing I was going to look into was joining these systems to an Active Directory domain. This seems like it would have similiar problems in that I would have to manually run realm join.

I think NixOS sounds like a wonderful idea, but without some way to do ‘all’ of the configuration I am back to keeping a notebook of steps to run on every install or update.

I am hoping someone on here can help me fix this situation.

System-wide remotes can be statically preconfigured by dropping flatpakrepo files into /etc/flatpak/remotes.d/.

From Flatpak Command Reference - Flatpak documentation

{pkgs, ...}: {
  environment.etc = {
    "flatpak/remotes.d/flathub.flatpakrepo".source = pkgs.fetchurl {
      url = "https://dl.flathub.org/repo/flathub.flatpakrepo";
      # Let this run once and you will get the hash as an error.
      hash = "";
    };
  };
}
4 Likes

Thank you! That is exactly what I needed!

This code created the file correctly, but flatpak doesn’t seem to see the remote. I will mess around with it some more and report back.

1 Like

I have tried all manner of variations on this and cannot seem to get it to work. I first tested it by doing a
flatpak --remote-delete flathub and then running nixos-rebuild switch
with the new config. Since I was worried that the remote-delete might have left some marker I also completely rebuilt my system from scratch (suprisingly easy with NixOS :slight_smile: ) with no luck.
I posted a question on this to the flathub discourse, but currently have no responses.

If I could do this the most ideal way that would be nice, but until that option is available I think having another way to put this in the configuration.nix would be better than resorting to doing it manually.

I know this will be unpopular, but can I just create a flathub package which runs
flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo on install,
flatpak remote-delete flathub on uninstall,
and depends on the flatpak package?

Is there some other way to do this that I haven’t thought of?

It appears that adding the flatpakrepo with:

  # This doesn't work on NixOS for some reason.
  environment.etc = {
   "flatpak/remotes.d/flathub.flatpakrepo".source = pkgs.fetchurl {
     url = "https://dl.flathub.org/repo/flathub.flatpakrepo";
     hash = "";
   };
 };

doesn’t work with NixOS for some reason (atleast in 23.05).

I know many won’t like it, but for now if you need a soluton; this does work:

  system.activationScripts = {
    flathub = ''
      /run/current-system/sw/bin/flatpak remote-add --system --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
    '';
  };

what is the difference to

flathub = { text = ''
      /run/current-system/sw/bin/flatpak remote-add --system --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
    '';

Would this info be helpful?

https://github.com/search?q=repo%3Awimpysworld%2Fnix-config+Flathub&type=commits

Cheers

I am not sure what the difference is. I didn’t see this solution posted anywhere. Should I do it this way instead?

I am still very new to NixOS and haven’t tried home-manager or flakes yet. So far everything I have done in NixOS has worked easier and more reliably than any distro I have used before. I am very excited to keep learning more.

Thanks for the link.

Check out GitHub - GermanBread/declarative-flatpak

2 Likes