From d039a262cecc42897c4d8b3014e83cadbd9c46e3 Mon Sep 17 00:00:00 2001 From: Travis Staton Date: Thu, 3 Oct 2024 18:39:26 -0400 Subject: [PATCH 1/7] disable sd-image --- flake.nix | 13 ++++++++----- rpi/default.nix | 8 ++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/flake.nix b/flake.nix index ed8be8e..c0acdc3 100644 --- a/flake.nix +++ b/flake.nix @@ -53,15 +53,18 @@ core = import ./overlays (builtins.removeAttrs srcs [ "self" ]); libcamera = import ./overlays/libcamera.nix (builtins.removeAttrs srcs [ "self" ]); }; - nixosModules.raspberry-pi = import ./rpi { - inherit pinned; - core-overlay = self.overlays.core; - libcamera-overlay = self.overlays.libcamera; + nixosModules = { + raspberry-pi = import ./rpi { + inherit pinned; + core-overlay = self.overlays.core; + libcamera-overlay = self.overlays.libcamera; + }; + sd-image = import ./sd-image; }; nixosConfigurations = { rpi-example = srcs.nixpkgs.lib.nixosSystem { system = "aarch64-linux"; - modules = [ self.nixosModules.raspberry-pi ./example ]; + modules = [ self.nixosModules.raspberry-pi self.nixosModules.sd-image ./example ]; }; }; checks.aarch64-linux = self.packages.aarch64-linux; diff --git a/rpi/default.nix b/rpi/default.nix index 02b582a..57b93a1 100644 --- a/rpi/default.nix +++ b/rpi/default.nix @@ -8,7 +8,7 @@ let kernel = pkgs.rpi-kernels."${version}"."${board}"; in { - imports = [ ../sd-image ./config.nix ./i2c.nix ]; + imports = [ ./config.nix ./i2c.nix ]; options = with lib; { raspberry-pi-nix = { @@ -93,7 +93,7 @@ in { Type = "oneshot"; MountImages = - "/dev/disk/by-label/${config.sdImage.firmwarePartitionName}:${firmware-path}"; + "/dev/disk/by-label/FIRMWARE:${firmware-path}"; StateDirectory = "raspberrypi-firmware"; ExecStart = pkgs.writeShellScript "migrate-rpi-firmware" '' shopt -s nullglob @@ -308,8 +308,8 @@ in # table, so the partition table id is a 1-indexed hex # number. So, we drop the hex prefix and stick on a "02" to # refer to the root partition. - "root=PARTUUID=${lib.strings.removePrefix "0x" config.sdImage.firmwarePartitionID}-02" - "rootfstype=ext4" + # "root=PARTUUID=${lib.strings.removePrefix "0x" config.sdImage.firmwarePartitionID}-02" + # "rootfstype=ext4" "fsck.repair=yes" "rootwait" "init=/sbin/init" From ee53143215d82acfe159009b5745795828fddb5b Mon Sep 17 00:00:00 2001 From: Travis Staton Date: Sun, 3 Nov 2024 12:06:15 -0500 Subject: [PATCH 2/7] add initrd --- rpi/default.nix | 11 +++++++++++ sd-image/default.nix | 2 ++ 2 files changed, 13 insertions(+) diff --git a/rpi/default.nix b/rpi/default.nix index 57b93a1..8a15a9b 100644 --- a/rpi/default.nix +++ b/rpi/default.nix @@ -6,6 +6,7 @@ let version = cfg.kernel-version; board = cfg.board; kernel = pkgs.rpi-kernels."${version}"."${board}"; + initrd = "${config.system.build.initialRamdisk}/${config.system.boot.loader.initrdFile}"; in { imports = [ ./config.nix ./i2c.nix ]; @@ -130,6 +131,8 @@ in touch "$STATE_DIRECTORY/kernel-migration-in-progress" cp "$KERNEL" "$TMPFILE" mv -T "$TMPFILE" "$TARGET_FIRMWARE_DIR/kernel.img" + cp "${initrd}" "$TMPFILE" + mv -T "$TMPFILE" "$TARGET_FIRMWARE_DIR/initrd" echo "${ builtins.toString kernel }" > "$STATE_DIRECTORY/kernel-version" @@ -243,6 +246,14 @@ in enable = true; value = if cfg.uboot.enable then "u-boot-rpi-arm64.bin" else "kernel.img"; }; + ramfsfile = { + enable = !cfg.uboot.enable; + value = "initrd"; + }; + ramfsaddr = { + enable = !cfg.uboot.enable; + value = -1; + }; arm_64bit = { enable = true; value = true; diff --git a/sd-image/default.nix b/sd-image/default.nix index 9e9930a..fb9c684 100644 --- a/sd-image/default.nix +++ b/sd-image/default.nix @@ -23,6 +23,7 @@ version = cfg.kernel-version; board = cfg.board; kernel = pkgs.rpi-kernels."${version}"."${board}"; + initrd = "${config.system.build.initialRamdisk}/${config.system.boot.loader.initrdFile}"; populate-kernel = if cfg.uboot.enable then '' @@ -30,6 +31,7 @@ '' else '' cp "${kernel}/Image" firmware/kernel.img + cp "${initrd}" firmware/initrd cp "${kernel-params}" firmware/cmdline.txt ''; in From f872d7748f503af66ed577aaf9b621c69235f40c Mon Sep 17 00:00:00 2001 From: Travis Staton Date: Sun, 3 Nov 2024 20:20:01 -0500 Subject: [PATCH 3/7] shuffle kernel params --- rpi/default.nix | 10 ++-------- sd-image/default.nix | 11 ++++++++++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/rpi/default.nix b/rpi/default.nix index 8a15a9b..3d9bb64 100644 --- a/rpi/default.nix +++ b/rpi/default.nix @@ -315,14 +315,8 @@ in kernelParams = if cfg.uboot.enable then [ ] else [ - # This is ugly and fragile, but the sdImage image has an msdos - # table, so the partition table id is a 1-indexed hex - # number. So, we drop the hex prefix and stick on a "02" to - # refer to the root partition. - # "root=PARTUUID=${lib.strings.removePrefix "0x" config.sdImage.firmwarePartitionID}-02" - # "rootfstype=ext4" - "fsck.repair=yes" - "rootwait" + "console=tty1" + "console=serial0,115200n8" "init=/sbin/init" ]; initrd = { diff --git a/sd-image/default.nix b/sd-image/default.nix index fb9c684..a9b3f68 100644 --- a/sd-image/default.nix +++ b/sd-image/default.nix @@ -9,7 +9,16 @@ boot.consoleLogLevel = lib.mkDefault 7; # https://github.com/raspberrypi/firmware/issues/1539#issuecomment-784498108 - boot.kernelParams = [ "console=serial0,115200n8" "console=tty1" ]; + boot.kernelParams = [ + # This is ugly and fragile, but the sdImage image has an msdos + # table, so the partition table id is a 1-indexed hex + # number. So, we drop the hex prefix and stick on a "02" to + # refer to the root partition. + "root=PARTUUID=${lib.strings.removePrefix "0x" config.sdImage.firmwarePartitionID}-02" + "rootfstype=ext4" + "fsck.repair=yes" + "rootwait" + ]; sdImage = let From 61d43e92fd7530d0162d9125a6cb9f27b4e1e32c Mon Sep 17 00:00:00 2001 From: Travis Staton Date: Mon, 4 Nov 2024 22:57:26 -0500 Subject: [PATCH 4/7] use config.system.boot.loader.kernelFile --- rpi/default.nix | 2 +- sd-image/default.nix | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rpi/default.nix b/rpi/default.nix index c6d73ce..339d3f0 100644 --- a/rpi/default.nix +++ b/rpi/default.nix @@ -102,7 +102,7 @@ in TARGET_FIRMWARE_DIR="${firmware-path}" TARGET_OVERLAYS_DIR="$TARGET_FIRMWARE_DIR/overlays" TMPFILE="$TARGET_FIRMWARE_DIR/tmp" - KERNEL="${kernel}/Image" + KERNEL="${kernel}/${config.system.boot.loader.kernelFile}" SHOULD_UBOOT=${if cfg.uboot.enable then "1" else "0"} SRC_FIRMWARE_DIR="${pkgs.raspberrypifw}/share/raspberrypi/boot" STARTFILES=("$SRC_FIRMWARE_DIR"/start*.elf) diff --git a/sd-image/default.nix b/sd-image/default.nix index 99a1be4..931fb29 100644 --- a/sd-image/default.nix +++ b/sd-image/default.nix @@ -31,7 +31,7 @@ cfg = config.raspberry-pi-nix; version = cfg.kernel-version; board = cfg.board; - kernel = config.system.build.kernel; + kernel = "${config.system.build.kernel}/${config.system.boot.loader.kernelFile}"; initrd = "${config.system.build.initialRamdisk}/${config.system.boot.loader.initrdFile}"; populate-kernel = if cfg.uboot.enable @@ -39,7 +39,7 @@ cp ${cfg.uboot.package}/u-boot.bin firmware/u-boot-rpi-arm64.bin '' else '' - cp "${kernel}/Image" firmware/kernel.img + cp "${kernel}" firmware/kernel.img cp "${initrd}" firmware/initrd cp "${kernel-params}" firmware/cmdline.txt ''; From beb01f006a795fa1c3d46ca2a7a3dd199d919b0b Mon Sep 17 00:00:00 2001 From: Travis Staton Date: Mon, 11 Nov 2024 11:36:56 -0500 Subject: [PATCH 5/7] add firmware-partition-label option --- rpi/default.nix | 7 ++++++- sd-image/sd-image.nix | 18 ++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/rpi/default.nix b/rpi/default.nix index 339d3f0..70d9509 100644 --- a/rpi/default.nix +++ b/rpi/default.nix @@ -26,6 +26,11 @@ in without the _defconfig part. ''; }; + firmware-partition-label = mkOption { + default = "FIRMWARE"; + type = types.str; + description = "label of rpi firmware partition"; + }; pin-inputs = { enable = mkOption { default = true; @@ -94,7 +99,7 @@ in { Type = "oneshot"; MountImages = - "/dev/disk/by-label/FIRMWARE:${firmware-path}"; + "/dev/disk/by-label/${cfg.firmware-partition-label}:${firmware-path}"; StateDirectory = "raspberrypi-firmware"; ExecStart = pkgs.writeShellScript "migrate-rpi-firmware" '' shopt -s nullglob diff --git a/sd-image/sd-image.nix b/sd-image/sd-image.nix index 9ca69d8..60eedfc 100644 --- a/sd-image/sd-image.nix +++ b/sd-image/sd-image.nix @@ -30,7 +30,8 @@ let } // optionalAttrs (config.sdImage.rootPartitionUUID != null) { uuid = config.sdImage.rootPartitionUUID; }); -in { +in +{ imports = [ ]; options.sdImage = { @@ -82,14 +83,6 @@ in { ''; }; - firmwarePartitionName = mkOption { - type = types.str; - default = "FIRMWARE"; - description = '' - Name of the filesystem which holds the boot firmware. - ''; - }; - rootPartitionUUID = mkOption { type = types.nullOr types.str; default = null; @@ -160,7 +153,7 @@ in { config = { fileSystems = { "/boot/firmware" = { - device = "/dev/disk/by-label/${config.sdImage.firmwarePartitionName}"; + device = "/dev/disk/by-label/${config.raspberry-pi-nix.firmware-partition-label}"; fsType = "vfat"; }; "/" = { @@ -226,7 +219,7 @@ in { # Create a FAT32 /boot/firmware partition of suitable size into firmware_part.img eval $(partx $img -o START,SECTORS --nr 1 --pairs) truncate -s $((SECTORS * 512)) firmware_part.img - faketime "1970-01-01 00:00:00" mkfs.vfat -i ${config.sdImage.firmwarePartitionID} -n ${config.sdImage.firmwarePartitionName} firmware_part.img + faketime "1970-01-01 00:00:00" mkfs.vfat -i ${config.sdImage.firmwarePartitionID} -n ${config.raspberry-pi-nix.firmware-partition-label} firmware_part.img # Populate the files intended for /boot/firmware mkdir firmware @@ -244,7 +237,8 @@ in { zstd -T$NIX_BUILD_CORES --rm $img fi ''; - }) { }; + }) + { }; boot.postBootCommands = lib.mkIf config.sdImage.expandOnBoot '' # On the first boot do some maintenance tasks From b7c3578710785c93c7d9e714bb1c8dd2421875bb Mon Sep 17 00:00:00 2001 From: Travis Staton Date: Tue, 12 Nov 2024 12:31:36 -0500 Subject: [PATCH 6/7] move comment --- rpi/default.nix | 1 + sd-image/default.nix | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/rpi/default.nix b/rpi/default.nix index 70d9509..5900692 100644 --- a/rpi/default.nix +++ b/rpi/default.nix @@ -321,6 +321,7 @@ in if cfg.uboot.enable then [ ] else [ "console=tty1" + # https://github.com/raspberrypi/firmware/issues/1539#issuecomment-784498108 "console=serial0,115200n8" "init=/sbin/init" ]; diff --git a/sd-image/default.nix b/sd-image/default.nix index 931fb29..b845de3 100644 --- a/sd-image/default.nix +++ b/sd-image/default.nix @@ -8,7 +8,6 @@ boot.consoleLogLevel = lib.mkDefault 7; - # https://github.com/raspberrypi/firmware/issues/1539#issuecomment-784498108 boot.kernelParams = [ # This is ugly and fragile, but the sdImage image has an msdos # table, so the partition table id is a 1-indexed hex From 848c5add7f5c4ad0792c86d9db620c6aa4abc30e Mon Sep 17 00:00:00 2001 From: Travis Staton Date: Tue, 12 Nov 2024 12:36:49 -0500 Subject: [PATCH 7/7] readme tweaks --- README.md | 67 +++++-------------------------------------------------- 1 file changed, 5 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 91ca157..4c7882e 100644 --- a/README.md +++ b/README.md @@ -19,64 +19,7 @@ and `rpi/config.nix`. The other modules are mostly wrappers that set ## Example -See the `rpi-example` config in this flake for a CI-checked example. - -```nix -{ - description = "raspberry-pi-nix example"; - inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05"; - raspberry-pi-nix.url = "github:nix-community/raspberry-pi-nix"; - }; - - outputs = { self, nixpkgs, raspberry-pi-nix }: - let - inherit (nixpkgs.lib) nixosSystem; - basic-config = { pkgs, lib, ... }: { - # bcm2711 for rpi 3, 3+, 4, zero 2 w - # bcm2712 for rpi 5 - # See the docs at: - # https://www.raspberrypi.com/documentation/computers/linux_kernel.html#native-build-configuration - raspberry-pi-nix.board = "bcm2711"; - time.timeZone = "America/New_York"; - users.users.root.initialPassword = "root"; - networking = { - hostName = "basic-example"; - useDHCP = false; - interfaces = { - wlan0.useDHCP = true; - eth0.useDHCP = true; - }; - }; - hardware = { - bluetooth.enable = true; - raspberry-pi = { - config = { - all = { - base-dt-params = { - # enable autoprobing of bluetooth driver - # https://github.com/raspberrypi/linux/blob/c8c99191e1419062ac8b668956d19e788865912a/arch/arm/boot/dts/overlays/README#L222-L224 - krnbt = { - enable = true; - value = "on"; - }; - }; - }; - }; - }; - }; - }; - - in { - nixosConfigurations = { - rpi-example = nixosSystem { - system = "aarch64-linux"; - modules = [ raspberry-pi-nix.nixosModules.raspberry-pi basic-config ]; - }; - }; - }; -} -``` +See the `rpi-example` config in this flake for an example config built by CI. ## Using the provided cache to avoid compiling linux This repo uses the raspberry pi linux kernel fork, and compiling linux takes a @@ -87,10 +30,10 @@ to use this cache. ## Building an sd-card image -An image suitable for flashing to an sd-card can be found at the -attribute `config.system.build.sdImage`. For example, if you wanted to -build an image for `rpi-example` in the above configuration -example you could run: +Include the provided `sd-image` nixos module this flake provides, then an image +suitable for flashing to an sd-card can be found at the attribute +`config.system.build.sdImage`. For example, if you wanted to build an image for +`rpi-example` in the above configuration example you could run: ``` nix build '.#nixosConfigurations.rpi-example.config.system.build.sdImage'