2019-12-09 03:40:48 +01:00
|
|
|
args: initPath:
|
2019-11-15 15:52:23 +01:00
|
|
|
|
|
|
|
let
|
|
|
|
inherit (builtins)
|
|
|
|
attrNames
|
2019-12-09 21:34:49 +01:00
|
|
|
baseNameOf
|
2019-11-15 15:52:23 +01:00
|
|
|
filter
|
|
|
|
head
|
2019-11-25 16:32:02 +01:00
|
|
|
length
|
2019-11-15 15:52:23 +01:00
|
|
|
listToAttrs
|
|
|
|
map
|
|
|
|
match
|
2019-12-10 15:46:05 +01:00
|
|
|
isAttrs
|
2019-12-09 21:34:49 +01:00
|
|
|
readDir;
|
2019-11-15 15:52:23 +01:00
|
|
|
|
2019-12-09 21:34:49 +01:00
|
|
|
argsWithPath = parts: args // {
|
|
|
|
locatedAt = parts;
|
|
|
|
};
|
2019-11-15 15:52:23 +01:00
|
|
|
|
2019-12-10 15:46:05 +01:00
|
|
|
# The marker is added to every set that was imported directly by
|
2019-12-09 21:34:49 +01:00
|
|
|
# readTree.
|
2019-12-10 15:46:05 +01:00
|
|
|
importWithMark = path: parts:
|
|
|
|
let imported = import path (argsWithPath parts);
|
|
|
|
in if (isAttrs imported)
|
|
|
|
then imported // { __readTree = true; }
|
|
|
|
else imported;
|
2019-11-15 15:52:23 +01:00
|
|
|
|
2019-12-09 21:34:49 +01:00
|
|
|
nixFileName = file:
|
2019-11-15 15:52:23 +01:00
|
|
|
let res = match "(.*)\.nix" file;
|
|
|
|
in if res == null then null else head res;
|
|
|
|
|
2019-12-09 21:34:49 +01:00
|
|
|
readTree = path: parts:
|
2019-12-09 04:16:02 +01:00
|
|
|
let
|
2019-12-09 21:34:49 +01:00
|
|
|
dir = readDir path;
|
2019-12-10 15:46:05 +01:00
|
|
|
self = importWithMark path parts;
|
2019-12-09 21:34:49 +01:00
|
|
|
joinChild = c: path + ("/" + c);
|
|
|
|
|
|
|
|
# Import non-empty subdirectories
|
|
|
|
filterDir = f: dir."${f}" == "directory";
|
|
|
|
children = map (c: {
|
|
|
|
name = c;
|
|
|
|
value = readTree (joinChild c) (parts ++ [ c ]);
|
|
|
|
}) (filter filterDir (attrNames dir));
|
|
|
|
|
|
|
|
# Import Nix files
|
|
|
|
nixFiles = filter (f: f != null) (map nixFileName (attrNames dir));
|
|
|
|
nixChildren = map (c: let p = joinChild (c + ".nix"); in {
|
|
|
|
name = c;
|
2019-12-10 15:46:05 +01:00
|
|
|
value = importWithMark p (parts ++ [ c ]);
|
2019-12-09 21:34:49 +01:00
|
|
|
}) nixFiles;
|
2019-12-02 16:05:54 +01:00
|
|
|
in if dir ? "default.nix"
|
2019-12-09 21:34:49 +01:00
|
|
|
then self // (listToAttrs children)
|
|
|
|
else listToAttrs (nixChildren ++ children);
|
|
|
|
in readTree initPath [ (baseNameOf initPath) ]
|