Skip to content
This repository was archived by the owner on Mar 23, 2025. It is now read-only.

Commit 3a016ff

Browse files
authored
Merge pull request #79 from nix-community/flexible-partition-table-and-fs
Allow other partition tables and filesystems
2 parents 57e8e8e + 848c5ad commit 3a016ff

File tree

5 files changed

+55
-94
lines changed

5 files changed

+55
-94
lines changed

README.md

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,64 +19,7 @@ and `rpi/config.nix`. The other modules are mostly wrappers that set
1919

2020
## Example
2121

22-
See the `rpi-example` config in this flake for a CI-checked example.
23-
24-
```nix
25-
{
26-
description = "raspberry-pi-nix example";
27-
inputs = {
28-
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
29-
raspberry-pi-nix.url = "github:nix-community/raspberry-pi-nix";
30-
};
31-
32-
outputs = { self, nixpkgs, raspberry-pi-nix }:
33-
let
34-
inherit (nixpkgs.lib) nixosSystem;
35-
basic-config = { pkgs, lib, ... }: {
36-
# bcm2711 for rpi 3, 3+, 4, zero 2 w
37-
# bcm2712 for rpi 5
38-
# See the docs at:
39-
# https://www.raspberrypi.com/documentation/computers/linux_kernel.html#native-build-configuration
40-
raspberry-pi-nix.board = "bcm2711";
41-
time.timeZone = "America/New_York";
42-
users.users.root.initialPassword = "root";
43-
networking = {
44-
hostName = "basic-example";
45-
useDHCP = false;
46-
interfaces = {
47-
wlan0.useDHCP = true;
48-
eth0.useDHCP = true;
49-
};
50-
};
51-
hardware = {
52-
bluetooth.enable = true;
53-
raspberry-pi = {
54-
config = {
55-
all = {
56-
base-dt-params = {
57-
# enable autoprobing of bluetooth driver
58-
# https://github.com/raspberrypi/linux/blob/c8c99191e1419062ac8b668956d19e788865912a/arch/arm/boot/dts/overlays/README#L222-L224
59-
krnbt = {
60-
enable = true;
61-
value = "on";
62-
};
63-
};
64-
};
65-
};
66-
};
67-
};
68-
};
69-
70-
in {
71-
nixosConfigurations = {
72-
rpi-example = nixosSystem {
73-
system = "aarch64-linux";
74-
modules = [ raspberry-pi-nix.nixosModules.raspberry-pi basic-config ];
75-
};
76-
};
77-
};
78-
}
79-
```
22+
See the `rpi-example` config in this flake for an example config built by CI.
8023

8124
## Using the provided cache to avoid compiling linux
8225
This repo uses the raspberry pi linux kernel fork, and compiling linux takes a
@@ -87,10 +30,10 @@ to use this cache.
8730

8831
## Building an sd-card image
8932

90-
An image suitable for flashing to an sd-card can be found at the
91-
attribute `config.system.build.sdImage`. For example, if you wanted to
92-
build an image for `rpi-example` in the above configuration
93-
example you could run:
33+
Include the provided `sd-image` nixos module this flake provides, then an image
34+
suitable for flashing to an sd-card can be found at the attribute
35+
`config.system.build.sdImage`. For example, if you wanted to build an image for
36+
`rpi-example` in the above configuration example you could run:
9437

