Search code examples
nixnixosnix-darwinhome-manager

How do I access the nixos (or nix-darwin) config from within a home-manager module?


How do I access the NixOS config from within a home-manager module, imported automatically by home-manager? When I use config, it's home-manager config, not NixOS config.

I have a home-manager module like so:

{ pkgs, ...}:
 {
  home.packages = [
    pkgs.hello
    pkgs.cowsay
  ];
}

Included twice using the same nixos flake, for 2 different machines with different hostnames. Both set their hostname in the nixos level module using networking.hostName = "hostA" and "hostB".

Now I only want to include cowsay if this is hostB, not hostA. How?

When I do:

{ pkgs, config, ...}:
 {
  home.packages = [
    pkgs.hello
  ] ++ pkgs.lib.optionals (config.networking.hostName == "hostB") [
    pkgs.cowsay
  ];
}

I get error:

error: attribute 'networking' missing

       at /nix/store/4h4c5s050y7hrdgrzdhhqdjw92ijf5fm-source/home-manager-user.nix:5:28:

            4|     pkgs.hello
            5|   ] ++ pkgs.lib.optionals (config.networking.hostName == "hostB") [
             |                            ^
            6|     pkgs.cowsay

Solution

  • Use osConfig instead of config:

    { pkgs, osConfig, ...}:
     {
      home.packages = [
        pkgs.hello
      ] ++ pkgs.lib.optionals (osConfig.networking.hostName == "hostB") [
        pkgs.cowsay
      ];
    }
    

    Credit to GitHub user rycee: https://github.com/nix-community/home-manager/issues/393#issuecomment-1259996423