Nixos custom module configuration with flakes and home-manager

Actually you can access the NixOS system configuration from a Home Manager module — in addition to the usual config argument (which holds the Home Manager config in that case), Home Manager modules also get the nixosConfig argument, which is the same as config in a regular NixOS module. For some reason this is not mentioned in the Home Manager documentation.

I did not actually try to use that support with custom NixOS options like yours, but this code currently works for me to get some host-specific Home Manager configuration (as other comments here suggest, this might not be a proper long term solution):

home-manager.users.foo = { lib, pkgs, config, nixosConfig, ... }: {

  # Import the per-host configuration module if it is present.
  imports =
    let
      hostModules = {
        bar = ./host_bar.nix;
        baz = ./host_baz.nix;
      };
      thisHostName = nixosConfig.networking.hostName or null;
      thisHostModule = lib.mapNullable (n: hostModules.${n} or null) thisHostName;
    in
    lib.optional (thisHostModule != null) thisHostModule;

  # ... the rest ...
}

Also apparently there is a similar darwinConfig argument if you use Home Manager together with nix-darwin, and even a generic osConfig argument which holds either NixOS or nix-darwin config, so you may write a Home Manager module which works with both OS types if the OS-level config options that you want to use have the same names.

1 Like