What happens when I enable Sway in both NixOS and Home Manager?

About two months ago, I switched from KDE to Sway. According to the wiki, Sway can be enabled via NixOS, Home Manager, or both. I followed the wiki instructions to enable Sway, using greetd as my display manager.

From my experience:

  • Enabling Sway only in NixOS works fine.

  • Enabling it only in Home Manager does not work (on my setup, Home Manager is installed via the NixOS module method with inputs.nixpkgs.follows = "nixpkgs"). I don’t remember the exact error, sorry. It seemed that greetd complained that it couldn’t find sway or something else. The certain thing I remember is sway couldn’t launch correctly. I am not sure if this error is very important for this topic.

Currently, I have Sway enabled in both NixOS (programs.sway) and Home Manager (wayland.windowManager.sway). This allows me to use more Home Manager options without errors.

So my question:
When both are enabled, what exactly happens? Does Nix build and install two separate copies of Sway? I found a thread mentioning configuration conflicts, but I haven’t experienced conflicts myself. Differences might be due to the Home Manager installation method, though I’m not sure.

And this may help:

readlink -f $(which sway) # executed by normal user
/nix/store/x7plswi14n36cm8v295ag8vj0bhqn9md-sway-1.11/bin/sway
readlink -f $(which sway) # executed by root
/nix/store/hnmskg446sngc7zg0di8l5c5aj2xi7pk-sway-1.11/bin/sway

What is your home manager config, are you setting use global pkgs to true?

Home Manager Manual If you use a flake setup which I assume since you mentioned follows, this section gives some clues. My understanding is if this is set it reuses it as if you would have installed them via configuration.nix for the user

Thanks for reply. And sry, I think I make it too vague. Let me describe it more clearly. I am actually using flake right now, and my home-manager input in flake.nix looks like this:

# inputs
inputs.home-manager = {
  url = "github:nix-community/home-manager";
  inputs.nixpkgs.follows = "nixpkgs";
};

# home-manager as nixos module
home-manager.nixosModules.home-manager {
  home-manager.useGlobalPkgs = true;
  home-manager.useUserPackages = true;
  home-manager.users.me = import ./me.nix;
  home-manager.extraSpecialArgs = {
    inherit inputs;
    pkgs-unfree = import inputs.nixpkgs { inherit system; config.allowUnfree = true; };
  };
}

I really hope that useGlobalPkgs = true will make an effort to reuse sway. But the output of readlink I pasted at bottom shows there are two different hash paths of sway in my labtop. I just don’t think I can regard this as reusing.

And another topic is still mystery. I can’t just write wayland.windowManager.sway.enable = true; in home-manager configuration and ignore programs.sway.enable = true; in nixos. It seems to be in conflict with wiki page I mentioned.

So all of these are related to title, in my opinion.

The main reason I could see to do this is to have some global config in /etc as well as your own user-specific config in ~/.config.

If the overrides used are different, or your nixpkgs revision differ, then yes.

Of course if you use HM as a NixOS module then you could set the wayland.windowManager.sway.package.package in HM config to osConfig.programs.sway.package.

Or you can set the NixOS programs.sway.package option to null as the description mentions: NixOS Search

That just shares the pkgs binding between HM and NixOS. Which is an important step, sure, but not sufficient by itself.

Thanks for the reply. And sry for late reply. I am a bit busy in real life. :wink:

That makes sense!

Oh, I mistakenly thought setting programs.sway.package to null only applied to the standalone version of home manager because of the thread I mentioned. And I actually haven’t noticed there was obvious explanation about programs.sway.package in nixos search page. It’s a deeply hidden info for me. :frowning:

So that’s why I have two Sway hash paths in my labtop, right? I have to say, NixOS is the most complex distribution I’ve ever used.

Anyways, I appreciate your patient explanation. Have a good day!

Why isn’t this sufficient?

Already explained above