How to use hostName in a path?

With Flake and Home Manager enabled, I want to use the host name as part of a path in an import statement.

Initially, I tried this (lf.nix):

{ lib, config, ... }:
    inherit (import ../../../hosts/${config.networking.hostName}/options.nix) lf;
in
{
    programs.lf.enable = lib.mkIf lf true;
}

error: attribute ‘networking’ missing


Then, after finding a related post, I tried declaring hostName in home.nix instead (excerpt):

{ lib, pkgs, ... }:
let
in
{
    hostName = "koala";
    home.stateVersion = "23.11";
…

I updated lf.nix accordingly:

{ lib, ... }:
let
    inherit (import ../../../hosts/${hostName}/options.nix) lf;
in
{
    programs.lf.enable = lib.mkIf lf true;
}

error: undefined variable ‘hostName’


The hostName is available in lf.nix. I also tried the following, in home.nix, with no success:

../../modules/programs/lf/lf.nix {hostName = "koala";}

The question is how I should pass hostName from home.nix to a sub module (lf.nix in my example). (Or, alternatively, how to access networking.hostName if possible.)

Is this in the context of NixOS configuration or home-manager configuration? If it’s home-manager running on NixOS, you should be able to use osConfig as a module argument to access the configuration of the NixOS host. In this context, think you might be able to get the hostname as osConfig.networking.hostName.

There’s an example in the Home Manager Manual that sounds like what you might be looking for (search osConfig).

3 Likes

Thanks a lot, @devusb. This was exactly was I was looking for, as it was home-manager config.

{ lib, osConfig, ... }:
let
    inherit (import ../../../hosts/${osConfig.networking.hostName}/options.nix) lf;
in
{
    programs.lf.enable = lib.mkIf lf true;
}

(For the record, I also found a following related post, about sending parameters into an imported module. However, nrdxp’s solution did not work for me. The statement ../../modules/programs/lf/lf.nix { hostName = "koala"; } resulted in an error saying that hostName is not defined. Maybe it is since I am doing it from inside home-manager.)

1 Like