Raspberry Pi 4 - add I2C to device tree

I have NixOS on my Raspberry Pi 4. Things are working pretty well except that the I2C bus is not initialized. I can add this to my /boot/config.txt manually and do the required modprobes to get the i2c devices to appear and be usable, but I’ve like to rebuild NixOS with this set up automatically.

What do I need to add to my configuration.nix file to get the config.txt updated and then the kernel drivers loaded at boot-time? I can see that config.txt is being overwritten when I rebuild NixOS.

Thanks!

1 Like

A while ago, I’ve never managed to figure out how things are supposed to work for NixOS and Raspberries. That’s why I opened /boot/config.txt related questions for Raspberry Pi · Issue #67792 · NixOS/nixpkgs · GitHub

And 1 thing good that came out of that was: Improve device-tree overlay support by sorki · Pull Request #79370 · NixOS/nixpkgs · GitHub

So perhaps that PR will help you.

1 Like

Note for anyone who passes through here looking for I2C config tips like I did:

I added an option to turn the bus on with a one-liner over here:

https://github.com/NixOS/nixos-hardware/pull/345

This is built on top of the device-tree.overlays work above. Should work for anyone building their device tree via NixOS settings, and importantly makes it easy to do if you’re using uboot rather than the stock Pi bootloader.

Usage:

{
  imports = [
    ./nixos-hardware/raspberry-pi/4
  ];

  hardware.raspberry-pi."4".i2c1.enable = true;
}

If your needs are more specific you can use it as sample code to roll your own .dts and apply via device-tree.overlays.

Sorry for the necro and hope this helps someone out in the future.

1 Like