forked from Princemachiavelli/flake-utils-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportModules.nix
74 lines (60 loc) · 2.1 KB
/
exportModules.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{ flake-utils-plus }:
let
inherit (flake-utils-plus.lib.internal)
genAttrs'
hasFileAttr
peek
removeSuffix
;
exportModules = args:
/**
Synopsis: exportModules _paths or modules_
paths: [ <path> <module> ]
Returns an attribute set of modules from a list of paths or modules by converting
the path's basename / the module's _file attribute's basename into the attribute key.
Example:
paths: [ ./path/to/moduleA.nix { _file = ./path/to/moduleBfolder; ... } ]
{
moduleA = import ./path/to/moduleA.nix;
moduleBfolder = { _file = ./path/to/moduleBfolder; ... }
}
**/
genAttrs'
(arg:
# a module function with a _file attr
if ((builtins.isFunction arg) && (hasFileAttr (peek arg))) then
{
name = removeSuffix ".toml" (removeSuffix ".nix" (baseNameOf (peek arg)._file));
value = arg;
}
# panic: a module function without a _file attr
else if ((builtins.isFunction arg) && (!(hasFileAttr (peek arg)))) then
builtins.throw ''
module function has no (required) _file argument key: ${builtins.trace (peek arg) "."}
''
# a simple module with a _file attr
else if (builtins.isAttrs arg) && (hasFileAttr arg) then
{
name = removeSuffix ".toml" (removeSuffix ".nix" (baseNameOf arg._file));
value = arg;
}
# panic: a simple module without a _file attr
else if (builtins.isAttrs arg) && !(hasFileAttr arg) then
builtins.throw ''
simple module has no (required) _file argument key: ${builtins.trace arg "."}
''
# a regular path to be imported
else if builtins.isPath arg then
{
name = removeSuffix ".nix" (baseNameOf arg);
value = import arg;
}
# panic: something else
else
builtins.throw ''
either pass a path or a module with _file key to exportModules: ${builtins.trace arg "."}
''
)
args;
in
exportModules