I’m writing some code that generates PDF, and needs to be told the exact location of some font files.
I now realize that I’m woefully ignorant of how fonts are managed, so ignorant that I don’t really know what questions I’m trying to ask:
How can I ensure that the flake.nix which provisions the environment for this project, provides the required fonts?
Where should my program look for these fonts?
Some details
I’m using genpdf - Rust, and managed to get its hello-world example to work by hard-wiring /nix/var/nix/profiles/system/sw/share/X11/fonts on NixOS, but how can I ensure that it will also work on non-NixOS?
I haven’t worked with fonts much, but here are my thoughts:
How can I ensure that the flake.nix which provisions the environment for this project, provides the required fonts?
Include the font package as a dependency in the inputs section, I suppose? You can find a lot of fonts available from nixpkgs.
Where should my program look for these fonts?
fc-list tells you the name and location of available fonts. Depending on what programming language you’re using, there’s probably a library package that will run the query for you.
As this wasn’t mentioned yet:
In a development shell you will probably want to set:
export FONTCONFIG_FILE=...
In nixpkgs there’s a makeFontsConf that you can use with that.
See e.g.
Note that this can lead to undesired situations where globally installed fonts won’t be able to be found anymore inside this development shell unless you explicitly list global directories in your call to makeFontsConf.
To me, this note was not enough to do it on my own, but here a (for me) working solution is presented: https://programming.dev/post/32484220
I more or less copied these instruction to one wiki Fonts - NixOS Wiki (why is there also Fonts - Official NixOS Wiki which has not the same data but very similar information?). If one of you could add an explanation in this wiki section what exactly happens there, that would be great.
Indeed: if I just add fonts.packages = [ pkgs.atkinson-hyperlegible-next ];, I get the same error as earlier for nix-shell: error: cannot coerce a set to a string: { packages = «thunk»; }
Edit: Apparently it works exactly the same way as in shell.nix:
Now, it compiles for a long time and says at the beginning “ Building a mixed python/rust project” which sounds like I maybe could shorten build times if I somehow avoid this mangling. But that’s for another thread.
I meant: if I add it next to dependencies as a key in the attribute set that is an argument of buildPythonPackage, I get the error. fonts seems to be a different key of buildPythonPackage and mkShell.