1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 cfg = config.hardware.deviceTree;
9
10 overlayType = lib.types.submodule {
11 options = {
12 name = lib.mkOption {
13 type = lib.types.str;
14 description = ''
15 Name of this overlay
16 '';
17 };
18
19 filter = lib.mkOption {
20 type = lib.types.nullOr lib.types.str;
21 default = null;
22 example = "*rpi*.dtb";
23 description = ''
24 Only apply to .dtb files matching glob expression.
25 '';
26 };
27
28 dtsFile = lib.mkOption {
29 type = lib.types.nullOr lib.types.path;
30 description = ''
31 Path to .dts overlay file, overlay is applied to
32 each .dtb file matching "compatible" of the overlay.
33 '';
34 default = null;
35 example = lib.literalExpression "./dts/overlays.dts";
36 };
37
38 dtsText = lib.mkOption {
39 type = lib.types.nullOr lib.types.str;
40 default = null;
41 description = ''
42 Literal DTS contents, overlay is applied to
43 each .dtb file matching "compatible" of the overlay.
44 '';
45 example = ''
46 /dts-v1/;
47 /plugin/;
48 / {
49 compatible = "raspberrypi";
50 };
51 &{/soc} {
52 pps {
53 compatible = "pps-gpio";
54 status = "okay";
55 };
56 };
57 '';
58 };
59
60 dtboFile = lib.mkOption {
61 type = lib.types.nullOr lib.types.path;
62 default = null;
63 description = ''
64 Path to .dtbo compiled overlay file.
65 '';
66 };
67 };
68 };
69
70 filterDTBs =
71 src:
72 if cfg.filter == null then
73 src
74 else
75 pkgs.runCommand "dtbs-filtered" { } ''
76 mkdir -p $out
77 cd ${src}
78 find . -type f -name '${cfg.filter}' -print0 \
79 | xargs -0 cp -v --no-preserve=mode --target-directory $out --parents
80 '';
81
82 filteredDTBs = filterDTBs cfg.dtbSource;
83
84 # Fill in `dtboFile` for each overlay if not set already.
85 # Existence of one of these is guarded by assertion below
86 withDTBOs =
87 xs:
88 lib.flip map xs (
89 o:
90 o
91 // {
92 dtboFile =
93 let
94 includePaths = [
95 "${lib.getDev cfg.kernelPackage}/lib/modules/${cfg.kernelPackage.modDirVersion}/source/scripts/dtc/include-prefixes"
96 ] ++ cfg.dtboBuildExtraIncludePaths;
97 extraPreprocessorFlags = cfg.dtboBuildExtraPreprocessorFlags;
98 in
99 if o.dtboFile == null then
100 let
101 dtsFile = if o.dtsFile == null then (pkgs.writeText "dts" o.dtsText) else o.dtsFile;
102 in
103 pkgs.deviceTree.compileDTS {
104 name = "${o.name}-dtbo";
105 inherit includePaths extraPreprocessorFlags dtsFile;
106 }
107 else
108 o.dtboFile;
109 }
110 );
111
112in
113{
114 imports = [
115 (lib.mkRemovedOptionModule [
116 "hardware"
117 "deviceTree"
118 "base"
119 ] "Use hardware.deviceTree.kernelPackage instead")
120 ];
121
122 options = {
123 hardware.deviceTree = {
124 enable = lib.mkOption {
125 default = pkgs.stdenv.hostPlatform.linux-kernel.DTB or false;
126 type = lib.types.bool;
127 description = ''
128 Build device tree files. These are used to describe the
129 non-discoverable hardware of a system.
130 '';
131 };
132
133 kernelPackage = lib.mkOption {
134 default = config.boot.kernelPackages.kernel;
135 defaultText = lib.literalExpression "config.boot.kernelPackages.kernel";
136 example = lib.literalExpression "pkgs.linux_latest";
137 type = lib.types.path;
138 description = ''
139 Kernel package where device tree include directory is from. Also used as default source of dtb package to apply overlays to
140 '';
141 };
142
143 dtboBuildExtraPreprocessorFlags = lib.mkOption {
144 default = [ ];
145 example = lib.literalExpression "[ \"-DMY_DTB_DEFINE\" ]";
146 type = lib.types.listOf lib.types.str;
147 description = ''
148 Additional flags to pass to the preprocessor during dtbo compilations
149 '';
150 };
151
152 dtboBuildExtraIncludePaths = lib.mkOption {
153 default = [ ];
154 example = lib.literalExpression ''
155 [
156 ./my_custom_include_dir_1
157 ./custom_include_dir_2
158 ]
159 '';
160 type = lib.types.listOf lib.types.path;
161 description = ''
162 Additional include paths that will be passed to the preprocessor when creating the final .dts to compile into .dtbo
163 '';
164 };
165
166 dtbSource = lib.mkOption {
167 default = "${cfg.kernelPackage}/dtbs";
168 defaultText = lib.literalExpression "\${cfg.kernelPackage}/dtbs";
169 type = lib.types.path;
170 description = ''
171 Path to dtb directory that overlays and other processing will be applied to. Uses
172 device trees bundled with the Linux kernel by default.
173 '';
174 };
175
176 name = lib.mkOption {
177 default = null;
178 example = "some-dtb.dtb";
179 type = lib.types.nullOr lib.types.str;
180 description = ''
181 The name of an explicit dtb to be loaded, relative to the dtb base.
182 Useful in extlinux scenarios if the bootloader doesn't pick the
183 right .dtb file from FDTDIR.
184 '';
185 };
186
187 filter = lib.mkOption {
188 type = lib.types.nullOr lib.types.str;
189 default = null;
190 example = "*rpi*.dtb";
191 description = ''
192 Only include .dtb files matching glob expression.
193 '';
194 };
195
196 overlays = lib.mkOption {
197 default = [ ];
198 example = lib.literalExpression ''
199 [
200 { name = "pps"; dtsFile = ./dts/pps.dts; }
201 { name = "spi";
202 dtsText = "...";
203 }
204 { name = "precompiled"; dtboFile = ./dtbos/example.dtbo; }
205 ]
206 '';
207 type = lib.types.listOf (
208 lib.types.coercedTo lib.types.path (path: {
209 name = baseNameOf path;
210 filter = null;
211 dtboFile = path;
212 }) overlayType
213 );
214 description = ''
215 List of overlays to apply to base device-tree (.dtb) files.
216 '';
217 };
218
219 package = lib.mkOption {
220 default = null;
221 type = lib.types.nullOr lib.types.path;
222 internal = true;
223 description = ''
224 A path containing the result of applying `overlays` to `kernelPackage`.
225 '';
226 };
227 };
228 };
229
230 config = lib.mkIf (cfg.enable) {
231
232 assertions =
233 let
234 invalidOverlay = o: (o.dtsFile == null) && (o.dtsText == null) && (o.dtboFile == null);
235 in
236 lib.singleton {
237 assertion = lib.all (o: !invalidOverlay o) cfg.overlays;
238 message = ''
239 deviceTree overlay needs one of dtsFile, dtsText or dtboFile set.
240 Offending overlay(s):
241 ${toString (map (o: o.name) (builtins.filter invalidOverlay cfg.overlays))}
242 '';
243 };
244
245 hardware.deviceTree.package =
246 if (cfg.overlays != [ ]) then
247 pkgs.deviceTree.applyOverlays filteredDTBs (withDTBOs cfg.overlays)
248 else
249 filteredDTBs;
250 };
251}