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.
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.
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:
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:
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:
If you are not using flakes, you can run the following command:
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:
If the font is installed correctly, you should see the path to the font files in the output. For example:
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.