{ config, lib, pkgs, namespace, ... }: let cfg = config.${namespace}.hardware.raspberry-pi; in { options.${namespace}.hardware.raspberry-pi = { enable = lib.mkEnableOption "Raspberry Pi common configuration"; variant = lib.mkOption { type = lib.types.enum [ "4" "5" ]; description = "Raspberry Pi variant (4 or 5)"; }; }; config = lib.mkIf cfg.enable { # Common Raspberry Pi packages environment.systemPackages = with pkgs; [ libraspberrypi raspberrypi-eeprom raspberrypifw raspberrypiWirelessFirmware raspberrypi-armstubs ] ++ lib.optionals (cfg.variant == "4") [ i2c-tools ] ++ lib.optionals (cfg.variant == "5") [ erofs-utils fex squashfuse squashfsTools ]; # Common nixpkgs overlays for Raspberry Pi nixpkgs.overlays = lib.mkAfter [ (_self: super: { # This is used in (modulesPath + "/hardware/all-firmware.nix") when at least # enableRedistributableFirmware is enabled inherit (super) raspberrypiWirelessFirmware; # Some derivations want to use it as an input, # e.g. raspberrypi-dtbs, omxplayer, sd-image-* modules inherit (super) raspberrypifw; }) ]; # Common Bluetooth configuration systemd.services.btattach = { before = [ "bluetooth.service" ]; after = [ "dev-ttyAMA0.device" ]; wantedBy = [ "multi-user.target" ]; serviceConfig = { ExecStart = "${pkgs.bluez}/bin/btattach -B /dev/ttyAMA0 -P bcm -S 3000000"; }; }; # Common hardware settings hardware.i2c.enable = lib.mkIf (cfg.variant == "4") true; # Pi 5 specific settings hardware.graphics.enable32Bit = lib.mkIf (cfg.variant == "5") (lib.mkForce false); zramSwap.enable = lib.mkIf (cfg.variant == "5") true; # Pi 5 specific system tags system.nixos.tags = lib.mkIf (cfg.variant == "5") ( let bootCfg = config.boot.loader.raspberry-pi; in [ "raspberry-pi-${bootCfg.variant}" bootCfg.bootloader config.boot.kernelPackages.kernel.version ] ); # Common programs programs.kdeconnect.enable = lib.mkDefault false; # Root user shell configuration users.users.root.shell = pkgs.zsh; }; }