anchor.diff:          EOF
   
           # Copy the rootfs into the SD image
  -        eval $(partx $out -o START,SECTORS --nr 2 --pairs)
  -        dd conv=notrunc if=${rootfsImage} of=$out seek=$START count=$SECTORS
  +        eval $(partx $img -o START,SECTORS --nr 2 --pairs)
  +        dd conv=notrunc if=${rootfsImage} of=$img seek=$START count=$SECTORS
   
           # Create a FAT32 /boot partition of suitable size into bootpart.img
  -        eval $(partx $out -o START,SECTORS --nr 1 --pairs)
  +        eval $(partx $img -o START,SECTORS --nr 1 --pairs)
           truncate -s $((SECTORS * 512)) bootpart.img
           faketime "1970-01-01 00:00:00" mkfs.vfat -i 0x2178694e -n NIXOS_BOOT bootpart.img
   
  
positive.code:     sdImage.storePaths = [ config.system.build.toplevel ];
  
      system.build.sdImage = pkgs.stdenv.mkDerivation {
        name = "sd-image-${pkgs.stdenv.system}.img";
  
        buildInputs = with pkgs; [ dosfstools e2fsprogs mtools libfaketime utillinux ];
  
        buildCommand = ''
          # Create the image file sized to fit /boot and /, plus 20M of slack
          rootSizeBlocks=$(du -B 512 --apparent-size ${rootfsImage} | awk '{ print $1 }')
          bootSizeBlocks=$((${toString config.sdImage.bootSize} * 1024 * 1024 / 512))
          imageSize=$((rootSizeBlocks * 512 + bootSizeBlocks * 512 + 20 * 1024 * 1024))
          truncate -s $imageSize $out
  
          # type=b is 'W95 FAT32', type=83 is 'Linux'.
          sfdisk $out <<EOF
              label: dos
              label-id: 0x2178694e
  
  
negative.code: 
          # Copy the populated /boot into the SD image
          (cd boot; mcopy -bpsvm -i ../bootpart.img ./* ::)
          dd conv=notrunc if=bootpart.img of=$img seek=$START count=$SECTORS
        '';
      };
  
  