Skip to content

Instantly share code, notes, and snippets.

@carlossless
Created August 3, 2022 19:31
Show Gist options
  • Save carlossless/7adfea6cd656201bb8d52daeb33a5112 to your computer and use it in GitHub Desktop.
Save carlossless/7adfea6cd656201bb8d52daeb33a5112 to your computer and use it in GitHub Desktop.
{ lib, pkgs, stdenvNoCC, dtc }:
with lib; {
applyOverlays = (base: overlays': stdenvNoCC.mkDerivation {
name = "device-tree-overlays";
nativeBuildInputs = [ dtc ];
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}
cp ${o.dtboFile} ${o.dtboFile}.dtbo
${pkgs.libraspberrypi}/bin/dtmerge "$dtb.in" "$dtb" ${o.dtboFile}.dtbo;
rm $dtb.in
rm ${o.dtboFile}.dtbo
fi
'')}
done
'';
});
}
@carlossless
Copy link
Author

The default pkgs.deviceTree.applyOverlays can be overriden by using:

{ config, lib, pkgs, modulesPath, ... }:
let
  callPackage = path: overrides:
    let f = import path;
    in f ((builtins.intersectAttrs (builtins.functionArgs f) pkgs) // overrides);
  dtmerge_overlay = (final: prev: {
    deviceTree.applyOverlays = (prev.callPackage ./apply-overlays-dtmerge.nix { }).applyOverlays;
  });
in
{
  nixpkgs.overlays = [ dtmerge_overlay ];
}

This is useful if you're applying rpi dt overlays that don't work with ftoverlay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment