Converting a pipeline from docker to singularity, Not able to access matplotlib/fontlist-v330

Hi,
I am trying to convert a pipeline from docker to singularity.
I have added this in config/default.config file

singularity {
    enabled = true
    autoMounts = true
}

However, I get this error related to matplotlib directory inside the container.

ERROR ~ Error executing process > 'parse_SourceVariant_workflow:parse_rMATS (1)'

Caused by:
  Process `parse_SourceVariant_workflow:parse_rMATS (1)` terminated with an error exit status (1)

Command executed:

  set -euo pipefail
  
  moPepGen parseRMATS          --se rmats_se.txt --a5ss rmats_a5ss.txt --a3ss rmats_a3ss.txt --mxe rmats_mxe.txt --ri rmats_ri.txt         --index-dir index         --output-path UCLA0001_rMATS_rMATS.gvf          --min-ijc 1 --min-sjc 1         --source rMATS

Command exit status:
  1

Command output:
  (empty)

Command error:
  INFO:    Environment variable SINGULARITYENV_TMPDIR is set, but APPTAINERENV_TMPDIR is preferred
  INFO:    Environment variable SINGULARITYENV_NXF_DEBUG is set, but APPTAINERENV_NXF_DEBUG is preferred
  Traceback (most recent call last):
    File "/usr/local/lib/python3.8/site-packages/matplotlib/font_manager.py", line 1429, in <module>
      fontManager = json_load(_fmcache)
    File "/usr/local/lib/python3.8/site-packages/matplotlib/font_manager.py", line 1012, in json_load
      with open(filename, 'r') as fh:
  FileNotFoundError: [Errno 2] No such file or directory: '/opt/matplotlib/fontlist-v330.json'

To solve this, in each module/.nf file I am adding

script:
    """
    export MPLCONFIGDIR=$PWD/.matplotlib
    mkdir -p $MPLCONFIGDIR
    """"

However, I think there should be a better way to it globally? I am a consumer of the pipeline.

I tried adding in the config file

process {
    beforeScript = '''
    export MPLCONFIGDIR=$PWD/.matplotlib
    mkdir -p $MPLCONFIGDIR
    '''

    environment = [
        'MPLCONFIGDIR': "$PWD/.matplotlib"
    ]
}

but then I get the same error again.

Command exit status:
  1

Command output:
  (empty)

Command error:
  INFO:    Environment variable SINGULARITYENV_TMPDIR is set, but APPTAINERENV_TMPDIR is preferred
  INFO:    Environment variable SINGULARITYENV_NXF_DEBUG is set, but APPTAINERENV_NXF_DEBUG is preferred
  Traceback (most recent call last):
    File "/usr/local/lib/python3.8/site-packages/matplotlib/font_manager.py", line 1429, in <module>
      fontManager = json_load(_fmcache)
    File "/usr/local/lib/python3.8/site-packages/matplotlib/font_manager.py", line 1012, in json_load
      with open(filename, 'r') as fh:
  FileNotFoundError: [Errno 2] No such file or directory: '/opt/matplotlib/fontlist-v330.json'
.
.
.
  OSError: [Errno 30] Read-only file system: '/opt/matplotlib/fontlist-v330.json.matplotlib-lock'

If the docker container runs some script through the --entrypoint ( use docker inspect to see it ) these scripts are not run when using apptainer or singularity. This usually means you have to check if something is set in the environment and if not then source that entrypoint script so stuff does get setup as it should. I had to do this for Quarto recently and added this to my bash code in the end:

    # Fix Quarto for apptainer
    ENV_QUARTO="\${ENV_QUARTO:-/opt/conda/etc/conda/activate.d/quarto.sh}"
    set +u
    if [ -z "\${QUARTO_DENO}" ] && [ -f "\${ENV_QUARTO}" ]; then
        source "\${ENV_QUARTO}"
    fi
    set -u