me like nix
0

Configure Feed

Select the types of activity you want to include in your feed.

1{ pkgs, lib, config, ... }: 2 3let 4 cfg = config.pi; 5 6 # Workaround from https://github.com/NixOS/nixos-hardware/blob/master/raspberry-pi/4/apply-overlays-dtmerge.nix 7 deviceTree_overlay = _final: prev: { 8 deviceTree = { 9 applyOverlays = prev.callPackage ./overlays/apply-overlays-dtmerge.nix { }; 10 compileDTS = prev.deviceTree.compileDTS; 11 }; 12 }; 13 14 # Custom libcamera with Raspberry Pi IPA/pipeline support 15 libcamera-rpi = pkgs.libcamera.overrideAttrs (old: { 16 mesonFlags = (old.mesonFlags or [ ]) ++ [ 17 "-Dipas=rpi/vc4,rpi/pisp" 18 "-Dpipelines=rpi/vc4,rpi/pisp" 19 ]; 20 }); 21 22 # rpicam-apps using custom libcamera 23 rpicam-apps = pkgs.stdenv.mkDerivation rec { 24 pname = "rpicam-apps"; 25 version = "1.11.1"; 26 27 src = pkgs.fetchFromGitHub { 28 owner = "raspberrypi"; 29 repo = "rpicam-apps"; 30 rev = "v${version}"; 31 hash = "sha256-hVoKbvWFeramPkHuibJwUgFOPS9v588+K8828a1fNnA="; 32 }; 33 34 nativeBuildInputs = with pkgs; [ 35 meson 36 ninja 37 pkg-config 38 ]; 39 40 buildInputs = [ 41 libcamera-rpi 42 pkgs.libdrm 43 pkgs.libexif 44 pkgs.libjpeg 45 pkgs.libpng 46 pkgs.libtiff 47 pkgs.boost 48 pkgs.ffmpeg 49 ]; 50 51 mesonFlags = [ 52 "-Denable_libav=enabled" 53 "-Denable_drm=enabled" 54 "-Denable_egl=disabled" 55 "-Denable_qt=disabled" 56 "-Denable_opencv=disabled" 57 "-Denable_tflite=disabled" 58 "-Denable_hailo=disabled" 59 ]; 60 61 meta = with lib; { 62 description = "Raspberry Pi camera applications"; 63 homepage = "https://github.com/raspberrypi/rpicam-apps"; 64 license = licenses.bsd2; 65 platforms = [ "aarch64-linux" ]; 66 }; 67 }; 68in 69{ 70 options.pi = { 71 streamName = lib.mkOption { 72 type = lib.types.str; 73 description = "Name of the camera stream"; 74 }; 75 76 resolution = { 77 width = lib.mkOption { 78 type = lib.types.int; 79 default = 1920; 80 description = "Camera resolution width"; 81 }; 82 height = lib.mkOption { 83 type = lib.types.int; 84 default = 1080; 85 description = "Camera resolution height"; 86 }; 87 }; 88 89 framerate = lib.mkOption { 90 type = lib.types.int; 91 default = 30; 92 description = "Camera framerate"; 93 }; 94 95 deviceTreeFilter = lib.mkOption { 96 type = lib.types.str; 97 description = "Device tree filter pattern"; 98 }; 99 100 deviceTreeCompatible = lib.mkOption { 101 type = lib.types.str; 102 description = "Device tree compatible string (e.g., brcm,bcm2711)"; 103 }; 104 105 gpuMem = lib.mkOption { 106 type = lib.types.int; 107 default = 256; 108 description = "GPU memory allocation in MB"; 109 }; 110 }; 111 112 config = { 113 # Add device tree overlay for dtmerge support 114 nixpkgs.overlays = [ deviceTree_overlay ]; 115 116 # Disable ZFS which isn't supported on Pi 117 boot.supportedFilesystems = lib.mkForce [ "vfat" "ext4" ]; 118 119 # Pi kernel lacks device-mapper, so use legacy initrd (not systemd) 120 boot.initrd.systemd.enable = false; 121 122 # Enable SSH for headless setup 123 services.openssh = { 124 enable = true; 125 settings = { 126 PasswordAuthentication = false; 127 PermitRootLogin = "no"; 128 }; 129 }; 130 131 # User config 132 users.users.sean = { 133 isNormalUser = true; 134 extraGroups = [ "wheel" "video" ]; 135 openssh.authorizedKeys.keys = [ 136 "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDCIqgZ7kedxo+mOW7YG73Vp3zel3h180y3GKvHtRsXfGlpIIvRDy7pgCBQ4AGXYD4y78URQmFohYSAPqCPOPaWcU2un3XG9KvCzEsHmsbskPonitUmCiKvrKkb6oW4jCBtd7AEtBn+AiajAQFtPZ7NN2Df3AmTypvR6Irg7R+nxnfc9NTIHmGvxSDyWcbb4pguL20sctUSqGL6xGh8q/bqhdOThSimM+z9bEUNxK/5rPhwkNniMrp4pJcUrUiAh5/4DiRFG6KT+oeg+/myoz/Z1sPvAs7u/8JDQI4RshRD8Hu0oTkRBN6Hxj478q2SXbeBUZlD6IdjP3RhGpmSecoDdtWqKbpuV3eVRtQtba3KL86GBeV/bugaOdJ1Aud+1SOFJreAAuvxzMMKT+cdQZk6oOPP148DA/No+mDm/2S43lcdCXh79wA6YRAmKQ8jmZxTCtPutrvuZK1rguvvUlEoG/vhdNHh7eDa4Td07V6bjCRPUl8qk/e4M0E3pwsTlZc=" 137 ]; 138 }; 139 140 # Allow sudo without password for wheel group 141 security.sudo.wheelNeedsPassword = false; 142 143 # go2rtc for camera streaming to Home Assistant 144 services.go2rtc = { 145 enable = true; 146 settings = { 147 ffmpeg.bin = "${pkgs.ffmpeg}/bin/ffmpeg"; 148 streams = { 149 "${cfg.streamName}" = "exec:${rpicam-apps}/bin/rpicam-vid -t 0 --width ${toString cfg.resolution.width} --height ${toString cfg.resolution.height} --framerate ${toString cfg.framerate} --codec h264 --inline -o -"; 150 }; 151 }; 152 }; 153 154 # udev rule to give video group access to DMA heap devices (required for libcamera) 155 services.udev.extraRules = '' 156 SUBSYSTEM=="dma_heap", GROUP="video", MODE="0660" 157 ''; 158 159 # Override go2rtc systemd service to run as root for camera access 160 systemd.services.go2rtc.serviceConfig = { 161 User = lib.mkForce "root"; 162 }; 163 164 # Camera and system tools 165 environment.systemPackages = [ 166 pkgs.ffmpeg 167 pkgs.libraspberrypi 168 libcamera-rpi 169 rpicam-apps 170 pkgs.v4l-utils 171 ]; 172 173 # Device tree configuration for Pi Camera v3 (IMX708) 174 hardware.deviceTree.filter = cfg.deviceTreeFilter; 175 hardware.deviceTree.overlays = [ 176 { 177 name = "imx708-overlay"; 178 dtsText = '' 179 // SPDX-License-Identifier: GPL-2.0-only 180 // Definitions for IMX708 camera module on VC I2C bus 181 /dts-v1/; 182 /plugin/; 183 184 /{ 185 compatible = "${cfg.deviceTreeCompatible}"; 186 187 fragment@0 { 188 target = <&i2c0if>; 189 __overlay__ { 190 status = "okay"; 191 }; 192 }; 193 194 clk_frag: fragment@1 { 195 target = <&cam1_clk>; 196 __overlay__ { 197 status = "okay"; 198 clock-frequency = <24000000>; 199 }; 200 }; 201 202 fragment@2 { 203 target = <&i2c0mux>; 204 __overlay__ { 205 status = "okay"; 206 }; 207 }; 208 209 reg_frag: fragment@3 { 210 target = <&cam1_reg>; 211 cam_reg: __overlay__ { 212 startup-delay-us = <70000>; 213 off-on-delay-us = <30000>; 214 regulator-min-microvolt = <2700000>; 215 regulator-max-microvolt = <2700000>; 216 }; 217 }; 218 219 i2c_frag: fragment@100 { 220 target = <&i2c_csi_dsi>; 221 __overlay__ { 222 #address-cells = <1>; 223 #size-cells = <0>; 224 status = "okay"; 225 226 // IMX708 sensor configuration (from imx708.dtsi) 227 cam_node: imx708@1a { 228 compatible = "sony,imx708"; 229 reg = <0x1a>; 230 status = "okay"; 231 232 clocks = <&cam1_clk>; 233 clock-names = "inclk"; 234 235 vana1-supply = <&cam1_reg>; 236 vana2-supply = <&cam_dummy_reg>; 237 vdig-supply = <&cam_dummy_reg>; 238 vddl-supply = <&cam_dummy_reg>; 239 240 rotation = <180>; 241 orientation = <2>; 242 243 port { 244 cam_endpoint: endpoint { 245 clock-lanes = <0>; 246 data-lanes = <1 2>; 247 clock-noncontinuous; 248 link-frequencies = 249 /bits/ 64 <450000000>; 250 }; 251 }; 252 }; 253 254 // VCM (autofocus motor) configuration 255 vcm_node: dw9817@c { 256 compatible = "dongwoon,dw9817-vcm"; 257 reg = <0x0c>; 258 status = "okay"; 259 VDD-supply = <&cam1_reg>; 260 }; 261 }; 262 }; 263 264 csi_frag: fragment@101 { 265 target = <&csi1>; 266 csi: __overlay__ { 267 status = "okay"; 268 brcm,media-controller; 269 270 port { 271 csi_ep: endpoint { 272 remote-endpoint = <&cam_endpoint>; 273 clock-lanes = <0>; 274 data-lanes = <1 2>; 275 clock-noncontinuous; 276 }; 277 }; 278 }; 279 }; 280 281 __overrides__ { 282 rotation = <&cam_node>,"rotation:0"; 283 orientation = <&cam_node>,"orientation:0"; 284 media-controller = <&csi>,"brcm,media-controller?"; 285 cam0 = <&i2c_frag>, "target:0=",<&i2c_csi_dsi0>, 286 <&csi_frag>, "target:0=",<&csi0>, 287 <&clk_frag>, "target:0=",<&cam0_clk>, 288 <&reg_frag>, "target:0=",<&cam0_reg>, 289 <&cam_node>, "clocks:0=",<&cam0_clk>, 290 <&cam_node>, "vana1-supply:0=",<&cam0_reg>, 291 <&vcm_node>, "VDD-supply:0=",<&cam0_reg>; 292 vcm = <&vcm_node>, "status", 293 <0>, "=4"; 294 link-frequency = <&cam_endpoint>,"link-frequencies#0"; 295 }; 296 }; 297 298 &cam_endpoint { 299 remote-endpoint = <&csi_ep>; 300 }; 301 ''; 302 } 303 ]; 304 305 # Raspberry Pi firmware for camera 306 hardware.enableRedistributableFirmware = true; 307 308 # Add camera config and overlays to firmware partition 309 sdImage.populateFirmwareCommands = lib.mkAfter '' 310 chmod u+w ./firmware/config.txt 311 cat >> ./firmware/config.txt << EOF 312 313# Camera support - Pi Camera v3 (IMX708) 314gpu_mem=${toString cfg.gpuMem} 315EOF 316 317 # Copy device tree overlays for camera auto-detect 318 if [ -d ${pkgs.raspberrypifw}/share/raspberrypi/boot/overlays ]; then 319 cp -r ${pkgs.raspberrypifw}/share/raspberrypi/boot/overlays ./firmware/ 320 fi 321 ''; 322 323 # Networking 324 networking.networkmanager.enable = true; 325 326 # Firewall 327 networking.firewall.allowedTCPPorts = [ 328 22 # SSH 329 1984 # go2rtc API 330 8554 # RTSP 331 ]; 332 }; 333}