Ensure fonts in development environment

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?

If the program solely relies on font-cache I wouldn’t know a why to do it in a nix-shell. Can you supply it directories to search fonts in?

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.

8 Likes

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.

Because the official wiki is the official wiki and nixos.wiki is the dead/unofficial one.

Though realistically both wikis are questionable.

Ahh, OK, thanks. Copied the same text wiki.nixos.org. At least, they have the same syntax, so copying is trivial.

Next step: how to add a font as a dependency for a program? I have not found an example in nixpkgs yet.

Just nixpkgs/nixos/modules/services/monitoring/munin.nix at 5c48c7dc138a15255a334d6ef9ce68368bb8a1fe · NixOS/nixpkgs · GitHub but it’s not clear to me how to apply this to my package and other build*Package functions?

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:

{ pkgs ? import <nixpkgs> {} }:
let
  metadata = builtins.fromTOML (builtins.readFile ./pyproject.toml);
  fontsConf = pkgs.makeFontsConf {
    fontDirectories = [
      pkgs.atkinson-hyperlegible-next
    ];
  };
in
  with pkgs; python312Packages.buildPythonPackage rec {
    pname = metadata.project.name;
    version = metadata.project.version;
    pyproject = true;
    src = ./.;
    nativeBuildInputs = [ python312Packages.pdm-backend ];
    dependencies =      [
      python3
      python312Packages.typst
      typst
    ];
    pythonRelaxDeps = true;
    shellHook = ''
      export FONTCONFIG_FILE="${fontsConf}"
    '';
    meta = { ... }
    };
  }

Now, it compiles for a long time and says at the beginning “:tropical_drink: 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.

That error is caused by something else. Or you’re putting that code in the wrong place.

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.

fonts.packages is a NixOS configuration attribute, not a derivation attribute.

1 Like