-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac46711
commit b38ca41
Showing
2 changed files
with
48 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,45 @@ | ||
{ lib, ... }: | ||
with lib; | ||
rec { | ||
with lib; rec { | ||
# List immediate subdirectories of a directory | ||
subDirs = | ||
d: | ||
foldlAttrs ( | ||
a: k: v: | ||
a // (if v == "directory" then { ${k} = d + "/${k}"; } else { }) | ||
) { } (builtins.readDir d); | ||
subDirs = d: | ||
foldlAttrs | ||
(a: k: v: a // (if v == "directory" then { ${k} = d + "/${k}"; } else { })) | ||
{ } (builtins.readDir d); | ||
|
||
# Annotate a region of text for simpler identification of origin | ||
textRegion = | ||
{ | ||
name, | ||
content, | ||
comment-char ? "#", | ||
}: | ||
'' | ||
${comment-char} +begin ${name} | ||
${content} | ||
${comment-char} +end ${name} | ||
''; | ||
textRegion = { name, content, comment-char ? "#", }: '' | ||
${comment-char} +begin ${name} | ||
${content} | ||
${comment-char} +end ${name} | ||
''; | ||
|
||
# Return the list of importable names in a directory. | ||
# Importable name is either | ||
# - a regular file with the nix extension | ||
# - or a directory containing default.nix. | ||
importables = | ||
root: | ||
let | ||
children = builtins.readDir root; | ||
in | ||
(builtins.attrNames ( | ||
lib.attrsets.filterAttrs ( | ||
n: t: | ||
(t == "regular" && (builtins.match ".+\\.nix$" n) != null) | ||
|| (t == "directory" && builtins.pathExists (root + "/${n}/default.nix")) | ||
) children | ||
)); | ||
importables = root: | ||
let children = builtins.readDir root; | ||
in (builtins.attrNames (lib.attrsets.filterAttrs (n: t: | ||
(t == "regular" && (builtins.match ".+\\.nix$" n) != null) | ||
|| (t == "directory" && builtins.pathExists (root + "/${n}/default.nix"))) | ||
children)); | ||
|
||
# Import each importable in the dir and return as an attrset keyed by their names. | ||
importDir = d: arg: | ||
builtins.foldl' (a: name: a // { ${name} = import "${d}/${name}" arg; }) { } | ||
(importables d); | ||
|
||
# Like callPackageWith but for a while directory. | ||
# The directory should contain nix files, or directories containing default.nix files that define packages. | ||
# The packages are imported with their file basename as the attribute name. | ||
callPackageDirWith = | ||
root: ins: | ||
callPackageDirWith = root: ins: | ||
let | ||
callPkg = lib.callPackageWith all; | ||
outs = builtins.listToAttrs ( | ||
builtins.map (f: { | ||
name = | ||
let | ||
m = (builtins.match "(.+)\\.nix$" f); | ||
in | ||
if m != null then builtins.head m else f; | ||
value = callPkg (root + ("/" + f)) { }; | ||
}) (importables root) | ||
); | ||
outs = builtins.listToAttrs (builtins.map (f: { | ||
name = let m = (builtins.match "(.+)\\.nix$" f); | ||
in if m != null then builtins.head m else f; | ||
value = callPkg (root + ("/" + f)) { }; | ||
}) (importables root)); | ||
all = ins // outs; | ||
in | ||
outs; | ||
in outs; | ||
} |