How can I create a list of package names to import?

I currently have this file which contains my cli packages:
https://git.2li.ch/Nebucatnetzer/nixos/src/branch/master/common/cli-packages.nix

I have some systems which aren’t running Nixos but I still would like to use home-manager.
How could I now include the cli-packages.nix in the home-manager config? E.g. here:
https://git.2li.ch/Nebucatnetzer/nixos/src/branch/master/home-manager/common.nix

Can I just rewrite cli-packages.nix to something like this:

[
  tree
  htop
];

and in home-manager this:

home.packages = with pkgs; [
  import ../common/cli-packages.nix
  ansible
];

Not as easy as that.

cli-packages.nix needs to know about pkgs from somewhere, therefore it would be easier to make it return a function:

pkgs:

with pkgs; [
  tree
  htop
]

And then also you need to concatenate the lists rather than nesting them:

home.packages = with pkgs; [ ansible ] ++ import ./cli-packages.nix {};

Though personally, I prefer the module approach. In general I do consider raw imports an antipattern in configurations.

1 Like

How would that look like?

Btw. what happens if I have:

imports = [
  ./something-else.nix
];
home.packages = with pkgs; [
  ansible
];

And something-else.nix contains home.packages as well?
Why doesn’t it get overwritten?

Because modules work that way. Options you set in modules do not get overwritten but merged.

Found the docs about it in nixos-help, I’m still trying to figure out where to find the information :slight_smile: .

1 Like