Declaring flatpaks in home-manager

Hi! I am dreaming about declaring flatpak in my home-manager file. I think that it is possible with this:

Here is my flake.nix:

{
  description = "Nixos config flake";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    nix-flatpak.url = "github:gmodena/nix-flatpak/";
    home-manager = {
      url = "github:nix-community/home-manager";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };
  outputs = { self, nixpkgs, nix-flatpak, home-manager, ... }@inputs:
    let
      system = "x86_64-linux";
      pkgs = "nixpkgs.legacyPackages.${system}";
    in
    { 
      nixosConfigurations.default = nixpkgs.lib.nixosSystem {
        specialArgs = {inherit inputs;};
        modules = [
          nix-flatpak.nixosModules.nix-flatpak
          home-manager.nixosModules.home-manager
          {
            home-manager.useGlobalPkgs = true;
            home-manager.useUserPackages = true;
            home-manager.extraSpecialArgs.flake-inputs = inputs;
          }
          ./configuration.nix
        ];
    };
  };
}

But when I try to add “services.flatpak” to my home.nix I get this error:

[jeansib@dell-3:~]$ home-manager switch -b backup
error:
       … while evaluating the attribute 'activationPackage'
         at /nix/store/5b0mw0h1cwwhvf32z5kjym00dd5saydj-home-manager-source/modules/default.nix:49:7:
           48|
           49|       activationPackage = module.config.home.activationPackage;
             |       ^
           50|

       … while evaluating a branch condition
         at /nix/store/bfr3xwnd4qam39i69czczlxjn1pmv340-nixos/nixos/lib/lists.nix:125:9:
          124|       fold' = n:
          125|         if n == len
             |         ^
          126|         then nul

       (stack trace truncated; use '--show-trace' to show the full, detailed trace)

       error: The option `services.flatpak' does not exist. Definition values:
       - In `/home/jeansib/.config/home-manager/home.nix':
           {
             enable = true;
             packages = [
               "im.riot.Riot"
             ];
           ...

[jeansib@dell-3:~]$ 

I am though able to declare flatpak globally in /etc/nixos/configuration.nix
Is there some workaround?

services.flatpak.enable is a NixOS option. You’ll need to enable it in your NixOS config. nix-flatpak can be configured either in your NixOS or Home-Manager config.

This block is both in /etc/nixos/configuration.nix and in home-manager config:

  services.flatpak = {
    enable = true;
    packages = [
      "im.riot.Riot"
      "com.brave.Browser"
    ];
  };

I can normally rebuild whole NixOS, but when I run home-manager switch -b backup I get the error.

Because you appear to have confused standalone HM vs HM as a NixOS module. Pick exactly one of those options. Then, import the nix-flatpak HM module in the applicable spot.

And as already said. services.flatpak.enable is a NixOS option. You must set that in NixOS config because iirc flatpak requires system-level support. You may also need to enable it in the HM config depending on how the HM nix-flatpak module works.

Okay, I am really sorry, but I don’t understand. Don’t get me wrong, I spend a lot of time reading documentation and tutorials, but I just don’t get it. What is the difference between standalone HM and HM as a module? How do I implement one and the other? What does it mean that “services.flatpak.enable” is a NixOS option? I am running NixOS. I thought that I set this option in /etc/nixos/configuration.nix (see below).

For reference this is my flake.nix:

{
  description = "Nixos config flake";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    nix-flatpak.url = "github:gmodena/nix-flatpak/";
    home-manager = {
      url = "github:nix-community/home-manager";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };
  outputs = { self, nixpkgs, nix-flatpak, home-manager, ... }@inputs:
    let
      system = "x86_64-linux";
      pkgs = "nixpkgs.legacyPackages.${system}";
    in
    {
      nixosConfigurations.default = nixpkgs.lib.nixosSystem {
        specialArgs = {inherit inputs;};
        modules = [
          nix-flatpak.nixosModules.nix-flatpak
          home-manager.nixosModules.home-manager
          {
            home-manager.useGlobalPkgs = true;
            home-manager.useUserPackages = true;
            home-manager.extraSpecialArgs.flake-inputs = inputs;
          }
          ./configuration.nix
        ];
    };
  };
}

This is my /etc/nixos/configuration.nix (I deleted those, which I think are irrelevant):

{ config, pkgs, ... }:
{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  nix.settings.experimental-features = [
    "nix-command"
    "flakes"
  ];

  networking.hostName = "dell-3";

  services.flatpak = {
    enable = true;
    packages = [
      "im.riot.Riot"
      "com.brave.Browser"
    ];
  };

  # users:
  users.users.jeansib = {
    isNormalUser = true;
    description = "Jean Sibelius";
    extraGroups = [ "networkmanager" "wheel" ];
    packages = with pkgs; [
      kdePackages.kate
    ];
  };
  users.users.rozliczenia = {
    isNormalUser = true;
    description = "rozliczenia";
    extraGroups = [ "networkmanager" ];
    packages = with pkgs; [
      kdePackages.kate
    ];
  };
  users.users.jacekrozycki = {
    isNormalUser = true;
    description = "Jacek Różycki";
    extraGroups = [ "networkmanager" ];
    packages = with pkgs; [
      kdePackages.kate
      pkgs.tor-browser
    ];
  };
  system.autoUpgrade.enable = true;
  environment.systemPackages = with pkgs; [
    pkgs.htop
    pkgs.clamav
    pkgs.tlp
    pkgs.home-manager
  ];
  system.stateVersion = "24.11"; # Did you read the comment?
}

I tested multiple times rebuilding nixos and installing flatpak this way works:

[root@dell-3:~]# nixos-rebuild switch --flake /etc/nixos#default
building the system configuration...
stopping the following units: accounts-daemon.service, avahi-daemon.service, avahi-daemon.socket, cups-browsed.service, cups.service, cups.socket, nscd.service, systemd-timesyncd.service
activating the configuration...
removing group ‘avahi’
removing user ‘avahi’
setting up /etc...
reloading user units for jeansib...
restarting sysinit-reactivation.target
reloading the following units: dbus.service, firewall.service
restarting the following units: polkit.service
starting the following units: accounts-daemon.service, cups.socket, nscd.service, systemd-timesyncd.service
the following new units were started: run-credentials-systemd\x2dtmpfiles\x2dresetup.service.mount, sysinit-reactivation.target, systemd-localed.service, systemd-tmpfiles-resetup.service

[root@dell-3:~]# 

This is my home-manager config:

{config, pkgs, nix-flatpak, ...}:

{
  nixpkgs.config.allowUnfree = true;
  home = {
    stateVersion = "24.11";
    username = "jeansib";
    homeDirectory = "/home/jeansib";
    packages = [
      pkgs.vscodium
      pkgs.syncthing
      pkgs.libreoffice
      pkgs.vlc
      pkgs.discord
      pkgs.rpi-imager
     ];
  };

  services.flatpak = {
    enable = true;
    packages = [
      "im.riot.Riot"
      "com.brave.Browser"
    ];
  };

  systemd.user.services = {
    syncthing = {
      Unit = {
        Description = "Start syncthing.";
      };
      Service = {
        Type = "oneshot";
        ExecStart = ''${pkgs.syncthing}/bin/syncthing'';
      };
      Install = {
        WantedBy = [ "default.target" ];
      };
    };

But I get this error when trying to switch HM:

[jeansib@dell-3:~]$ home-manager switch -b backup
error:
       … while evaluating the attribute 'activationPackage'
         at /nix/store/5b0mw0h1cwwhvf32z5kjym00dd5saydj-home-manager-source/modules/default.nix:49:7:
           48|
           49|       activationPackage = module.config.home.activationPackage;
             |       ^
           50|

       … while evaluating a branch condition
         at /nix/store/8fwsiv0hd7nw1brkvka0jf1frk3m7qkr-source/lib/lists.nix:125:9:
          124|       fold' = n:
          125|         if n == len
             |         ^
          126|         then nul

       (stack trace truncated; use '--show-trace' to show the full, detailed trace)

       error: The option `services.flatpak' does not exist. Definition values:
       - In `/home/jeansib/.config/home-manager/home.nix':
           {
             enable = true;
             packages = [
               "im.riot.Riot"
               "com.brave.Browser"
           ...
[jeansib@dell-3:~]$ 

What am I doing wrong?

Can you guys help me out with that?

{
      nixosConfigurations.default = nixpkgs.lib.nixosSystem {
        specialArgs = {inherit inputs;};
        modules = [
          nix-flatpak.nixosModules.nix-flatpak
          ...

The flake’s nixosModule is what you’re using here, and it’s added to your nixosConfiguration, so the options are only exposed on the nixos side.

The error is saying it doesn’t exist under home-manager, and it wouldn’t unless you import the flake’s hmModule in your home-manager config. The extraSpecialArgs allow you to do that if you want user-specific options. If you remove the options from your home-manager config, or choose to use the hmModule and remove the module/options from your nixos config, it should be good.

There are several ways home-manager can be used with nixos and it definitely gets confusing, so don’t feel bad for not getting it at first!

Thank you for your kind answer :heart_eyes: !