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.


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 https://github.com/NixOS/nixpkgs/issues/67792

And 1 thing good that came out of that was: https://github.com/NixOS/nixpkgs/pull/79370

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:

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.


  imports = [

  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