From 9ebf2ff9c255534177fff4a06687563ff15812e1 Mon Sep 17 00:00:00 2001 From: Travis Staton Date: Thu, 23 Feb 2023 14:22:49 -0500 Subject: [PATCH] config txt rendering --- overlay/default.nix | 2 +- overlay/raspberrypi-wireless-firmware.nix | 10 +-- rpi/config.nix | 80 +++++++++++++++++++++++ rpi/default.nix | 28 +++++++- 4 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 rpi/config.nix diff --git a/overlay/default.nix b/overlay/default.nix index 14e133e..a72eae3 100644 --- a/overlay/default.nix +++ b/overlay/default.nix @@ -74,7 +74,7 @@ in { fw = rpi-firmware-stable-src; wireless-fw = import ./raspberrypi-wireless-firmware.nix { bluez-firmware = rpi-bluez-firmware-src; - firmware-nonfree-src = rpi-firmware-nonfree-src; + firmware-nonfree = rpi-firmware-nonfree-src; }; }] // { latest = final.rpi-kernels."${latest}"; diff --git a/overlay/raspberrypi-wireless-firmware.nix b/overlay/raspberrypi-wireless-firmware.nix index e673922..1059bae 100644 --- a/overlay/raspberrypi-wireless-firmware.nix +++ b/overlay/raspberrypi-wireless-firmware.nix @@ -1,14 +1,15 @@ -{ bluez-firmware-src, firmware-nonfree-src }: +{ bluez-firmware, firmware-nonfree }: { lib, stdenvNoCC, fetchFromGitHub }: stdenvNoCC.mkDerivation { pname = "raspberrypi-wireless-firmware"; version = "2023-01-19"; - srcs = [ bluez-firmware-src firmware-nonfree-src ]; + srcs = [ ]; sourceRoot = "."; + dontUnpack = true; dontBuild = true; # Firmware blobs do not need fixing and should not be modified dontFixup = true; @@ -18,14 +19,13 @@ stdenvNoCC.mkDerivation { mkdir -p "$out/lib/firmware/brcm" # Wifi firmware - cp -rv "$NIX_BUILD_TOP/firmware-nonfree/debian/config/brcm80211/." "$out/lib/firmware/" + cp -rv "${firmware-nonfree}/debian/config/brcm80211/." "$out/lib/firmware/" # Bluetooth firmware - cp -rv "$NIX_BUILD_TOP/bluez-firmware/broadcom/." "$out/lib/firmware/brcm" + cp -rv "${bluez-firmware}/broadcom/." "$out/lib/firmware/brcm" # CM4 symlink must be added since it's missing from upstream pushd $out/lib/firmware/brcm &>/dev/null - ln -s "./brcmfmac43455-sdio.txt" "$out/lib/firmware/brcm/brcmfmac43455-sdio.raspberrypi,4-compute-module.txt" # There are two options for the brcmfmac43455 binary: minimal or # standard. For more info see the readme at: diff --git a/rpi/config.nix b/rpi/config.nix new file mode 100644 index 0000000..19efc94 --- /dev/null +++ b/rpi/config.nix @@ -0,0 +1,80 @@ +{ lib, config, pkgs, ... }: +let + cfg = config; + render-raspberrypi-config = let + render-options = opts: + lib.strings.concatStringsSep "\n" (render-dt-kvs opts); + render-dt-param = x: "dtparam=" + x; + render-dt-kv = k: v: + if isNull v then k else let vstr = toString v; in "${k}=${vstr}"; + render-dt-kvs = x: lib.attrsets.mapAttrsToList render-dt-kv x; + render-dt-overlay = { overlay, args }: + "dtoverlay=" + overlay + "\n" + + lib.strings.concatMapStringsSep "\n" render-dt-param args + "\n" + + "dtoverlay="; + render-base-dt-params = params: + lib.strings.concatMapStringsSep "\n" render-dt-param + (render-dt-kvs params); + render-dt-overlays = overlays: + lib.strings.concatMapStringsSep "\n" render-dt-overlay + (lib.attrsets.mapAttrsToList (k: v: { + overlay = k; + args = render-dt-kvs v; + }) overlays); + render-config-section = k: + { options, base-dtb-params, dt-overlays }: '' + [${k}] + ${render-options options} + ${render-base-dt-params base-dtb-params} + ${render-dt-overlays dt-overlays} + ''; + in conf: + lib.strings.concatStringsSep "\n" + (lib.attrsets.mapAttrsToList render-config-section conf); +in { + options = { + raspberrypi-config = let + raspberrypi-config-options = { + options = { + options = lib.mkOption { + type = with lib.types; attrsOf anything; + default = { }; + example = { + enable_gic = true; + armstub = "armstub8-gic.bin"; + arm_boost = true; + }; + }; + base-dtb-params = lib.mkOption { + type = with lib.types; attrsOf anything; + default = { }; + example = { + i2c = "on"; + audio = "on"; + }; + description = "parameters to pass to the base dtb"; + }; + dt-overlays = lib.mkOption { + type = with lib.types; attrsOf (attrsOf (nullOr str)); + default = { }; + example = { vc4-kms-v3d = { cma-256 = null; }; }; + description = "dtb overlays to apply"; + }; + }; + }; + in lib.mkOption { + type = with lib.types; attrsOf (submodule raspberrypi-config-options); + }; + raspberrypi-config-output = lib.mkOption { + type = lib.types.package; + default = pkgs.writeTextFile { + name = "config.txt"; + text = '' + # Auto-generated by nix. Modifications will be overwritten. + ${render-raspberrypi-config cfg.raspberrypi-config} + ''; + }; + }; + }; + +} diff --git a/rpi/default.nix b/rpi/default.nix index b34b4da..7a8186f 100644 --- a/rpi/default.nix +++ b/rpi/default.nix @@ -2,7 +2,33 @@ { lib, pkgs, config, ... }: { - imports = [ ../sd-image ]; + imports = [ ../sd-image ./config.nix ]; + + raspberrypi-config = { + pi4 = { + options = { + enable_gic = true; + armstub = "armstub8-gic.bin"; + arm_boost = true; + disable_overscan = true; + }; + dt-overlays = { vc4-kms-v3d-pi4 = { cma-512 = null; }; }; + }; + pi02 = { dt-overlays = { vc4-kms-v3d = { cma-256 = null; }; }; }; + all = { + options = { + kernel = "u-boot-rpi_arm64.bin"; + enable_uart = true; + avoid_warnings = true; + arm_64bit = true; + }; + base-dtb-params = { + i2c = "on"; + audio = "on"; + krnbt = "on"; + }; + }; + }; nixpkgs = { overlays = [ overlay ]; }; boot = {