9538
```
9639
nix build '.#nixosConfigurations.rpi-example.config.system.build.sdImage'

flake.nix

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,18 @@
5353
core = import ./overlays (builtins.removeAttrs srcs [ "self" ]);
5454
libcamera = import ./overlays/libcamera.nix (builtins.removeAttrs srcs [ "self" ]);
5555
};
56-
nixosModules.raspberry-pi = import ./rpi {
57-
inherit pinned;
58-
core-overlay = self.overlays.core;
59-
libcamera-overlay = self.overlays.libcamera;
56+
nixosModules = {
57+
raspberry-pi = import ./rpi {
58+
inherit pinned;
59+
core-overlay = self.overlays.core;
60+
libcamera-overlay = self.overlays.libcamera;
61+
};
62+
sd-image = import ./sd-image;
6063
};
6164
nixosConfigurations = {
6265
rpi-example = srcs.nixpkgs.lib.nixosSystem {
6366
system = "aarch64-linux";
64-
modules = [ self.nixosModules.raspberry-pi ./example ];
67+
modules = [ self.nixosModules.raspberry-pi self.nixosModules.sd-image ./example ];
6568
};
6669
};
6770
checks.aarch64-linux = self.packages.aarch64-linux;

rpi/default.nix

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ let
66
version = cfg.kernel-version;
77
board = cfg.board;
88
kernel = config.system.build.kernel;
9+
initrd = "${config.system.build.initialRamdisk}/${config.system.boot.loader.initrdFile}";
910
in
1011
{
11-
imports = [ ../sd-image ./config.nix ./i2c.nix ];
12+
imports = [ ./config.nix ./i2c.nix ];
1213

1314
options = with lib; {
1415
raspberry-pi-nix = {
@@ -25,6 +26,11 @@ in
2526
without the _defconfig part.
2627
'';
2728
};
29+
firmware-partition-label = mkOption {
30+
default = "FIRMWARE";
31+
type = types.str;
32+
description = "label of rpi firmware partition";
33+
};
2834
pin-inputs = {
2935
enable = mkOption {
3036
default = true;
@@ -93,15 +99,15 @@ in
9399
{
94100
Type = "oneshot";
95101
MountImages =
96-
"/dev/disk/by-label/${config.sdImage.firmwarePartitionName}:${firmware-path}";
102+
"/dev/disk/by-label/${cfg.firmware-partition-label}:${firmware-path}";
97103
StateDirectory = "raspberrypi-firmware";
98104
ExecStart = pkgs.writeShellScript "migrate-rpi-firmware" ''
99105
shopt -s nullglob
100106
101107
TARGET_FIRMWARE_DIR="${firmware-path}"
102108
TARGET_OVERLAYS_DIR="$TARGET_FIRMWARE_DIR/overlays"
103109
TMPFILE="$TARGET_FIRMWARE_DIR/tmp"
104-
KERNEL="${kernel}/Image"
110+
KERNEL="${kernel}/${config.system.boot.loader.kernelFile}"
105111
SHOULD_UBOOT=${if cfg.uboot.enable then "1" else "0"}
106112
SRC_FIRMWARE_DIR="${pkgs.raspberrypifw}/share/raspberrypi/boot"
107113
STARTFILES=("$SRC_FIRMWARE_DIR"/start*.elf)
@@ -130,6 +136,8 @@ in
130136
touch "$STATE_DIRECTORY/kernel-migration-in-progress"
131137
cp "$KERNEL" "$TMPFILE"
132138
mv -T "$TMPFILE" "$TARGET_FIRMWARE_DIR/kernel.img"
139+
cp "${initrd}" "$TMPFILE"
140+
mv -T "$TMPFILE" "$TARGET_FIRMWARE_DIR/initrd"
133141
echo "${
134142
builtins.toString kernel
135143
}" > "$STATE_DIRECTORY/kernel-version"
@@ -243,6 +251,14 @@ in
243251
enable = true;
244252
value = if cfg.uboot.enable then "u-boot-rpi-arm64.bin" else "kernel.img";
245253
};
254+
ramfsfile = {
255+
enable = !cfg.uboot.enable;
256+
value = "initrd";
257+
};
258+
ramfsaddr = {
259+
enable = !cfg.uboot.enable;
260+
value = -1;
261+
};
246262
arm_64bit = {
247263
enable = true;
248264
value = true;
@@ -304,14 +320,9 @@ in
304320
kernelParams =
305321
if cfg.uboot.enable then [ ]
306322
else [
307-
# This is ugly and fragile, but the sdImage image has an msdos
308-
# table, so the partition table id is a 1-indexed hex
309-
# number. So, we drop the hex prefix and stick on a "02" to
310-
# refer to the root partition.
311-
"root=PARTUUID=${lib.strings.removePrefix "0x" config.sdImage.firmwarePartitionID}-02"
312-
"rootfstype=ext4"
313-
"fsck.repair=yes"
314-
"rootwait"
323+
"console=tty1"
324+
# https://github.com/raspberrypi/firmware/issues/1539#issuecomment-784498108
325+
"console=serial0,115200n8"
315326
"init=/sbin/init"
316327
];
317328
initrd = {

sd-image/default.nix

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88

99
boot.consoleLogLevel = lib.mkDefault 7;
1010

11-
# https://github.com/raspberrypi/firmware/issues/1539#issuecomment-784498108
12-
boot.kernelParams = [ "console=serial0,115200n8" "console=tty1" ];
11+
boot.kernelParams = [
12+
# This is ugly and fragile, but the sdImage image has an msdos
13+
# table, so the partition table id is a 1-indexed hex
14+
# number. So, we drop the hex prefix and stick on a "02" to
15+
# refer to the root partition.
16+
"root=PARTUUID=${lib.strings.removePrefix "0x" config.sdImage.firmwarePartitionID}-02"
17+
"rootfstype=ext4"
18+
"fsck.repair=yes"
19+
"rootwait"
20+
];
1321

1422
sdImage =
1523
let
@@ -22,14 +30,16 @@
2230
cfg = config.raspberry-pi-nix;
2331
version = cfg.kernel-version;
2432
board = cfg.board;
25-
kernel = config.system.build.kernel;
33+
kernel = "${config.system.build.kernel}/${config.system.boot.loader.kernelFile}";
34+
initrd = "${config.system.build.initialRamdisk}/${config.system.boot.loader.initrdFile}";
2635
populate-kernel =
2736
if cfg.uboot.enable
2837
then ''
2938
cp ${cfg.uboot.package}/u-boot.bin firmware/u-boot-rpi-arm64.bin
3039
''
3140
else ''
32-
cp "${kernel}/Image" firmware/kernel.img
41+
cp "${kernel}" firmware/kernel.img
42+
cp "${initrd}" firmware/initrd
3343
cp "${kernel-params}" firmware/cmdline.txt
3444
'';
3545
in

sd-image/sd-image.nix

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ let
3030
} // optionalAttrs (config.sdImage.rootPartitionUUID != null) {
3131
uuid = config.sdImage.rootPartitionUUID;
3232
});
33-
in {
33+
in
34+
{
3435
imports = [ ];
3536

3637
options.sdImage = {
@@ -82,14 +83,6 @@ in {
8283
'';
8384
};
8485

85-
firmwarePartitionName = mkOption {
86-
type = types.str;
87-
default = "FIRMWARE";
88-
description = ''
89-
Name of the filesystem which holds the boot firmware.
90-
'';
91-
};
92-
9386
rootPartitionUUID = mkOption {
9487
type = types.nullOr types.str;
9588
default = null;
@@ -160,7 +153,7 @@ in {
160153
config = {
161154
fileSystems = {
162155
"/boot/firmware" = {
163-
device = "/dev/disk/by-label/${config.sdImage.firmwarePartitionName}";
156+
device = "/dev/disk/by-label/${config.raspberry-pi-nix.firmware-partition-label}";
164157
fsType = "vfat";
165158
};
166159
"/" = {
@@ -226,7 +219,7 @@ in {
226219
# Create a FAT32 /boot/firmware partition of suitable size into firmware_part.img
227220
eval $(partx $img -o START,SECTORS --nr 1 --pairs)
228221
truncate -s $((SECTORS * 512)) firmware_part.img
229-
faketime "1970-01-01 00:00:00" mkfs.vfat -i ${config.sdImage.firmwarePartitionID} -n ${config.sdImage.firmwarePartitionName} firmware_part.img
222+
faketime "1970-01-01 00:00:00" mkfs.vfat -i ${config.sdImage.firmwarePartitionID} -n ${config.raspberry-pi-nix.firmware-partition-label} firmware_part.img
230223
231224
# Populate the files intended for /boot/firmware
232225
mkdir firmware
@@ -244,7 +237,8 @@ in {
244237
zstd -T$NIX_BUILD_CORES --rm $img
245238
fi
246239
'';
247-
}) { };
240+
})
241+
{ };
248242

249243
boot.postBootCommands = lib.mkIf config.sdImage.expandOnBoot ''
250244
# On the first boot do some maintenance tasks

0 commit comments

Comments
 (0)