Cover Image

Why?

I recently bought a new font called Monolisa and wanted to use it in my NixOS system. I had a hard time figuring out how to do it, so I decided to write this guide to help others who might be in the same situation.

MonoLisa

Prerequisites

Before we start, make sure you have the font files in .ttf or .otf format. For this guide, I will be using the Monolisa font, but you can replace it with any other font you want to use.

I also assume that you have a basic understanding of Nix and NixOS. If you are new to Nix, I recommend checking out the Nix Pills guide to get started.

What exactly are we going to do?

We will be creating a package for the font and adding it to the Nix configuration. This will allow us to install the font system-wide and use it in any application that supports custom fonts.

Step 1: Writing a package definition

First, we need to create a package for the font. We will create a new file called your-font.nix in the packages directory.

packages/monolisa.nix
{
  stdenvNoCC,
  lib,
}:
stdenvNoCC.mkDerivation {
  pname = "your-font-pkg-name";
  version = "0.x";
  src = ./path/to/font/files;
 
  installPhase = ''
    mkdir -p $out/share/fonts/truetype/
    cp -r $src/*.{ttf,otf} $out/share/fonts/truetype/
  '';
 
  meta = with lib; {
    description = "Your Font Name";
    homepage = "https://www.yourfontwebsite.com/";
    platforms = platforms.all;
  };
}

Replace the placeholders with the appropriate values:

  • your-font-pkg-name: The name of the font package.
  • 0.x: The version of the font package.
  • ./path/to/font/files: The path to the font files.
  • Your Font Name: The name of the font.
  • https://www.yourfontwebsite.com/: The website of the font.

mkDerivation is a function provided by Nix that is used to define a package derivation. It takes a set of arguments that define the properties of the derivation.

In this case, we are using stdenvNoCC.mkDerivation to create a new derivation without the C compiler.

For the Monolisa font, the monolisa.nix file will look something like this:

packages/monolisa.nix
{
  stdenvNoCC,
  lib,
}:
stdenvNoCC.mkDerivation {
  pname = "monolisa-nerdfonts";
  version = "2.015";
  src = ./.;
 
  installPhase = ''
    mkdir -p $out/share/fonts/truetype/
    if [ -d "$src/fonts" ]; then
      cp -r $src/fonts/*.ttf $out/share/fonts/truetype/
    else
      echo "No fonts found in $src/fonts"
      exit 0
    fi
  '';
 
  meta = with lib; {
    description = "Monolisa Nerd Fonts";
    homepage = "https://www.monolisa.dev/";
    platforms = platforms.all;
  };
}

Since this is a paid font, I have added a check in installPhase to see if the font files are present in the fonts directory. If they are not found, the script will exit gracefully.

Step 2: Adding the font package

Next, we need to add the font package to the Nix configuration. Open the configuration.nix file and add the following:

configuration.nix
let
  monolisa-typeface = pkgs.callPackage ./packages/monolisa.nix { inherit pkgs };
in
 
# ...
# other config
# ...
 
fonts.packages = with pkgs; [
  monolisa-typeface
];

I’ve just used monolisa-typeface as an example. You can replace it with the name of your font package.

You can also check the NixOS Wiki for more information on configuring fonts in NixOS, and my fonts/default.nix file for an example.

Step 3: Rebuilding the system

After adding the font package to the configuration, we need to rebuild the system to apply the changes.

Depending on your system configuration, you may or may not be using flakes. If you are using flakes, you can run the following command:

sudo nixos-rebuild switch --flake .

If you are not using flakes, you can run the following command:

sudo nixos-rebuild switch

Step 4: Verifying the font installation

After rebuilding the system, we can verify that the font has been installed correctly by running the following command:

fc-list -v | grep -i "Your Font Name" # or "Monolisa" in my case

If the font is installed correctly, you should see the path to the font files in the output. For example:

family: "MonoLisaMono Nerd Font"(s) "MonoLisaMono Nerd Font Medium"(s)
fullname: "MonoLisaMono Nerd Font Medium"(s)
file: "/nix/store/5fzh6yff5hpj01vwhkxbdbri4lhl2cy8-monolisa-nerdfonts-2.015/share/fonts/truetype/MonoLisaMonoNerdFont-MediumRegular.ttf"(s)
postscriptname: "MonoLisaMonoNF-MediumRegular"(s)
family: "MonoLisaMono Nerd Font"(s)
fullname: "MonoLisaMono Nerd Font Bold"(s)
file: "/nix/store/5fzh6yff5hpj01vwhkxbdbri4lhl2cy8-monolisa-nerdfonts-2.015/share/fonts/truetype/MonoLisaMonoNerdFont-Bold.ttf"(s)
postscriptname: "MonoLisaMonoNF-Bold"(s)
family: "MonoLisaMono Nerd Font"(s) "MonoLisaMono Nerd Font Light"(s)
fullname: "MonoLisaMono Nerd Font Light"(s)
file: "/nix/store/5fzh6yff5hpj01vwhkxbdbri4lhl2cy8-monolisa-nerdfonts-2.015/share/fonts/truetype/MonoLisaMonoNerdFont-LightRegular.ttf"(s)
postscriptname: "MonoLisaMonoNF-LightRegular"(s)
family: "MonoLisaMono Nerd Font"(s)
fullname: "MonoLisaMono Nerd Font Italic"(s)
file: "/nix/store/5fzh6yff5hpj01vwhkxbdbri4lhl2cy8-monolisa-nerdfonts-2.015/share/fonts/truetype/MonoLisaMonoNerdFont-Italic.ttf"(s)
postscriptname: "MonoLisaMonoNF-Italic"(s)
family: "MonoLisaMono Nerd Font"(s)
fullname: "MonoLisaMono Nerd Font"(s)
file: "/nix/store/5fzh6yff5hpj01vwhkxbdbri4lhl2cy8-monolisa-nerdfonts-2.015/share/fonts/truetype/MonoLisaMonoNerdFont-Regular.ttf"(s)
postscriptname: "MonoLisaMonoNF-Regular"(s)
family: "MonoLisaMono Nerd Font"(s) "MonoLisaMono Nerd Font SemiBold"(s)
fullname: "MonoLisaMono Nerd Font SemiBold"(s)
file: "/nix/store/5fzh6yff5hpj01vwhkxbdbri4lhl2cy8-monolisa-nerdfonts-2.015/share/fonts/truetype/MonoLisaMonoNerdFont-SemiBoldRegular.ttf"(s)
postscriptname: "MonoLisaMonoNF-SemiBoldRegular"(s)
family: "MonoLisaMono Nerd Font"(s) "MonoLisaMono Nerd Font Light"(s)
fullname: "MonoLisaMono Nerd Font Light Italic"(s)
file: "/nix/store/5fzh6yff5hpj01vwhkxbdbri4lhl2cy8-monolisa-nerdfonts-2.015/share/fonts/truetype/MonoLisaMonoNerdFont-LightItalic.ttf"(s)
postscriptname: "MonoLisaMonoNF-LightItalic"(s)
family: "MonoLisaMono Nerd Font"(s)
fullname: "MonoLisaMono Nerd Font Bold Italic"(s)
file: "/nix/store/5fzh6yff5hpj01vwhkxbdbri4lhl2cy8-monolisa-nerdfonts-2.015/share/fonts/truetype/MonoLisaMonoNerdFont-BoldItalic.ttf"(s)
postscriptname: "MonoLisaMonoNF-BoldItalic"(s)
family: "MonoLisaMono Nerd Font"(s) "MonoLisaMono Nerd Font SemiBold"(s)
fullname: "MonoLisaMono Nerd Font SemiBold Italic"(s)
file: "/nix/store/5fzh6yff5hpj01vwhkxbdbri4lhl2cy8-monolisa-nerdfonts-2.015/share/fonts/truetype/MonoLisaMonoNerdFont-SemiBoldItalic.ttf"(s)
postscriptname: "MonoLisaMonoNF-SemiBoldItalic"(s)
family: "MonoLisaMono Nerd Font"(s) "MonoLisaMono Nerd Font Medium"(s)
fullname: "MonoLisaMono Nerd Font Medium Italic"(s)
file: "/nix/store/5fzh6yff5hpj01vwhkxbdbri4lhl2cy8-monolisa-nerdfonts-2.015/share/fonts/truetype/MonoLisaMonoNerdFont-MediumItalic.ttf"(s)
postscriptname: "MonoLisaMonoNF-MediumItalic"(s)

Conclusion

That’s it! You have successfully installed a custom font in NixOS. You can now use the font in any application that supports custom fonts.