From afb60fc031ed27a0b806c5a477a23c714776308d Mon Sep 17 00:00:00 2001 From: mjallen18 Date: Fri, 26 Dec 2025 16:20:02 -0600 Subject: [PATCH] stuff --- .../x86_64-linux/matt@matt-nixos/default.nix | 1 - modules/nixos/nix/default.nix | 6 + overlays/linux-rpi/default.nix | 22 ++ .../raspberryPiWirelessFirmware/default.nix | 63 ++++ .../raspberrypi/raspberrypi-utils/default.nix | 6 +- .../raspberrypi/raspberrypifw/default.nix | 37 +++ packages/uboot/default.nix | 275 ++++++++++++++++++ packages/uboot/python.nix | 160 ++++++++++ systems/aarch64-linux/pi5/boot.nix | 2 +- systems/aarch64-linux/pi5/default.nix | 6 + 10 files changed, 573 insertions(+), 5 deletions(-) create mode 100644 overlays/linux-rpi/default.nix create mode 100644 packages/raspberrypi/raspberryPiWirelessFirmware/default.nix create mode 100644 packages/raspberrypi/raspberrypifw/default.nix create mode 100644 packages/uboot/default.nix create mode 100644 packages/uboot/python.nix diff --git a/homes/x86_64-linux/matt@matt-nixos/default.nix b/homes/x86_64-linux/matt@matt-nixos/default.nix index aa65446..d8add02 100755 --- a/homes/x86_64-linux/matt@matt-nixos/default.nix +++ b/homes/x86_64-linux/matt@matt-nixos/default.nix @@ -173,7 +173,6 @@ in heroic omnissa-horizon-client jq - lutris lzip morph orca-slicer diff --git a/modules/nixos/nix/default.nix b/modules/nixos/nix/default.nix index 184d3ce..5cb2638 100644 --- a/modules/nixos/nix/default.nix +++ b/modules/nixos/nix/default.nix @@ -41,10 +41,14 @@ "linuxPackages_cachyos-lto-server" "linuxPackages_cachyos-lto-server-znver4" "mesa" + "mesa_i686" "mesa-radeonsi-jupiter" + "mesa-radv-jupiter" "mgba" "onnxruntime" "opencv" + "opencv4" + "opencv4WithoutCuda" "pipewire" "qemu" "qemu_kvm" @@ -68,6 +72,8 @@ (_self: super: { ${namespace} = super.${namespace} // { linux-rpi = super.${namespace}.linux-rpi.override { stdenv = super.ccacheStdenv; }; + libraspberrypi = super.${namespace}.libraspberrypi.override { stdenv = super.ccacheStdenv; }; + raspberrypi-utils = super.${namespace}.raspberrypi-utils.override { stdenv = super.ccacheStdenv; }; }; }) (_self: super: { diff --git a/overlays/linux-rpi/default.nix b/overlays/linux-rpi/default.nix new file mode 100644 index 0000000..87c90e6 --- /dev/null +++ b/overlays/linux-rpi/default.nix @@ -0,0 +1,22 @@ +{ inputs, self, namespace, ... }: + +final: _prev: +let + pi5 = final.callPackage ../../packages/raspberrypi/linux-rpi/default.nix { + pkgs = final; + nixpkgs = inputs.nixpkgs; + inherit self; + }; + pi4 = final.callPackage ../../packages/raspberrypi/linux-rpi/default.nix { + pkgs = final; + nixpkgs = inputs.nixpkgs; + rpiVersion = 4; + inherit self; + }; +in +{ + # ${namespace} = final.${namespace} // { + linuxPackages_rpi5 = final.linuxPackagesFor pi5; + linuxPackages_rpi4 = final.linuxPackagesFor pi4; + # }; +} diff --git a/packages/raspberrypi/raspberryPiWirelessFirmware/default.nix b/packages/raspberrypi/raspberryPiWirelessFirmware/default.nix new file mode 100644 index 0000000..fd11eb3 --- /dev/null +++ b/packages/raspberrypi/raspberryPiWirelessFirmware/default.nix @@ -0,0 +1,63 @@ +{ + lib, + stdenvNoCC, + fetchFromGitHub, +}: + +stdenvNoCC.mkDerivation { + pname = "raspberrypi-wireless-firmware"; + version = "0-unstable-2025-04-08"; + + srcs = [ + (fetchFromGitHub { + name = "bluez-firmware"; + owner = "RPi-Distro"; + repo = "bluez-firmware"; + rev = "cdf61dc691a49ff01a124752bd04194907f0f9cd"; + hash = "sha256-35pnbQV/zcikz9Vic+2a1QAS72riruKklV8JHboL9NY="; + }) + (fetchFromGitHub { + name = "firmware-nonfree"; + owner = "RPi-Distro"; + repo = "firmware-nonfree"; + rev = "40dea60e27078fac57a3fed51010e2c26865d49b"; + hash = "sha256-yXKzrkr7zdw/ba8GEi0r+XjnZEsQ59LPEuXj0HaKwxU="; + }) + ]; + + sourceRoot = "."; + + dontBuild = true; + # Firmware blobs do not need fixing and should not be modified + dontFixup = true; + + installPhase = '' + runHook preInstall + mkdir -p "$out/lib/firmware/brcm" + + # Wifi firmware + cp -rv "$NIX_BUILD_TOP/firmware-nonfree/debian/config/brcm80211/." "$out/lib/firmware/" + + # Bluetooth firmware + cp -rv "$NIX_BUILD_TOP/bluez-firmware/debian/firmware/broadcom/." "$out/lib/firmware/brcm" + + # brcmfmac43455-sdio.bin is a symlink to the non-existent path: ../cypress/cyfmac43455-sdio.bin. + # See https://github.com/RPi-Distro/firmware-nonfree/issues/26 + ln -s "./cyfmac43455-sdio-standard.bin" "$out/lib/firmware/cypress/cyfmac43455-sdio.bin" + + pushd $out/lib/firmware/brcm &>/dev/null + # Symlinks for Zero 2W + ln -s "./brcmfmac43436-sdio.clm_blob" "$out/lib/firmware/brcm/brcmfmac43430b0-sdio.clm_blob" + popd &>/dev/null + + runHook postInstall + ''; + + meta = with lib; { + description = "Firmware for builtin Wifi/Bluetooth devices in the Raspberry Pi 3+ and Zero W"; + homepage = "https://github.com/RPi-Distro/firmware-nonfree"; + license = licenses.unfreeRedistributableFirmware; + platforms = platforms.linux; + maintainers = with maintainers; [ lopsided98 ]; + }; +} \ No newline at end of file diff --git a/packages/raspberrypi/raspberrypi-utils/default.nix b/packages/raspberrypi/raspberrypi-utils/default.nix index 7f9e845..0c533b3 100644 --- a/packages/raspberrypi/raspberrypi-utils/default.nix +++ b/packages/raspberrypi/raspberrypi-utils/default.nix @@ -10,13 +10,13 @@ stdenv.mkDerivation (_finalAttrs: { pname = "raspberrypi-utils"; - version = "unstable-2025-11-19"; + version = "unstable-2025-12-26"; src = fetchFromGitHub { owner = "raspberrypi"; repo = "utils"; - rev = "063d092c528c09b9dad5d7ed187ebf110a53303b"; - hash = "sha256-vi89lzfuLS9xKHDnlpm7Aw1UCOGCcMfd0cQq0rA9yOQ="; + rev = "230d67ad28e74b17a42064453b2163991cb51a5e"; + hash = "sha256-x3E8DwKKCmjxloCRB8djqNwuyrR4rok+JcNgPQ1ZH3Y="; }; buildInputs = [ diff --git a/packages/raspberrypi/raspberrypifw/default.nix b/packages/raspberrypi/raspberrypifw/default.nix new file mode 100644 index 0000000..753e351 --- /dev/null +++ b/packages/raspberrypi/raspberrypifw/default.nix @@ -0,0 +1,37 @@ +{ + lib, + stdenvNoCC, + fetchFromGitHub, +}: + +stdenvNoCC.mkDerivation rec { + # NOTE: this should be updated with linux_rpi + pname = "raspberrypi-firmware"; + version = "1.20250915"; + + src = fetchFromGitHub { + owner = "raspberrypi"; + repo = "firmware"; + rev = version; + hash = "sha256-DqVgsPhppxCsZ+H6S7XY5bBoRhOgPipKibDwikqBk08="; + }; + + installPhase = '' + mkdir -p $out/share/raspberrypi/ + mv boot "$out/share/raspberrypi/" + ''; + + dontConfigure = true; + dontBuild = true; + dontFixup = true; + + meta = with lib; { + description = "Firmware for the Raspberry Pi board"; + homepage = "https://github.com/raspberrypi/firmware"; + license = licenses.unfreeRedistributableFirmware; # See https://github.com/raspberrypi/firmware/blob/master/boot/LICENCE.broadcom + maintainers = with maintainers; [ dezgeg ]; + # Hash mismatch on source, mystery. + # Maybe due to https://github.com/NixOS/nix/issues/847 + broken = stdenvNoCC.hostPlatform.isDarwin; + }; +} \ No newline at end of file diff --git a/packages/uboot/default.nix b/packages/uboot/default.nix new file mode 100644 index 0000000..485b923 --- /dev/null +++ b/packages/uboot/default.nix @@ -0,0 +1,275 @@ +{ + stdenv, + lib, + bc, + bison, + dtc, + fetchFromGitHub, + fetchpatch, + fetchurl, + flex, + gnutls, + installShellFiles, + libuuid, + meson-tools, + ncurses, + openssl, + rkbin, + swig, + which, + python3, + perl, + armTrustedFirmwareAllwinner, + armTrustedFirmwareAllwinnerH6, + armTrustedFirmwareAllwinnerH616, + armTrustedFirmwareRK3328, + armTrustedFirmwareRK3399, + armTrustedFirmwareRK3568, + armTrustedFirmwareRK3588, + armTrustedFirmwareS905, + opensbi, + buildPackages, + callPackages, + darwin, +}@pkgs: + +let + defaultVersion = "2025.07"; + defaultSrc = fetchurl { + url = "https://ftp.denx.de/pub/u-boot/u-boot-${defaultVersion}.tar.bz2"; + hash = "sha256-D5M/bFpCaJW/MG6T5qxTxghw5LVM2lbZUhG+yZ5jvsc="; + }; + + # Dependencies for the tools need to be included as either native or cross, + # depending on which we're building + toolsDeps = [ + ncurses # tools/kwboot + libuuid # tools/mkeficapsule + gnutls # tools/mkeficapsule + openssl # tools/mkimage and tools/env/fw_printenv + ]; + + buildUBoot = lib.makeOverridable ( + { + version ? null, + src ? null, + filesToInstall, + pythonScriptsToInstall ? { }, + installDir ? "$out", + defconfig, + extraConfig ? "", + extraPatches ? [ ], + extraMakeFlags ? [ ], + extraMeta ? { }, + crossTools ? false, + stdenv ? pkgs.stdenv, + ... + }@args: + stdenv.mkDerivation ( + { + pname = "uboot-${defconfig}"; + + version = if src == null then defaultVersion else version; + + src = if src == null then defaultSrc else src; + + patches = extraPatches; + + postPatch = '' + ${lib.concatMapStrings (script: '' + substituteInPlace ${script} \ + --replace "#!/usr/bin/env python3" "#!${pythonScriptsToInstall.${script}}/bin/python3" + '') (builtins.attrNames pythonScriptsToInstall)} + patchShebangs tools + patchShebangs scripts + ''; + + nativeBuildInputs = [ + ncurses # tools/kwboot + bc + bison + flex + installShellFiles + (buildPackages.python3.withPackages (p: [ + p.libfdt + p.setuptools # for pkg_resources + p.pyelftools + ])) + swig + which # for scripts/dtc-version.sh + perl # for oid build (secureboot) + ] + ++ lib.optionals (!crossTools) toolsDeps + ++ lib.optionals stdenv.buildPlatform.isDarwin [ darwin.DarwinTools ]; # sw_vers command is needed on darwin + depsBuildBuild = [ buildPackages.gccStdenv.cc ]; # gccStdenv is needed for Darwin buildPlatform + buildInputs = lib.optionals crossTools toolsDeps; + + hardeningDisable = [ "all" ]; + + enableParallelBuilding = true; + + makeFlags = [ + "DTC=${lib.getExe buildPackages.dtc}" + "CROSS_COMPILE=${stdenv.cc.targetPrefix}" + "HOSTCFLAGS=-fcommon" + ] + ++ extraMakeFlags; + + passAsFile = [ "extraConfig" ]; + + configurePhase = '' + runHook preConfigure + + make -j$NIX_BUILD_CORES ${defconfig} + + cat $extraConfigPath >> .config + + runHook postConfigure + ''; + + installPhase = '' + runHook preInstall + + mkdir -p ${installDir} + cp ${ + lib.concatStringsSep " " (filesToInstall ++ builtins.attrNames pythonScriptsToInstall) + } ${installDir} + + mkdir -p "$out/nix-support" + ${lib.concatMapStrings (file: '' + echo "file binary-dist ${installDir}/${baseNameOf file}" >> "$out/nix-support/hydra-build-products" + '') (filesToInstall ++ builtins.attrNames pythonScriptsToInstall)} + + runHook postInstall + ''; + + dontStrip = true; + + meta = + with lib; + { + homepage = "https://www.denx.de/wiki/U-Boot/"; + description = "Boot loader for embedded systems"; + license = licenses.gpl2Plus; + maintainers = with maintainers; [ + dezgeg + lopsided98 + ]; + } + // extraMeta; + } + // removeAttrs args [ + "extraMeta" + "pythonScriptsToInstall" + ] + ) + ); +in +{ + inherit buildUBoot; + + ubootTools = buildUBoot { + defconfig = "tools-only_defconfig"; + installDir = "$out/bin"; + hardeningDisable = [ ]; + dontStrip = false; + extraMeta.platforms = lib.platforms.linux; + + crossTools = true; + extraMakeFlags = [ + "HOST_TOOLS_ALL=y" + "NO_SDL=1" + "cross_tools" + "envtools" + ]; + + outputs = [ + "out" + "man" + ]; + + postInstall = '' + installManPage doc/*.1 + + # from u-boot's tools/env/README: + # "You should then create a symlink from fw_setenv to fw_printenv. They + # use the same program and its function depends on its basename." + ln -s $out/bin/fw_printenv $out/bin/fw_setenv + ''; + + filesToInstall = [ + "tools/dumpimage" + "tools/fdt_add_pubkey" + "tools/fdtgrep" + "tools/kwboot" + "tools/mkeficapsule" + "tools/mkenvimage" + "tools/mkimage" + "tools/env/fw_printenv" + "tools/mkeficapsule" + ]; + + pythonScriptsToInstall = { + "tools/efivar.py" = (python3.withPackages (ps: [ ps.pyopenssl ])); + }; + }; + + ubootPythonTools = lib.recurseIntoAttrs (callPackages ./python.nix { }); + + ubootQemuAarch64 = buildUBoot { + defconfig = "qemu_arm64_defconfig"; + extraMeta.platforms = [ "aarch64-linux" ]; + filesToInstall = [ "u-boot.bin" ]; + }; + + ubootQemuArm = buildUBoot { + defconfig = "qemu_arm_defconfig"; + extraMeta.platforms = [ "armv7l-linux" ]; + filesToInstall = [ "u-boot.bin" ]; + }; + + ubootQemuRiscv64Smode = buildUBoot { + defconfig = "qemu-riscv64_smode_defconfig"; + extraMeta.platforms = [ "riscv64-linux" ]; + filesToInstall = [ "u-boot.bin" ]; + }; + + ubootQemuX86 = buildUBoot { + defconfig = "qemu-x86_defconfig"; + extraConfig = '' + CONFIG_USB_UHCI_HCD=y + CONFIG_USB_EHCI_HCD=y + CONFIG_USB_EHCI_GENERIC=y + CONFIG_USB_XHCI_HCD=y + ''; + extraMeta.platforms = [ + "i686-linux" + "x86_64-linux" + ]; + filesToInstall = [ "u-boot.rom" ]; + }; + + ubootQemuX86_64 = buildUBoot { + defconfig = "qemu-x86_64_defconfig"; + extraConfig = '' + CONFIG_USB_UHCI_HCD=y + CONFIG_USB_EHCI_HCD=y + CONFIG_USB_EHCI_GENERIC=y + CONFIG_USB_XHCI_HCD=y + ''; + extraMeta.platforms = [ "x86_64-linux" ]; + filesToInstall = [ "u-boot.rom" ]; + }; + + ubootRaspberryPi4 = buildUBoot { + defconfig = "rpi_4_defconfig"; + extraMeta.platforms = [ "aarch64-linux" ]; + filesToInstall = [ "u-boot.bin" ]; + }; + + ubootRaspberryPi5 = buildUBoot { + defconfig = "rpi_5_defconfig"; + extraMeta.platforms = [ "aarch64-linux" ]; + filesToInstall = [ "u-boot.bin" ]; + }; +} \ No newline at end of file diff --git a/packages/uboot/python.nix b/packages/uboot/python.nix new file mode 100644 index 0000000..9bd7f7f --- /dev/null +++ b/packages/uboot/python.nix @@ -0,0 +1,160 @@ +{ + lib, + python3Packages, + fetchPypi, + makeWrapper, + + armTrustedFirmwareTools, + bzip2, + cbfstool, + gzip, + lz4, + lzop, + openssl, + ubootTools, + vboot-utils, + xilinx-bootgen, + xz, + zstd, +}: + +let + # We are fetching from PyPI because the code in the repository seems to be + # lagging behind the PyPI releases somehow... + version = "0.0.7"; +in +rec { + + u_boot_pylib = python3Packages.buildPythonPackage rec { + pname = "u_boot_pylib"; + inherit version; + pyproject = true; + + src = fetchPypi { + inherit pname version; + hash = "sha256-A5r20Y8mgxhOhaKMpd5MJN5ubzPbkodAO0Tr0RN1SRA="; + }; + + build-system = with python3Packages; [ + setuptools + ]; + + checkPhase = '' + ${python3Packages.python.interpreter} "src/$pname/__main__.py" + # There are some tests in other files, but they are broken + ''; + + pythonImportsCheck = [ "u_boot_pylib" ]; + }; + + dtoc = python3Packages.buildPythonPackage rec { + pname = "dtoc"; + inherit version; + pyproject = true; + + src = fetchPypi { + inherit pname version; + hash = "sha256-NA96CznIxjqpw2Ik8AJpJkJ/ei+kQTCUExwFgssV+CM="; + }; + + build-system = with python3Packages; [ + setuptools + ]; + + dependencies = + (with python3Packages; [ + libfdt + ]) + ++ [ + u_boot_pylib + ]; + + pythonImportsCheck = [ "dtoc" ]; + }; + + binman = + let + btools = [ + armTrustedFirmwareTools + bzip2 + cbfstool + # TODO: cst + gzip + lz4 + # TODO: lzma_alone + lzop + openssl + ubootTools + vboot-utils + xilinx-bootgen + xz + zstd + ]; + in + python3Packages.buildPythonApplication rec { + pname = "binary_manager"; + inherit version; + pyproject = true; + + src = fetchPypi { + inherit pname version; + hash = "sha256-llEBBhUoW5jTEQeoaTCjZN8y6Kj+PGNUSB3cKpgD06w="; + }; + + patches = [ + ./binman-resources.patch + ]; + patchFlags = [ + "-p2" + "-d" + "src" + ]; + + build-system = with python3Packages; [ + setuptools + ]; + + nativeBuildInputs = [ makeWrapper ]; + + dependencies = + (with python3Packages; [ + jsonschema + pycryptodomex + pyelftools + yamllint + ]) + ++ [ + dtoc + u_boot_pylib + ]; + + preFixup = '' + wrapProgram "$out/bin/binman" --prefix PATH : "${lib.makeBinPath btools}" + ''; + }; + + patman = python3Packages.buildPythonApplication rec { + pname = "patch_manager"; + inherit version; + pyproject = true; + + src = fetchPypi { + inherit pname version; + hash = "sha256-zD9e87fpWKynpUcfxobbdk6wbM6Ja3f8hEVHS7DGIKQ="; + }; + + build-system = with python3Packages; [ + setuptools + ]; + + dependencies = + (with python3Packages; [ + aiohttp + pygit2 + ]) + ++ [ + u_boot_pylib + ]; + }; + +} \ No newline at end of file diff --git a/systems/aarch64-linux/pi5/boot.nix b/systems/aarch64-linux/pi5/boot.nix index 992abcc..97d0f78 100755 --- a/systems/aarch64-linux/pi5/boot.nix +++ b/systems/aarch64-linux/pi5/boot.nix @@ -8,7 +8,7 @@ in bootloader = "kernel"; firmwarePackage = pkgs.raspberrypifw; }; - kernelPackages = pkgs.linuxPackagesFor pkgs.${namespace}.linux-rpi; + kernelPackages = pkgs.linuxPackages_rpi5; supportedFilesystems = lib.mkForce [ ]; }; diff --git a/systems/aarch64-linux/pi5/default.nix b/systems/aarch64-linux/pi5/default.nix index 96b1ba0..1acfbb2 100644 --- a/systems/aarch64-linux/pi5/default.nix +++ b/systems/aarch64-linux/pi5/default.nix @@ -3,6 +3,7 @@ # https://search.nixos.org/options and in the NixOS manual (`nixos-help`). { + pkgs, namespace, ... }: @@ -15,6 +16,11 @@ virtualisation.docker.enable = true; + environment.systemPackages = with pkgs.${namespace}; [ + ubootRaspberryPi5 + ubootTools + ]; + ${namespace} = { # ###################################################