/ modules / default.nix
default.nix
 1  {lib, ...}: let
 2    inherit (lib) attrNames optional hasSuffix;
 3    inherit (builtins) elem readDir foldl';
 4  
 5    foldlAttrs = f: init: set:
 6      foldl'
 7      (acc: name: f acc name set.${name})
 8      init
 9      (attrNames set);
10  
11    concatPaths = path1: path2: (toString path1) + "/" + (toString path2);
12    getModules = ignoreDefault: path: let
13      files = readDir path;
14      isModuleDirectory = !ignoreDefault && elem "default.nix" (attrNames files);
15    in
16      if isModuleDirectory
17      then [(concatPaths path "default.nix")]
18      else
19        foldlAttrs (
20          acc: name: type:
21            acc
22            ++ (
23              if (type == "regular")
24              then optional (name != "default.nix" && hasSuffix "nix" name) (concatPaths path name)
25              else getModules false (concatPaths path name)
26            )
27        ) []
28        files;
29  in {
30    imports = getModules true ./.;
31  }