Overlays in sd-image and installers

I’m looking to provide an sd-image for an aarch64 device.

To get everything working, it is sometimes necessary to modify the generic sd-image, for example to include

  • Kernel/U-boot built from downstream kernel sources
  • Forked or git versions of other hardware-related libraries, such as libdrm, mesa.
  • Platform specific applications and scripts, such as fan and LED controls.

It’s straightforward enough to make these changes to a branch of nixpkgs and build an sd-image from that. However, once you’ve flashed the sd-image and started using the device, it becomes a bit more awkward: updating nixpkgs or changing channels requires you to have a copy of the device-specific branch and to constantly rebase it onto the upstream nixpkgs branch.

With this in mind, I’ve switched from having a modified nixpkgs, to having an overlay which modifies some existing packages and adds some new ones. This seems to work quite well. What I am wondering, is whether it is possible to:

  1. Build an sd-image using nixpkgs + overlay (both using the overlay to build packages such as the kernel, and also including the overlay in the rootfs)
  2. Install the overlay in /etc/nixos/ as part of the installation procedure

Has anyone else tried anything similar? Is there a better way to go about this?