Reference flake output from within another flake output within same flake

Is there a way to reference a flake’s nixosModules output and homeConfiguration output from within a nixosConfiguration output inside of the same flake? Furthermore, if there does happen to be a way, would I be able update my current system’s home-manager specific configuration (as it’s managed with that flake) without going through the nixos-rebuild switch?

The outputs function takes a self argument, which contains your flake’s outputs. Loosely, nix implements this with recursion of the form let self = outputs { inherit self; }; in self. So e.g. you can do this:

{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

  outputs = { nixpkgs, self }: {
    nixosModules.foo = ./foo.nix;

    nixosConfigurations.bar = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [self.nixosModules.foo];
    };
  };
}

If you’re using the home-manager NixOS module, then no. The home-manager NixOS module is activated with nixos-rebuild. But you can just use the home-manager CLI instead of the NixOS module, in which case they are indeed separately updated.

2 Likes

Wow that’s exactly what I was looking for on my first point! :pray:

To clarify on the second: if I’m simply exposing my homeConfiguration from within a nixosModule in that nixosConfiguration, and I make changes to the homeConfiguration, I can just safely “override” what’s configured in an actively used nixosConfiguration by referencing the homeConfiguration output directly with the home-manager cli?

You’re use of terminology is confusing me.

This doesn’t make sense. You don’t expose a homeConfiguration from a nixosModule. homeConfigurations is a top level output of your flake. However, home-manager does ship a nixos module that can be imported by a nixos module (or of course listed directly in the modules argument of nixpkgs.lib.nixosSystem). If you use home-manager this way, you should not attempt to use the home-manager CLI to update your home-manager configuration.

But if you use the top level homeConfigurations output, this is explicitly meant for the home-manager CLI, and you should not use the home-manager nixos module at all.

The difference I’m describing is the difference between the “Standalone setup” (section 3.2) and the “NixOS module” (section 3.3) of the home-manager documentation on flakes.

Finally there is hmModules, which is a top level output that is agnostic to this difference. These modules can be imported by home-manager setups whether they use the standalone setup or the nixos module. Though I think this might only be a common practice, and not actually a documented part of the conventional schema.

2 Likes

Ok wow. Thank you again! I wasn’t considering (or even aware of) homeManagerModules.