Prevent home-manager from installing program twice

Hello, I’m in the process of migrating from dotfiles to home-manager and have a question about how program installation is handled by home-manager.

For example I want to have git installed system wide but also configure it through home-manager. What I found out on the internet is, that home-manager will also install git and both installations of git (the system wide installed one and the one in the user profile) are present in the PATH variable, but the one in the user profile takes precedence (because it’s appears earlier in PATH than the system wide installed one).
I’m not sure if this is 100% correct, so please correct me if I’m wrong.

Now my questions:

  1. If the above is true, is git really installed twice? Like does it actually take up twice the space?
    Or does Nix detect that and create two symlinks (one for the system wide installed git and one for git in the user profile)?
  2. If it’s actually installed twice, is there a way to prevent that? (Maybe even with Nix store deduplication as “last resort”?)

Thanks in advance :slight_smile:

Nix doesn’t have to “detect” anything to keep it from being installed twice. The hash in a package’s /nix/store path is derived from the definition of the package. If two different things (e.g. NixOS and home-manager) use an identical definition, they get the same package. So as long as you’re using the same version of nixpkgs for both of them, they both just decide to use the same path by definition. And then yes, they both have their own mechanisms for setting up symlinks that ultimately appear on your PATH

1 Like

Wow, thank you so much for your quick answer!
Now I understand how it works and that I don’t need to worry about packages being installed twice.
I’m fairly new to Nix but as far as I understand the following code in my flake.nix makes sure that the same version of nixpkgs is used:

inputs = {
    nixpkgs.url = "nixpkgs/nixos-22.11";
    home-manager = {
        url = "home-manager";
        inputs.nixpkgs.follows = "nixpkgs";
    };
};

Is that correct?

Edit: Is there a difference between using nixpkgs/nixos-22.11 and nixpkgs/release-22.11?

1 Like

Yes, for the most part. Nixpkgs configuration such as overlays can also change the package hashes, however, so if you set those, you’ll need to keep them the same too in order to be completely assured that everything is shared.

Yes. release-22.11 is for internal use by developers. nixos-22.11 automatically updates to match release-22.11 when hydra has built the packages and tests have passed. Never follow release-22.11 unless you know exactly what you’re doing. It creates subtle problems of various sorts you might not notice immediately.

1 Like

Thank you very much @tejing. This really helps me a lot because I’m still unsure about a lot of things but thanks to your explanation I now at least understand the imports in my flake :slight_smile:

Actually, that is not sufficient, as per the home-manager documentation.

You can ask home-manager to use the same nixpkgs instance as your nixos system though, by setting this in your nixos config:

{
  home-manager = {
    useGlobalPkgs = true;
  };
}
1 Like

That’s very interesting @R-VdP because I saw this option yesterday in the documentation but thought it was not necessary because I’ve never seen it in any tutorial or article. Does it replace inputs.nixpkgs.follows or do I have to use both in combination?

You actually don’t need the follows, it won’t have any effect on installed packages, only on the libs HM uses for its internal “bootstrapping” in flake/standalone.

It helps though to keep the “inputs closure” (made up concept, but should be clear what I mean) reasonably sized.

To be honest, for HM used as a system module, I’d always suggest to enable the global pkgs thing, and I think it’s also like that in the HM manuals install snippets.

1 Like

Thank you for the clarification. You’re absolutely right. I just checked the home-manager documentation and if home-manager is used as a NixOS module useGlobalPkgs = true is used.
I hope it’s ok to ask this kind of follow up question: I was planning on using home-manager as NixOS module but the home-manager documentation states, that home-manager does not explicitly support rollbacks. Does that affect the ability to select an older configuration in the boot menu if I screw something up?

When using HM as a system module, then the systems manager rollback mechanisms fully apply.

In general, even with HM standalone, rollbacks are possible, though not implemented in the CLI. You have to activate previous generations manually by running their activation scripts.

When using the system module approach, the activation scripts are re-ran as part of the systems activation anyway, and that happens on each boot anyway.

1 Like