git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #79370 from sorki/dtoverlays

Improve device-tree overlay support

commitf0f88be1eabf2504736debb418d1f164b3228c8e authorWORLDofPEACE <worldofpeace@protonmail.ch> dateWed 9 Sep 2020, 11:01 (6 years ago) parentca2fd926c9df40
3 files changed+183−19
nixos/doc/manual/release-notes/rl-2009.xml+11 nixos/modules/hardware/device-tree.nix+151−13 pkgs/os-specific/linux/device-tree/default.nix+21−6
nixos/doc/manual/release-notes/rl-2009.xml+11
@@ -843,6 +843,17 @@ CREATE ROLE postgres LOGIN SUPERUSER;
843843 of the default <literal>out</literal> output anymore - if you relied on the
844844 <literal>notmuch-emacs-mua</literal> binary or the emacs lisp files, access them via
845845 the <literal>notmuch.emacs</literal> output.
846+
847+ Device tree overlay support was improved in
848+ <link xlink:href="https://github.com/NixOS/nixpkgs/pull/79370">#79370</link>
849+ and now uses <xref linkend="opt-hardware.deviceTree.kernelPackage"/>
850+ instead of <option>hardware.deviceTree.base</option>.
851+
852+ <xref linkend="opt-hardware.deviceTree.overlays"/> configuration was
853+ extended to support <literal>.dts</literal> files with symbols.
854+
855+ Device trees can now be filtered by setting
856+ <xref linkend="opt-hardware.deviceTree.filter"/> option.
846857 </para>
847858 </listitem>
848859 <listitem>
nixos/modules/hardware/device-tree.nix+151−13
@@ -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}
pkgs/os-specific/linux/device-tree/default.nix+21−6
@@ -1,16 +1,31 @@
11{ stdenvNoCC, dtc, findutils }:
22
33with stdenvNoCC.lib; {
4− applyOverlays = (base: overlays: stdenvNoCC.mkDerivation {
4+ applyOverlays = (base: overlays': stdenvNoCC.mkDerivation {
55 name = "device-tree-overlays";
66 nativeBuildInputs = [ dtc findutils ];
77 buildCommand = let
8− quotedDtbos = concatMapStringsSep " " (o: "\"${toString o}\"") (toList overlays);
8+ overlays = toList overlays';
99 in ''
10− for dtb in $(find ${base} -name "*.dtb" ); do
11− outDtb=$out/$(realpath --relative-to "${base}" "$dtb")
12− mkdir -p "$(dirname "$outDtb")"
13− fdtoverlay -o "$outDtb" -i "$dtb" ${quotedDtbos};
10+ mkdir -p $out
11+ cd ${base}
12+ find . -type f -name '*.dtb' -print0 \
13+ | xargs -0 cp -v --no-preserve=mode --target-directory $out --parents
14+
15+ for dtb in $(find $out -type f -name '*.dtb'); do
16+ dtbCompat="$( fdtget -t s $dtb / compatible )"
17+
18+ ${flip (concatMapStringsSep "\n") overlays (o: ''
19+ overlayCompat="$( fdtget -t s ${o.dtboFile} / compatible )"
20+ # overlayCompat in dtbCompat
21+ if [[ "$dtbCompat" =~ "$overlayCompat" ]]; then
22+ echo "Applying overlay ${o.name} to $( basename $dtb )"
23+ mv $dtb{,.in}
24+ fdtoverlay -o "$dtb" -i "$dtb.in" ${o.dtboFile};
25+ rm $dtb.in
26+ fi
27+ '')}
28+
1429 done
1530 '';
1631 });