How to recursively import nix files (eg program modules) for Home Manager using lib.fileset?

I’m trying to import all my home manager program module configs in nixpkgs/pkgs-home-manager/modules-macos-active and its subdirectories without a default.nix (and one per subdirectory if that’s the default behaviour), as follows:

home.nix

imports = [ ./pkgs-home-manager/modules-macos-active ./options-home-manager.nix ];

I’ve seen umport, which looks great, but I have no idea how to use it (as I’m noob and can’t find its package) and I’d prefer to use lib.fileset because it’s part of Nix and more supported.

Are there any basic examples of how to use lib.fileset for use cases like mine and is it possible, or would I be better to use umport, and if so, how do I use it?

Thanks

lib.fileset wasn’t really made for this use case, but I think this might work:

{
  imports = lib.fileset.toList (
    # All default.nix files in ./.
    lib.fileset.fileFilter (file: file.name == "default.nix") ./.
  );
}
1 Like

Is it possible to import every *.nix file in any subfolder recursively without entering multiple lines for each subfolder), so that in the future I can create a .nix file, drop it in the relative subfolder and rebuild without editing the main configuration.nix file?

For example let’s say my root directory contains these files, folders and subfolders:

configuration.nix
hardware-configuration.nix
disk-mounts.nix
./networking
├── network-config.nix
├── ./ssh
    ├── ssh-server.nix
    ├── ssh-client.nix
./software
├── ./utilities
    ├── cli-utils.nix
    ├── ./gui
        ├── example1.nix
        ├── example2.nix
        ├── ./another-folder
            ├── another-file.nix
├── office.nix
├── browsers.nix

Then one day I create the nfs.nix and the smb.nix files in the networking folder and just rebuild switch without editing configuration.nix

I also found this thread but I don’t understand how to adapt it to my use case.

There’s nothing to adapt there, the top comment is quite copy-paste.
If it doesn’t work, share exactly what you tried, in context i.e. including any surrounding code or better the repo itself.

Do you mean the top comment here or on Reddit? I just want the config to be super modular so I can take single files and folders for other machines at need, and I can drop some new files and folders at will without editing the main config file. I would also like to keep everything in the /home/nixos-config directory so should the need to reinstall arise I can install the base system with the GUI installer without formatting /home and immediately rebuild switch. Sorry, new to NixOS and still trying to figure out the language.

(file: file.ext == “nix”)

I think you can filter on extension instead of name