Build raspberry image with preinstalled software

I would like to create an SD image for a completely offline Raspberry PI 4.
Since the Raspberry will be offline, I want to preinstall all the binaries I need. Some binaries will come from flakes, thus I am trying to use the flake system.
I have another machine with aarch64 so I don’t need to cross-compile.
I am at the point where I can successfully create the image and the Raspberry boots with the following flake.nix (I started from How to move from building sd card images, to be able to remotely deploy new versions?):

{
  description = "Build Raspberry PI 4 image";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05";
  };
  outputs = { self, nixpkgs }: rec {
    nixosConfigurations.rpi4 = nixpkgs.lib.nixosSystem {
      system = "aarch64-linux";
      modules = [
        "${nixpkgs}/nixos/modules/installer/sd-card/sd-image-aarch64.nix"
        ({ ... }: {
          config = {
            time.timeZone = "Europe/Rome";
            i18n.defaultLocale = "it_IT.UTF-8";
            sdImage.compressImage = false;
            console.keyMap = "it";

            users.users.root.initialHashedPassword = "$y$j9T$/29noYRT4W/22Hy4lW7B71$MNtGBgjk01Zo3LtKgFRQtwaXdv6I15oiSgGGCMkt9s2"; # =test use mkpasswd to generate
            system = {
              stateVersion = "23.05";
            };
            #services.nginx = {
            #    enable = true;
            #};
            networking = {
              wireless.enable = false;
              useDHCP = false;
            };
            hardware.bluetooth.powerOnBoot = false;
          };
        })
      ];
    };
    images.rpi4 = nixosConfigurations.rpi4.config.system.build.sdImage;
  };
}

I build the image with: nix build .#images.rpi4 and flash it to the ssd with dd.

The Raspberry boots fine, however, I don’t know how to pre-install binaries in the image from nixpkgs such as gpg.

(if you feel ultra helpful following issues are: how to add binaries from flakes, and how to avoid bluetooth error message at login, I want to disable it)

Finally I was able to include some binaries.
First I removed the recursive definition using insetad let in and it’s more clear to me.
Then I discovered the environment.systemPackages field to inizialize. Here it is for who is interested:

{
  description = "Build Raspberry PI 4 image";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05";
  };
  outputs = { self, nixpkgs }: let
    system = "aarch64-linux";
    pkgs = import nixpkgs {
        inherit system;
    };
    nixosConfigurations.rpi4 = nixpkgs.lib.nixosSystem {
      inherit system pkgs;
      modules = [
        "${nixpkgs}/nixos/modules/installer/sd-card/sd-image-aarch64.nix"
        ({ ... }: {
          config = {
            time.timeZone = "Europe/Rome";
            i18n.defaultLocale = "it_IT.UTF-8";
            sdImage.compressImage = false;
            console.keyMap = "it";

            users.users.root.initialHashedPassword = "$y$j9T$/29noYRT4W/22Hy4lW7B71$MNtGBgjk01Zo3LtKgFRQtwaXdv6I15oiSgGGCMkt9s2"; # =test use mkpasswd to generate
            system = {
              stateVersion = "23.05";
            };
            networking = {
              wireless.enable = false;
              useDHCP = false;
            };
            environment.systemPackages = with pkgs; [ git gnupg ];
          };
        })
      ];
    };
    in {
      image.rpi4 = nixosConfigurations.rpi4.config.system.build.sdImage;
    };

}

Now I need to include binaries from flake instead of from nixpkgs and solve the bluetooth issue

Here is an example with another flake binary:

{
  description = "Build Raspberry PI 4 image";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05";
    fbbe.url = "github:RCasatta/fbbe";
  };
  outputs = { self, nixpkgs, fbbe }: let
    system = "aarch64-linux";
    pkgs = import nixpkgs {
      inherit system;
    };
    fbbe-pkg = fbbe.packages.${system};
    nixosConfigurations.rpi4 = nixpkgs.lib.nixosSystem {
      inherit system pkgs;
      modules = [
        "${nixpkgs}/nixos/modules/installer/sd-card/sd-image-aarch64.nix"
        ({ ... }: {
          config = {
            time.timeZone = "Europe/Rome";
            i18n.defaultLocale = "it_IT.UTF-8";
            sdImage.compressImage = false;
            console.keyMap = "it";

            users.users.root.initialHashedPassword = "$y$j9T$/29noYRT4W/22Hy4lW7B71$MNtGBgjk01Zo3LtKgFRQtwaXdv6I15oiSgGGCMkt9s2"; # =test use mkpasswd to generate
            system = {
              stateVersion = "23.05";
            };
            networking = {
              wireless.enable = false;
              useDHCP = false;
            };
            environment.systemPackages = [ pkgs.git pkgs.bitcoin pkgs.gnupg pkgs.pass pkgs.age pkgs.htop fbbe-pkg.fbbe ];
          };
        })
      ];
    };
    in {
      image.rpi4 = nixosConfigurations.rpi4.config.system.build.sdImage;
    };

}