@@ -4,7 +4,114 @@ with lib;
44
55let
66 cfg = config.hardware.deviceTree;
7−in {
7+
8+ overlayType = types.submodule {
9+ options = {
10+ name = mkOption {
11+ type = types.str;
12+ description = ''
13+ Name of this overlay
14+ '';
15+ };
16+
17+ dtsFile = mkOption {
18+ type = types.nullOr types.path;
19+ description = ''
20+ Path to .dts overlay file, overlay is applied to
21+ each .dtb file matching "compatible" of the overlay.
22+ '';
23+ default = null;
24+ example = literalExample "./dts/overlays.dts";
25+ };
26+
27+ dtsText = mkOption {
28+ type = types.nullOr types.str;
29+ default = null;
30+ description = ''
31+ Literal DTS contents, overlay is applied to
32+ each .dtb file matching "compatible" of the overlay.
33+ '';
34+ example = literalExample ''
35+ /dts-v1/;
36+ /plugin/;
37+ / {
38+ compatible = "raspberrypi";
39+ fragment@0 {
40+ target-path = "/soc";
41+ __overlay__ {
42+ pps {
43+ compatible = "pps-gpio";
44+ status = "okay";
45+ };
46+ };
47+ };
48+ };
49+ '';
50+ };
51+
52+ dtboFile = mkOption {
53+ type = types.nullOr types.path;
54+ default = null;
55+ description = ''
56+ Path to .dtbo compiled overlay file.
57+ '';
58+ };
59+ };
60+ };
61+
62+ # this requires kernel package
63+ dtbsWithSymbols = pkgs.stdenv.mkDerivation {
64+ name = "dtbs-with-symbols";
65+ inherit (cfg.kernelPackage) src nativeBuildInputs depsBuildBuild;
66+ patches = map (patch: patch.patch) cfg.kernelPackage.kernelPatches;
67+ buildPhase = ''
68+ patchShebangs scripts/*
69+ substituteInPlace scripts/Makefile.lib \
70+ --replace 'DTC_FLAGS += $(DTC_FLAGS_$(basetarget))' 'DTC_FLAGS += $(DTC_FLAGS_$(basetarget)) -@'
71+ make ${pkgs.stdenv.hostPlatform.platform.kernelBaseConfig} ARCH="${pkgs.stdenv.hostPlatform.platform.kernelArch}"
72+ make dtbs ARCH="${pkgs.stdenv.hostPlatform.platform.kernelArch}"
73+ '';
74+ installPhase = ''
75+ make dtbs_install INSTALL_DTBS_PATH=$out/dtbs ARCH="${pkgs.stdenv.hostPlatform.platform.kernelArch}"
76+ '';
77+ };
78+
79+ filterDTBs = src: if isNull cfg.filter
80+ then "${src}/dtbs"
81+ else
82+ pkgs.runCommand "dtbs-filtered" {} ''
83+ mkdir -p $out
84+ cd ${src}/dtbs
85+ find . -type f -name '${cfg.filter}' -print0 \
86+ | xargs -0 cp -v --no-preserve=mode --target-directory $out --parents
87+ '';
88+
89+ # Compile single Device Tree overlay source
90+ # file (.dts) into its compiled variant (.dtbo)
91+ compileDTS = name: f: pkgs.callPackage({ dtc }: pkgs.stdenv.mkDerivation {
92+ name = "${name}-dtbo";
93+
94+ nativeBuildInputs = [ dtc ];
95+
96+ buildCommand = ''
97+ dtc -I dts ${f} -O dtb -@ -o $out
98+ '';
99+ }) {};
100+
101+ # Fill in `dtboFile` for each overlay if not set already.
102+ # Existence of one of these is guarded by assertion below
103+ withDTBOs = xs: flip map xs (o: o // { dtboFile =
104+ if isNull o.dtboFile then
105+ if !isNull o.dtsFile then compileDTS o.name o.dtsFile
106+ else compileDTS o.name (pkgs.writeText "dts" o.dtsText)
107+ else o.dtboFile; } );
108+
109+in
110+{
111+ imports = [
112+ (mkRemovedOptionModule [ "hardware" "deviceTree" "base" ] "Use hardware.deviceTree.kernelPackage instead")
113+ ];
114+
8115 options = {
9116 hardware.deviceTree = {
10117 enable = mkOption {
@@ -16,13 +123,13 @@ in {
16123 '';
17124 };
18125
19− base = mkOption {
20− default = "${config.boot.kernelPackages.kernel}/dtbs";
21− defaultText = "\${config.boot.kernelPackages.kernel}/dtbs";
22− example = literalExample "pkgs.device-tree_rpi";
126+ kernelPackage = mkOption {
127+ default = config.boot.kernelPackages.kernel;
128+ defaultText = "config.boot.kernelPackages.kernel";
129+ example = literalExample "pkgs.linux_latest";
23130 type = types.path;
24131 description = ''
25− The path containing the base device-tree (.dtb) to boot. Contains
132+ Kernel package containing the base device-tree (.dtb) to boot. Uses
26133 device trees bundled with the Linux kernel by default.
27134 '';
28135 };
@@ -38,14 +145,32 @@ in {
38145 '';
39146 };
40147
148+ filter = mkOption {
149+ type = types.nullOr types.str;
150+ default = null;
151+ example = "*rpi*.dtb";
152+ description = ''
153+ Only include .dtb files matching glob expression.
154+ '';
155+ };
156+
41157 overlays = mkOption {
42158 default = [];
43− example = literalExample
44− "[\"\${pkgs.device-tree_rpi.overlays}/w1-gpio.dtbo\"]";
45− type = types.listOf types.path;
159+ example = literalExample ''
160+ [
161+ { name = "pps"; dtsFile = ./dts/pps.dts; }
162+ { name = "spi";
163+ dtsText = "...";
164+ }
165+ { name = "precompiled"; dtboFile = ./dtbos/example.dtbo; }
166+ ]
167+ '';
168+ type = types.listOf (types.coercedTo types.path (path: {
169+ name = baseNameOf path;
170+ dtboFile = path;
171+ }) overlayType);
46172 description = ''
47− A path containing device tree overlays (.dtbo) to be applied to all
48− base device-trees.
173+ List of overlays to apply to base device-tree (.dtb) files.
49174 '';
50175 };
51176
@@ -54,14 +179,27 @@ in {
54179 type = types.nullOr types.path;
55180 internal = true;
56181 description = ''
57− A path containing the result of applying `overlays` to `base`.
182+ A path containing the result of applying `overlays` to `kernelPackage`.
58183 '';
59184 };
60185 };
61186 };
62187
63188 config = mkIf (cfg.enable) {
189+
190+ assertions = let
191+ invalidOverlay = o: isNull o.dtsFile && isNull o.dtsText && isNull o.dtboFile;
192+ in lib.singleton {
193+ assertion = lib.all (o: !invalidOverlay o) cfg.overlays;
194+ message = ''
195+ deviceTree overlay needs one of dtsFile, dtsText or dtboFile set.
196+ Offending overlay(s):
197+ ${toString (map (o: o.name) (builtins.filter invalidOverlay cfg.overlays))}
198+ '';
199+ };
200+
64201 hardware.deviceTree.package = if (cfg.overlays != [])
65− then pkgs.deviceTree.applyOverlays cfg.base cfg.overlays else cfg.base;
202+ then pkgs.deviceTree.applyOverlays (filterDTBs dtbsWithSymbols) (withDTBOs cfg.overlays)
203+ else (filterDTBs cfg.kernelPackage);
66204 };
67205}