MacOS + R: Library not found for -liconv when evaluating R package derivation

The following fails on MacOS aarch64

 nix-shell -p 'rWrapper.override{packages = [ rPackages.sf ];}'

with stdout including

f951: Warning: command-line option '-iframework /nix/store/ba1z2grzjm12bm7bvcjxrqgj8x2adqra-apple-framework-CoreFoundation-11.0.0/Library/Frameworks' is valid for C/C++/ObjC/ObjC++ but not for Fortran
/nix/store/48py6zrawzim9ghrnkqwm36jl4j1l23x-clang-wrapper-11.1.0/bin/cc -I"/nix/store/sddgzxiv53lz4q56q73alkxg6vaa3k85-R-4.2.1/lib/R/include" -DNDEBUG   -isystem /nix/store/10gwbik9qyh8dfv39mgrkl3wf62gk31z-libcxx-11.1.0-dev/include/c++/v1   -fPIC  -g -O2  -c init.c -o init.o
/nix/store/48py6zrawzim9ghrnkqwm36jl4j1l23x-clang-wrapper-11.1.0/bin/cc -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/nix/store/sddgzxiv53lz4q56q73alkxg6vaa3k85-R-4.2.1/lib/R/lib -L/nix/store/2bylcyd4g4c4vdvra5swnzgxcn0lq10g-libcxx-11.1.0/lib -o classInt.so fish1.o init.o -L/nix/store/7vsh6j8r3a7vialwl87qsj3df081iml5-gfortran-11.2.0-lib/lib -L/nix/store/nwgfmz856p5qwilrdpdbk5cp1yj8wm1s-gfortran-wrapper-11.2.0/bin -L/nix/store/fcp5vy436gmqcyzlqc2bfjhiyrjskjgz-gfortran-11.2.0/lib/gcc/aarch64-apple-darwin/11.2.0 -L/nix/store/fcp5vy436gmqcyzlqc2bfjhiyrjskjgz-gfortran-11.2.0/lib -lintl -liconv -lgfortran -lm -L/nix/store/sddgzxiv53lz4q56q73alkxg6vaa3k85-R-4.2.1/lib/R/lib -lR -lintl -Wl,-framework -Wl,CoreFoundation
ld: library not found for -liconv
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/nix/store/sddgzxiv53lz4q56q73alkxg6vaa3k85-R-4.2.1/lib/R/share/make/shlib.mk:10: classInt.so] Error 1
ERROR: compilation failed for package ‘classInt’
* removing ‘/nix/store/17lw6hncla0yh4hk0fyqw16db61ckq71-r-classInt-0.4-7/library/classInt’
error: builder for '/nix/store/qcgd31ad9bdh52a9jan8x8cwly0zcrw3-r-classInt-0.4-7.drv' failed with exit code 1;
       last 10 log lines:
       > f951: Warning: command-line option '-iframework /nix/store/ffldf8nh20a89hvv2kcq3x8b2rpd7y3l-apple-framework-OpenCL-11.0.0/Library/Frameworks' is valid for C/C++/ObjC/ObjC++ but not for Fortran
       > f951: Warning: command-line option '-iframework /nix/store/hwpcx0nzll0nxahk5774z22wsxzgpbln-apple-private-framework-UIFoundation-11.0.0/Library/Frameworks' is valid for C/C++/ObjC/ObjC++ but not for Fortran
       > f951: Warning: command-line option '-iframework /nix/store/ba1z2grzjm12bm7bvcjxrqgj8x2adqra-apple-framework-CoreFoundation-11.0.0/Library/Frameworks' is valid for C/C++/ObjC/ObjC++ but not for Fortran
       > /nix/store/48py6zrawzim9ghrnkqwm36jl4j1l23x-clang-wrapper-11.1.0/bin/cc -I"/nix/store/sddgzxiv53lz4q56q73alkxg6vaa3k85-R-4.2.1/lib/R/include" -DNDEBUG   -isystem /nix/store/10gwbik9qyh8dfv39mgrkl3wf62gk31z-libcxx-11.1.0-dev/include/c++/v1   -fPIC  -g -O2  -c init.c -o init.o
       > /nix/store/48py6zrawzim9ghrnkqwm36jl4j1l23x-clang-wrapper-11.1.0/bin/cc -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/nix/store/sddgzxiv53lz4q56q73alkxg6vaa3k85-R-4.2.1/lib/R/lib -L/nix/store/2bylcyd4g4c4vdvra5swnzgxcn0lq10g-libcxx-11.1.0/lib -o classInt.so fish1.o init.o -L/nix/store/7vsh6j8r3a7vialwl87qsj3df081iml5-gfortran-11.2.0-lib/lib -L/nix/store/nwgfmz856p5qwilrdpdbk5cp1yj8wm1s-gfortran-wrapper-11.2.0/bin -L/nix/store/fcp5vy436gmqcyzlqc2bfjhiyrjskjgz-gfortran-11.2.0/lib/gcc/aarch64-apple-darwin/11.2.0 -L/nix/store/fcp5vy436gmqcyzlqc2bfjhiyrjskjgz-gfortran-11.2.0/lib -lintl -liconv -lgfortran -lm -L/nix/store/sddgzxiv53lz4q56q73alkxg6vaa3k85-R-4.2.1/lib/R/lib -lR -lintl -Wl,-framework -Wl,CoreFoundation
       > ld: library not found for -liconv
       > clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
       > make: *** [/nix/store/sddgzxiv53lz4q56q73alkxg6vaa3k85-R-4.2.1/lib/R/share/make/shlib.mk:10: classInt.so] Error 1
       > ERROR: compilation failed for package ‘classInt’
       > * removing ‘/nix/store/17lw6hncla0yh4hk0fyqw16db61ckq71-r-classInt-0.4-7/library/classInt’
       For full logs, run 'nix log /nix/store/qcgd31ad9bdh52a9jan8x8cwly0zcrw3-r-classInt-0.4-7.drv'.
