-
Notifications
You must be signed in to change notification settings - Fork 62
/
lib.nix
162 lines (143 loc) · 4.36 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Copyright 2022-2024 TII (SSRC) and the Ghaf contributors
# Copyright 2020-2023 Pacman99 and the Digga Contributors
#
# SPDX-License-Identifier: MIT
# FlattenTree and rakeLeaves originate from
# https://github.com/divnix/digga
{ inputs, ... }:
let
inherit (inputs) nixpkgs;
in
nixpkgs.lib.extend (
lib: _:
# some utils for importing trees
rec {
/*
*
Filters Nix packages based on the target system platform.
Returns a filtered attribute set of Nix packages compatible with the target system.
# Example
```
lib.platformPkgs "x86_64-linux" {
hello-compatible = pkgs.hello.overrideAttrs (old: { meta.platforms = ["x86_64-linux"]; });
hello-inccompatible = pkgs.hello.overrideAttrs (old: { meta.platforms = ["aarch-linux"]; });
}
=> { hello-compatible = «derivation /nix/store/g2mxdrkwr1hck4y5479dww7m56d1x81v-hello-2.12.1.drv»; }
```
# Type
```
filterAttrs :: String -> AttrSet -> AttrSet
```
# Arguments
- [system] Target system platform (e.g., "x86_64-linux").
- [pkgsSet] a set of Nix packages.
*/
platformPkgs =
system:
lib.filterAttrs (
_: value:
let
platforms = lib.attrByPath [
"meta"
"platforms"
] [ ] value;
in
lib.elem system platforms
);
/*
*
Flattens a _tree_ of the shape that is produced by rakeLeaves.
An attrset with names in the spirit of the Reverse DNS Notation form
that fully preserve information about grouping from nesting.
# Example
```
flattenTree {
a = {
b = {
c = <path>;
};
};
}
=> { "a.b.c" = <path>; }
```
*/
flattenTree =
tree:
let
op =
sum: path: val:
let
pathStr = builtins.concatStringsSep "." path; # dot-based reverse DNS notation
in
if builtins.isPath val then
# builtins.trace "${toString val} is a path"
(sum // { "${pathStr}" = val; })
else if builtins.isAttrs val then
# builtins.trace "${builtins.toJSON val} is an attrset"
# recurse into that attribute set
(recurse sum path val)
else
# ignore that value
# builtins.trace "${toString path} is something else"
sum;
recurse =
sum: path: val:
builtins.foldl' (sum: key: op sum (path ++ [ key ]) val.${key}) sum (builtins.attrNames val);
in
recurse { } [ ] tree;
/*
*
Recursively collect the nix files of _path_ into attrs.
Return an attribute set where all `.nix` files and directories with `default.nix` in them
are mapped to keys that are either the file with .nix stripped or the folder name.
All other directories are recursed further into nested attribute sets with the same format.
# Example
Example file structure:
```
./core/default.nix
./base.nix
./main/dev.nix
./main/os/default.nix
```
```nix
rakeLeaves .
=> {
core = ./core;
base = base.nix;
main = {
dev = ./main/dev.nix;
os = ./main/os;
};
}
```
*/
rakeLeaves =
dirPath:
let
seive =
file: type:
# Only rake `.nix` files or directories
(type == "regular" && lib.hasSuffix ".nix" file) || (type == "directory");
collect = file: type: {
name = lib.removeSuffix ".nix" file;
value =
let
path = dirPath + "/${file}";
in
if (type == "regular") || (type == "directory" && builtins.pathExists (path + "/default.nix")) then
path
# recurse on directories that don't contain a `default.nix`
else
rakeLeaves path;
};
files = lib.filterAttrs seive (builtins.readDir dirPath);
in
lib.filterAttrs (_n: v: v != { }) (lib.mapAttrs' collect files);
importLeaves =
#
# Create an import stanza by recursing a directory to find all default.nix and <file.nix>
# files beneath withough manually having to list all the subsequent files.
#
path: builtins.attrValues (lib.mapAttrs (_: import) (rakeLeaves path));
}
)