Importing modules based on system in home-manager

Hi everyone!

I am trying to configure my home-manager config so that it ports to multiple systems. I mainly use linux and mac. Thing is, I have run into an issue where some of the nix modules I import home.nix are not viable on mac.

To get around this I have tried to do something like this:

{ config, pkgs, ... }:
let darwin = pkgs.system == "x86_64-darwin";
in
let my_imports = if !darwin then [
    ./nixpkgs.nix
    ./alacritty.nix
    ./zsh.nix
    ./starship.nix
    ./tmux.nix
]
else [
    ./alacritty.nix
    ./zsh.nix
    ./starship.nix
    ./tmux.nix
];
# ...
in
{
    imports = my_imports;
    # ...
}

This is definitely a “new” user type question, but I am getting this:

error:
       … while evaluating a branch condition

         at /nix/store/riqkpszjqk02bi1wppfg8ip5xvh102qd-source/lib/lists.nix:125:9:

          124|       fold' = n:
          125|         if n == len
             |         ^
          126|         then nul

       … while calling the 'length' builtin

         at /nix/store/riqkpszjqk02bi1wppfg8ip5xvh102qd-source/lib/lists.nix:123:13:

          122|     let
          123|       len = length list;
             |             ^
          124|       fold' = n:

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: infinite recursion encountered

       at /nix/store/riqkpszjqk02bi1wppfg8ip5xvh102qd-source/lib/modules.nix:515:28:

          514|         addErrorContext (context name)
          515|           (args.${name} or config._module.args.${name})
             |                            ^
          516|       ) (functionArgs f);

Now I figure that it’s because it’s evaluating the same modules over and over again or something like that. My question: what’s the best way to get around this?

Don’t conditionally import, create separate entry points per host.
And the infrec here is because of pkgs, you cannot depend on pkgs when deciding which modules to import, because the modules themselves would influence pkgs

Can you clarify what you mean by separate entry points per host?

A file which the rest of your config is connected to.
E.g. sudo nixos-rebuild switch -I nixos-config=/path/to/host1-entrypoint.nix

Then you don’t need conditionals at all since you can put the host-specific bits in that entry point (or some other modules, if you wish).

Does this work the same for home-manager? Because that’s what I’m trying to configure right now. Sorry for these dumb questions

My bad, I didn’t notice that this was about HM.

Same idea:

home-manager switch -I home-manager=/path/to/whatever.nix

I haven’t tested this, as I don’t use HM, but it should work.

Thanks! This makes sense :slight_smile: