Standalone home-manager for multiple computers with different usernames

Hello all,

I am very new to the world of Nix and am using standalone home-manager with nix flakes. I would like to use the same configuration for both my PopOS machine and my Windows machine (WSL2). It turns out that I have different usernames on each machine. How should I modify my home.nix and flake.nix files to work on both machines? My configuration is version controlled and I would like it to “just work” across machines despite their differences in username. Is there any way to programatically fetch the username ($USER in bash) and home directory ($HOME in bash) inside of the Nix files?

Thank you.

  • home.nix
...
home.username = "myusername"
home.homeDirectory = "/home/myusername"
...
  • flake.nix
{
  inputs = {...};
  output = {...}: let
    ...
  in {
    homeConfigurations."myusername" = home-manager.lib.homeManagerConfiguration {
    ...
};
  }
}

That would be impure and defeat the whole purpose of flakes (pure eval i.e. eval that doesn’t depend on the environment).

Very well noted. I still would love to know which (preferably Nix-specific) approach could solve my issue. I thought about writing an external bash script to automatically change the values of interest; however, I would love to use a native approach if possible.

The answer is to not do that and just enumerate the configs as needed.

What does “enumerating” a configuration mean?

I meant to list them out one-by-one.
Or use a function from lib to generate the attrset, there’s lib.genAttrs for example.