ImageMagick's 'convert' command fails due to FontConfig Error

Running the following command

convert sourceimage.png -pointsize 60 -annotate +0+100 'Some Text' -font '@somefont.otf' temp1.jpg

Produces the following error:

Fontconfig error: Cannot load default config file: No such file: (null)


Researching this issue has not turned up any information that can aid me in resolving it. My current guess as to why the issue is occurring is that there is some path mapping issue which causes the fontconfig file to not be referenced.

Hoping the community here can point me in the right direction.

Currently running this on MacOS 12.4 and using Nix 2.10.3

Not sure how much this helps, but in my environment it successfully loads configurations from ImageMagick’s fontconfig dependency, NixOS’s /etc/fonts, and Home Manager’s $XDG_CONFIG_HOME/fontconfig:

$ nix-info
system: "x86_64-linux", multi-user?: yes, version: nix-env (Nix) 2.8.1, channels(root): "nixos-22.05, nixos-hardware", channels(user): "home-manager-22.05.tar.gz, nixos-unstable, rust-overlay", nixpkgs: /nix/var/nix/profiles/per-user/root/channels/nixos
$ nix-shell --pure --packages '[ imagemagick strace ]' --run "strace -e trace=open,openat convert -size '320x240' 'xc:white' -pointsize '60' -annotate '+0+100' 'Some Text' -font '@somefont.otf' 'ppm:-' | display" 2>&1 | grep --invert-match '\.so'
…
openat(AT_FDCWD, "/nix/store/h05495n7pm4g9c6qkjni18ll04q90ibh-fontconfig-2.13.94/etc/fonts/fonts.conf", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/etc/fonts/conf.d", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 3
…
openat(AT_FDCWD, "/home/user/.config/fontconfig/conf.d", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 4
…

Notably I don’t see it looking for somefont.otf so you might want to double-check that parameter, though I doubt it’s causing this problem.

To help with troubleshooting, convert -list font might be a simpler reproduction.

See Chromium in container `Fontconfig error: Cannot load default config file: No such file: (null)` · Issue #176081 · NixOS/nixpkgs · GitHub for a possible fix.

Thanks for the reference @jtojnar. After reading through the discussion you referenced I am still unclear as to what path forward I should take here. I am not trying to GUI apps inside a container, so I am wondering if the issue in my case is tied to how Nix represents its environment to ImageMagick.

In looking at the ImageMagick source it has some logic to detect the OS, and I am wondering if Nix shows up as linux vs. MacOS which is causing the different path to the fontconfig file to be set.

Any thoughts on how to resolve locally and/or the steps to a resolution? I am not familiar enough with manipulating my local Nix environment on my Mac to patch the issue.

Thanks!

The issue is that fontconfig requires /etc/fonts/fonts.conf file, which MacOS likely does not have, unless it uses fontconfig itself. So we would either need to patch fontconfig to look into a fallback path when that does not exist, or work around it by setting FONTCONFIG_FILE environment variable as folllows:

1 Like

Thanks so much for sharing those details. My nixpkgs appears to have the FONTCONFIG_FILE environment variable set.

Perhaps I am not in the right place? (see below)

I am looking at “/nix/store/6hmwry071zcdf5jphlrxgv3amgwwqc7n-nixpkgs/nixpkgs/pkgs/applications/misc/glom/default.nix#L112-115”

I meant that as an example – you will need to set the variable similarly in the shell you are running the command. E.g. add it to your shell.nix or manually run something like the following:

export FONTCONFIG_FILE=$(nix-build -E 'let pkgs = import <nixpkgs> { }; in pkgs.makeFontsConf { fontDirectories = [ pkgs.freefont_ttf ]; }')
1 Like