error: 1 dependencies of derivation '/nix/store/91xsabzq02sykwcmcwjb2y9ssvv2kn0n-r-sf-1.0-8.drv' failed to build
error: 1 dependencies of derivation '/nix/store/ijj5myfn7bx3hd4ckx97hwvqyp6gb8dy-R-4.2.1-wrapper.drv' failed to build

I am not sure what to do, suggestions welcome.

The above error can be overcome by editing the packagesWithNativeBuildInputs attribute in nixpkgs/pkgs/development/r-modules/default.nix to include

 classInt = [ ] ++ lib.optional stdenv.isDarwin pkgs.libiconv;
# i.e. classInt = [ pkgs.libiconv ];

The next error then includes

configure: pkg-config proj exists, will use it
configure: using proj.h.
Package libtiff-4 was not found in the pkg-config search path.
Perhaps you should add the directory containing `libtiff-4.pc'
to the PKG_CONFIG_PATH environment variable
Package 'libtiff-4', required by 'proj', not found
configure: PROJ: 9.0.0
checking PROJ: checking whether PROJ and sqlite3 are available for linking:... no
configure: error: libproj or sqlite3 not found in standard or given locations.
ERROR: configuration failed for package ‘sf’
* removing ‘/nix/store/x6s7cbyr5xicvh9k6nr3qc1qd7y3czqi-r-sf-1.0-8/library/sf’

with the notable line

checking PROJ: checking whether PROJ and sqlite3 are available for linking:... no

Furthermore, the relevant attributes of nixpkgs/pkgs/development/r-modules/default.nix look fine:

packagesWithNativeBuildInputs = {
...
    sf = with pkgs; [ gdal proj geos ];
...};

and

packagesWithNativeBuildInputs = {
...
    sf = with pkgs; [ pkg-config sqlite.dev proj.dev ];
...

although I suppose they are not fine for MacOS.

After recieving the suggestion to adjust sf’s buildInputs (here) I realised I could adjust the sf buildInputs using the otherOverrides overlay within the r-modules derivation (nixpkgs/pkgs/development/r-modules/default.nix):

otherOverrides = old: new: {
...

    sf = old.sf.overrideDerivation (attrs: with pkgs; {
      buildInputs = [ libtiff sqlite gdal proj geos ] ++ attrs.buildInputs;
    });
...
}

However, no effect was discernible.