Skip to content

Instantly share code, notes, and snippets.

@hftsai256
Created October 25, 2024 04:39
Show Gist options
  • Save hftsai256/6e8c3111649b48d06df6e703caaaa71d to your computer and use it in GitHub Desktop.
Save hftsai256/6e8c3111649b48d06df6e703caaaa71d to your computer and use it in GitHub Desktop.
Nixos: Device Tree Overlay on Raspberry PI CM4
{
description = "Base system for raspberry pi 4";
inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable";
nixos-hardware.url = "nixos-hardware/master";
nixos-generators = {
url = "github:nix-community/nixos-generators";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, nixos-hardware, nixos-generators, ... }:
let
pkgs = import nixpkgs { system = "aarch64-linux"; };
in
{
nixosModules = {
system = {
boot = {
kernelPatches = [
{
name = "mcp4728-config";
patch = null;
structuredExtraConfig = with pkgs.lib.kernel; {
DAC_MCP4728 = yes;
};
}
];
kernelParams = [
"console=serial0,115200n8"
];
kernelPackages = pkgs.linuxPackages_rpi4;
};
fileSystems = {
"/" = {
device = "/dev/disk/by-label/NIXOS_SD";
fsType = "ext4";
};
};
hardware = {
enableRedistributableFirmware = true;
deviceTree = {
kernelPackage = pkgs.linux_rpi4;
overlays = [
{ name = "ad7616"; dtsFile = ./ad7616.dts; }
{ name = "mcp4728"; dtsFile = ./mcp4728.dts; }
{ name = "mcp3208"; dtsFile = ./mcp3208.dts; }
{ name = "spi6-1cs"; dtboFile = "${pkgs.device-tree_rpi.overlays}/spi6-1cs.dtbo"; }
];
};
};
nix.gc = {
automatic = true;
dates = "daily";
};
environment.systemPackages = with pkgs; [
raspberrypifw
libraspberrypi
raspberrypi-eeprom
libiio
neovim
];
services = {
avahi.enable = true;
openssh.enable = true;
cron.enable = true;
udev.extraRules = ''
KERNEL=="gpiochip[0-9]*", KERNELS=="1-0023", SUBSYSTEM=="gpio", SUBSYSTEMS=="i2c", ATTRS{name}=="mcp23017", SYMLINK+="gpioexp0"
KERNEL=="gpiochip[0-9]*", KERNELS=="1-0024", SUBSYSTEM=="gpio", SUBSYSTEMS=="i2c", ATTRS{name}=="mcp23017", SYMLINK+="gpioexp1"
KERNEL=="iio:device[0-9]*", SUBSYSTEM=="iio", RUN+="/bin/sh -c 'chgrp -R iio /sys%p; chmod -R g=u /sys%p'"
KERNEL=="spidev*", GROUP="spi", MODE="0660"
'';
};
networking = {
networkmanager.enable = true;
hostName = "nixpi";
};
# Disabling the whole `profiles/base.nix` module, which is responsible
# for adding ZFS and a bunch of other unnecessary programs:
disabledModules = [
"profiles/base.nix"
];
system.stateVersion = "24.05";
};
users = {
users.users.pi = {
isNormalUser = true;
extraGroups = [ "wheel" "iio" "spi" ]; # Add user to the wheel group for sudo access
};
users.groups = {
spi = { };
iio = { };
};
};
};
packages.aarch64-linux = {
sdcard = nixos-generators.nixosGenerate {
system = "aarch64-linux";
format = "sd-aarch64";
modules = [
nixos-hardware.nixosModules.raspberry-pi-4
self.nixosModules.system
self.nixosModules.users
];
};
};
};
}
/dts-v1/;
/plugin/;
/ {
compatible = "brcm,bcm2711";
fragment@0 {
target = <&i2c1>;
__overlay__ {
status = "okay";
};
};
fragment@1 {
target = <&i2c1>;
__overlay__ {
/* needed to avoid dtc warning */
#address-cells = <1>;
#size-cells = <0>;
mcp4728: dac@60 {
compatible = "microchip,mcp4728";
reg = <0x60>;
vdd-supply = <&vdd_5v0_reg>;
label = "mcp4728";
};
};
};
__overrides__ {
addr = <&mcp4728>,"reg:0";
label = <&mcp4728>,"label";
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment