me like nix
1# modification of nixpkgs deviceTree.applyOverlays to resolve https://github.com/NixOS/nixpkgs/issues/125354
2# derived from https://github.com/NixOS/nixpkgs/blob/916ca8f2b0c208def051f8ea9760c534a40309db/pkgs/os-specific/linux/device-tree/default.nix
3{ lib, stdenvNoCC, dtc, libraspberrypi }:
4
5with lib; (base: overlays': stdenvNoCC.mkDerivation {
6 name = "device-tree-overlays";
7 nativeBuildInputs = [ dtc libraspberrypi ];
8 buildCommand =
9 let
10 overlays = toList overlays';
11 in
12 ''
13 mkdir -p $out
14 cd "${base}"
15 find . -type f -name '*.dtb' -print0 \
16 | xargs -0 cp -v --no-preserve=mode --target-directory "$out" --parents
17
18 for dtb in $(find "$out" -type f -name '*.dtb'); do
19 dtbCompat=$(fdtget -t s "$dtb" / compatible 2>/dev/null || true)
20 # skip files without `compatible` string
21 test -z "$dtbCompat" && continue
22
23 ${flip (concatMapStringsSep "\n") overlays (o: ''
24 overlayCompat="$(fdtget -t s "${o.dtboFile}" / compatible)"
25
26 # skip incompatible and non-matching overlays
27 if [[ ! "$dtbCompat" =~ "$overlayCompat" ]]; then
28 echo "Skipping overlay ${o.name}: incompatible with $(basename "$dtb")"
29 elif ${if ((o.filter or null) == null) then "false" else ''
30 [[ "''${dtb//${o.filter}/}" == "$dtb" ]]
31 ''}
32 then
33 echo "Skipping overlay ${o.name}: filter does not match $(basename "$dtb")"
34 else
35 echo -n "Applying overlay ${o.name} to $(basename "$dtb")... "
36 mv "$dtb"{,.in}
37
38 # dtmerge requires a .dtbo ext for dtbo files, otherwise it adds it to the given file implicitly
39 dtboWithExt="$TMPDIR/$(basename "${o.dtboFile}").dtbo"
40 cp -r ${o.dtboFile} "$dtboWithExt"
41
42 dtmerge "$dtb.in" "$dtb" "$dtboWithExt"
43
44 echo "ok"
45 rm "$dtb.in" "$dtboWithExt"
46 fi
47 '')}
48
49 done'';
50})