-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.nix
80 lines (80 loc) · 3.06 KB
/
lib.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
75
76
77
78
79
80
lib:
(lib // rec {
alphabet = "abcdefghijklmnopqrstuvwxyz";
letterOfAlphabet = x: builtins.substring x 1 alphabet;
splitString = reg: s: builtins.filter (x: builtins.isString x && "" != x)
(builtins.split reg s);
lines = splitString "\n";
mapListToAttrs' = f: xs: builtins.listToAttrs (map f xs);
mapListToAttrs = f: mapListToAttrs' (name: { inherit name; value = f name; });
unlines = x: builtins.concatStringsSep "\n" x;
words = splitString "[[:space:]]";
unwords = x: builtins.concatStringsSep " " x;
anyPathBaseName = path:
if (builtins.dirOf (toString path)) == "/nix/store"
then builtins.substring 33 1000 (builtins.baseNameOf path)
else builtins.baseNameOf path;
stringPathToStorePathPWD = stringPathToStorePath (builtins.getEnv "PWD");
stringPathToStorePath = relativeTo: x:
if lib.hasPrefix "/nix/store/" x
then builtins.storePath x
else if lib.hasPrefix "/" x
then builtins.filterSource (p: t: true) (builtins.toPath (/. + "/${x}"))
else builtins.filterSource (p: t: true) (builtins.toPath (relativeTo + "/${x}"));
stripComment = x: builtins.head (builtins.split "#.*$" x);
commentedFileToLines = path: builtins.filter
(x: 0 != builtins.length (words x))
(map stripComment (lines (builtins.readFile path)));
matchPattern = pattern: data:
builtins.head (builtins.elemAt (builtins.split pattern data) 1);
resolveModule = modulesPaths: modName:
let
folder = olds: thisModPath:
olds ++ (if builtins.pathExists (thisModPath + "/${modName}.nix")
then [ (thisModPath + "/${modName}.nix") ]
else [ ]);
mods = builtins.foldl' folder [ ] modulesPaths;
in
if [ ] == mods then throw "Module ${modName} not found." else mods;
handleSetDeps =
{ handledSets ? { }
, handledModules ? { }
, inputs
, modulesPaths
, passthrus ? [ ]
, sets
}:
let
hInputs = builtins.head inputs;
isPassthru = !(builtins.isString hInputs);
isSet = !isPassthru && lib.hasPrefix "set:" hInputs;
isModule = !(isPassthru || isSet);
name = lib.removePrefix "set:" hInputs;
newHandledSets = handledSets // lib.optionalAttrs isSet { "${name}" = [ ]; };
newHandledModules = handledModules // lib.optionalAttrs isModule {
"${name}" = [ ];
};
passthruModule =
if builtins.isAttrs hInputs ||
builtins.isFunction hInputs ||
builtins.isPath hInputs
then hInputs
else throw "Module type not recognized!";
newPassthrus = passthrus
++ lib.optional isPassthru passthruModule
++ lib.optionals isModule (resolveModule modulesPaths name);
newInputs = builtins.tail inputs
++ lib.optionals isSet (handledSets.${name} or sets.${name})
++ lib.optionals isModule (handledModules.${name} or [ name ]);
in
if [ ] == inputs
then passthrus
else
handleSetDeps {
inherit modulesPaths sets;
handledSets = newHandledSets;
handledModules = newHandledModules;
passthrus = newPassthrus;
inputs = newInputs;
};
})