Failed to build a Raspberry Pi image

Hello,
I’m trying to build an image for a raspberry 2. To do so, I’ve written the following flake.nix (taken from a wiki)

{
  description = "Build image";
  inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05";
  outputs = { self, nixpkgs }: rec {
    nixosConfigurations.rpi2 = nixpkgs.lib.nixosSystem {
      modules = [
        "${nixpkgs}/nixos/modules/installer/sd-card/sd-image-raspberrypi.nix"
        {
          nixpkgs.config.allowUnsupportedSystem = true;
          nixpkgs.hostPlatform.system = "armv7l-linux";
          nixpkgs.buildPlatform.system = "x86_64-linux"; #If you build on x86 other wise changes this.
          # ... extra configs as above
          system.stateVersion = "25.05";
        }
      ];
    };
    images.rpi2 = nixosConfigurations.rpi2.config.system.build.sdImage;
  };
}

then nix build .#images.rpi2. However, this fails with

error: builder for '/nix/store/nwwa1z7h7hsypywxb9ybv2qjdryfi2rb-efivar-armv7l-unknown-linux-gnueabihf-39.drv' failed with exit code 2;
       last 25 log lines:
       >       |                                                                                  long unsigned int
       >       |                                                                                %llx
       >   364 |                                       iter->esl->signature_list_size,
       >   365 |                                       (intmax_t)(iter->len - iter->offset), iter->offset);
       >       |                                                                             ~~~~~~~~~~~~
       >       |                                                                                 |
       >       |                                                                                 off_t {aka long long int}
       > esl-iter.c:396:29: error: cast to pointer from integer of different size [8;;https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Warning-Options.html#index-Wint-to-pointer-cast-Werror=int-to-pointer-cast8;;]
       >   396 |                 iter->esl = (efi_signature_list_t *)((intptr_t)iter->buf
       >       |                             ^
       > esl-iter.c:414:74: error: format '%lx' expects argument of type 'long unsigned int', but argument 4 has type 'off_t' {aka 'long long int'} [8;;https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Warning-Options.html#index-Wformat-Werror=format=8;;]
       >   414 |                         warnx("correcting ESL size from %d to %jd at 0x%lx",
       >       |                                                                        ~~^
       >       |                                                                          |
       >       |                                                                          long unsigned int
       >       |                                                                        %llx
       >   415 |                               iter->esl->signature_list_size,
       >   416 |                               (intmax_t)(iter->len - iter->offset), iter->offset);
       >       |                                                                     ~~~~~~~~~~~~
       >       |                                                                         |
       >       |                                                                         off_t {aka long long int}
       > cc1: all warnings being treated as errors
       > make[1]: *** [/build/source/src/include/rules.mk:53: esl-iter.o] Error 1
       > make[1]: Leaving directory '/build/source/src'
       > make: *** [Makefile:17: all] Error 2
       For full logs, run 'nix log /nix/store/nwwa1z7h7hsypywxb9ybv2qjdryfi2rb-efivar-armv7l-unknown-linux-gnueabihf-39.drv'.

The full log is

armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -fPIC  -DLIBEFIVAR_VERSION=39 ->
armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -fPIC  -DLIBEFIVAR_VERSION=39 ->
armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -fPIC  -DLIBEFIVAR_VERSION=39 ->
armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -fPIC  -DLIBEFIVAR_VERSION=39 ->
armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -fPIC  -DLIBEFIVAR_VERSION=39 ->
armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -fPIC  -DLIBEFIVAR_VERSION=39 ->
armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -fPIC  -DLIBEFIVAR_VERSION=39 ->
armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -fPIC  -DLIBEFIVAR_VERSION=39 ->
armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -fPIC  -DLIBEFIVAR_VERSION=39 ->
armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -fPIC  -DLIBEFIVAR_VERSION=39 ->
armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -fPIC  -DLIBEFIVAR_VERSION=39 ->
armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -fPIC  -DLIBEFIVAR_VERSION=39 ->
armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -L.   -Wl,--build-id -Wl,--no-a>
ln -vfs libefiboot.so libefiboot.so.1
'libefiboot.so.1' -> 'libefiboot.so'
armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -fPIC  -DLIBEFIVAR_VERSION=39 ->
armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -fPIC  -DLIBEFIVAR_VERSION=39 ->
armv7l-unknown-linux-gnueabihf-gcc -Og  -g3 -Wall -Wextra  -Werror  -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/build/source/src/include/gcc.specs -fno-merge-constants  -fPIC  -DLIBEFIVAR_VERSION=39 ->
esl-iter.c: In function 'esl_list_iter_next_with_size_correction':
esl-iter.c:338:80: error: format '%lx' expects argument of type 'long unsigned int', but argument 4 has type 'off_t' {aka 'long long int'} [-Werror=format=]
  338 |                                 warnx("correcting ESL size from %d to %jd at %lx",
      |                                                                              ~~^
      |                                                                                |
      |                                                                                long unsigned int
      |                                                                              %llx
  339 |                                       iter->esl->signature_list_size,
  340 |                                       (intmax_t)(iter->len - iter->offset), iter->offset);
      |                                                                             ~~~~~~~~~~~~
      |                                                                                 |
      |                                                                                 off_t {aka long long int}
esl-iter.c:363:82: error: format '%lx' expects argument of type 'long unsigned int', but argument 4 has type 'off_t' {aka 'long long int'} [-Werror=format=]
  363 |                                 warnx("correcting ESL size from %d to %jd at 0x%lx",
      |                                                                                ~~^
      |                                                                                  |
      |                                                                                  long unsigned int
      |                                                                                %llx
  364 |                                       iter->esl->signature_list_size,
  365 |                                       (intmax_t)(iter->len - iter->offset), iter->offset);
      |                                                                             ~~~~~~~~~~~~
      |                                                                                 |
      |                                                                                 off_t {aka long long int}
esl-iter.c:396:29: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
  396 |                 iter->esl = (efi_signature_list_t *)((intptr_t)iter->buf
      |                             ^
esl-iter.c:414:74: error: format '%lx' expects argument of type 'long unsigned int', but argument 4 has type 'off_t' {aka 'long long int'} [-Werror=format=]
  414 |                         warnx("correcting ESL size from %d to %jd at 0x%lx",
      |                                                                        ~~^
      |                                                                          |
      |                                                                          long unsigned int
      |                                                                        %llx
  415 |                               iter->esl->signature_list_size,
  416 |                               (intmax_t)(iter->len - iter->offset), iter->offset);
      |                                                                     ~~~~~~~~~~~~
      |                                                                         |
      |                                                                         off_t {aka long long int}
cc1: all warnings being treated as errors
make[1]: *** [/build/source/src/include/rules.mk:53: esl-iter.o] Error 1
make[1]: Leaving directory '/build/source/src'
make: *** [Makefile:17: all] Error 2

Does anyone have an idea on how to make it work?

My usual strategy with NixOS on RPi is to chop out anything which is tricky to build.

In this case, the package efivar gets installed from ${nixpkgs}/nixos/modules/profiles/base.nix, which is imported by sd-image-raspberrypi.nix.

We can remove efivar by chopping out the entire base.nix module:

{ modulesPath, ... }: {
  disabledModules = [ "${modulesPath}/profiles/base.nix" ];
}

Also it could be helpful to add the module ${nixpkgs}/nixos/modules/profiles/minimal.nix to your configuration. This will disable building things which aren’t usually necessary on a RPi, like documentation.

Another more surgical way of “removing” a package is through a nixpkgs overlay:

{
  nixpkgs.overlays = [ (final: prev: {
    efivar = final.emptyDirectory; # replace efivar with a dummy package
  }) ];
}

If your overlay will be used with other systems, then make your hacks conditional. Here is one of my overlays as an example:

final: prev: let
  inherit (final) lib;
  inherit (final.stdenv.hostPlatform) isAarch32;
  disableOnRpi = pkg: if isAarch32 then disabled else pkg;
  disabled = final.emptyDirectory;
  isCross = final.stdenv.buildPlatform != final.stdenv.hostPlatform;
in {
  # btrfs-progs fails to cross compile - disable it.
  btrfs-progs = if isCross then disabled else prev.btrfs-progs;
  # w3m fails to build on rpi - disable it.
  w3m-nographics = disableOnRpi prev.w3m-nographics;
}

You may encounter further build errors after this. Workarounds might be found in other Raspberry Pi threads on this forum, which can be found with a search.