nixpkgs/pkgs/os-specific/linux/device-tree/default.nix
Richard Marko 6c9df40a4b nixos/device-tree: improve overlays support
Now allows applying external overlays either in form of
.dts file, literal dts context added to store or precompiled .dtbo.

If overlays are defined, kernel device-trees are compiled with '-@'
so the .dtb files contain symbols which we can reference in our
overlays.

Since `fdtoverlay` doesn't respect `/ compatible` by itself
we query compatible strings of both `dtb` and `dtbo(verlay)`
and apply only if latter is substring of the former.

Also adds support for filtering .dtb files (as there are now nearly 1k
dtbs).

Co-authored-by: georgewhewell <georgerw@gmail.com>
Co-authored-by: Kai Wohlfahrt <kai.wohlfahrt@gmail.com>
2020-09-09 16:34:58 +02:00

33 lines
978 B
Nix

{ stdenvNoCC, dtc, findutils }:
with stdenvNoCC.lib; {
applyOverlays = (base: overlays': stdenvNoCC.mkDerivation {
name = "device-tree-overlays";
nativeBuildInputs = [ dtc findutils ];
buildCommand = let
overlays = toList overlays';
in ''
mkdir -p $out
cd ${base}
find . -type f -name '*.dtb' -print0 \
| xargs -0 cp -v --no-preserve=mode --target-directory $out --parents
for dtb in $(find $out -type f -name '*.dtb'); do
dtbCompat="$( fdtget -t s $dtb / compatible )"
${flip (concatMapStringsSep "\n") overlays (o: ''
overlayCompat="$( fdtget -t s ${o.dtboFile} / compatible )"
# overlayCompat in dtbCompat
if [[ "$dtbCompat" =~ "$overlayCompat" ]]; then
echo "Applying overlay ${o.name} to $( basename $dtb )"
mv $dtb{,.in}
fdtoverlay -o "$dtb" -i "$dtb.in" ${o.dtboFile};
rm $dtb.in
fi
'')}
done
'';
});
}