-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
113 lines (106 loc) · 3.96 KB
/
flake.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
{
description = "The NFH helps you create lists of files from your Nix File Hierarchy.";
inputs.nixpkgs.url = "github:nix-community/nixpkgs.lib";
outputs =
{ nixpkgs, ... }:
{
templates.default = {
description = "NixOS configuration template.";
path = templates/nixos-configuration;
};
__functor =
with nixpkgs.lib;
self: dir:
pipe dir [
builtins.readDir
(filterAttrs (name: type: type == "directory" || hasSuffix ".nix" name))
(mapAttrs' (
name: type:
if type == "directory" then
nameValuePair name (self (dir + "/${name}"))
else if name == "default.nix" then
nameValuePair "self" (dir + "/${name}")
else
nameValuePair (removeSuffix ".nix" name) (dir + "/${name}")
))
]
# add functor to generate a list of files at any level of `fileSet`
// {
__functor =
self: filterSet:
let
fileSet = filterAttrsRecursive (name: _: name != "__functor") self;
specials = [
"_reverseRecursive"
"_reverse"
];
validate = {
path = [ ];
__functor =
self: fSet:
throwIf (fSet._reverse or false && fSet._reverseRecursive or false)
''
The path '${concatStringsSep "." self.path}' in your 'filterSet' enables
'_reverse' and '_reverseRecursive' at the same time, this is not allowed.
''
mapAttrs
(
name: value:
let
currentPath = self.path ++ singleton name;
in
throwIfNot (elem name specials || hasAttrByPath currentPath fileSet)
''
'${concatStringsSep "." currentPath}' doesn't exist in 'fileSet',
all values in 'filterSet' must exist in 'fileSet'.
''
(
if isAttrs value then
(self // { path = currentPath; }) value
else
throwIfNot (isBool value) ''
'${concatStringsSep "." currentPath}' is not a boolean,
all values in 'filterSet' must be boolean.
'' value
)
)
fSet;
};
extend = recursiveUpdate (mapAttrsRecursive (_: _: true) fileSet);
# function that handles `_reverse` and `_reverseRecursive` values
applyReverse =
fSet:
let
updateRecursive =
path: value:
if !fSet ? _reverseRecursive then
value
else if elem (tail path) specials then
value
else
!value;
update =
_: value:
if isAttrs value then
applyReverse value
else if fSet ? _reverse then
!value
else
value;
in
removeAttrs (pipe fSet [
(mapAttrsRecursive updateRecursive)
(mapAttrs update)
]) specials;
in
pipe filterSet [
validate
extend
applyReverse
(filterAttrsRecursive (_: v: isAttrs v || v)) # discard false values
(mapAttrsRecursive (path: _: getAttrFromPath path fileSet)) # convert to file paths from 'fileSet'
(collect isPath)
];
};
};
}