I’m using modules for my NixOS configuration files, and I created a folder for all my services where I have a .nix for each service defined. I’m trying to import these files in my NixOS configuration like this:
imports =
[
"./services/*.nix"
];
But I realize that this is not technically a path. Would it be possible to write some Nix code to add the paths matching the wildcard to the path set?
There is also lib.fileset which IIRC already allows returning lists (right @infinisil?). You can’t use wildcards there but it’s possible to define filters in the Nix language (which is more verbose but I find it easier to reason about since it’s not trying to mash multiple languages into each other).
I don’t think lib.fileset would work well here, because there’s no way to only filter files in a directory without recursing. You can only do ./services/**/*.nix using
lib.pipe “pipes” a value through an arbitrary number of functions that each take a single argument. The result of each function is passed into the next function as the argument.
The first thing that happens is that builtins.readDir is called on the ./services path which returns an attrset of files. This attrset is then filtered to only contain the attributes for files that end in .nix. Lastly, a list is generated from the attrset’s names (the file names), each being concatenated with the ./services path in order to have the path to each file.
The documentation for each function can be found in the code comment above their declaration. I don’t think there’s a single doc for them them yet but there is https://noogle.dev/ which is quite handy.