Logo

dev-resources.site

for different kinds of informations.

Packing Custom Fonts for NixOS

Published at
8/4/2024
Categories
nixos
fonts
nix
berkeleymono
Author
alioguzhan
Categories
4 categories in total
nixos
open
fonts
open
nix
open
berkeleymono
open
Author
10 person written this
alioguzhan
open
Packing Custom Fonts for NixOS

Originally published at yildiz.dev on April 5, 2024.

I have been using NixOS for a long time. And I am very happy with it. It is very stable and easy to configure.

With NixOS, installing and configuring famous and open-source fonts is already very straightforward. Here is my font configuration in configuration.nix:

fonts = {
    fontDir.enable = true;
    enableGhostscriptFonts = true;
    packages = with pkgs; [
      cantarell-fonts
      hack-font
      inter
      jetbrains-mono
      liberation_ttf
      monaspace
      noto-fonts
      ubuntu_font_family
      (nerdfonts.override { fonts = [ "FiraCode" "DroidSansMono" "JetBrainsMono" ]; })
    ];
  };
Enter fullscreen mode Exit fullscreen mode

Things get a little bit complicated when you want to use a custom or any commercial font that is not available in Nix packages. I am using Berkeley Mono. It is a great font for both the editor and the terminal. And here is how I packed it for NixOS:

Adding the Font to the NixOS Configuration

We need to add the font to our NixOS configuration so it will be installed and available after the build.

There are a few ways to do this. You can zip your font and add it to your config repository. Or you can serve the font files from
a web server with some credentials. Since my config repo is not public, I am using the first option. Here is how I did it:

First, create a derivation for our font:

# make a  derivation for berkeley-mono font installation
{ pkgs }:

pkgs.stdenv.mkDerivation {
  pname = "berkeley-mono-typeface";
  version = "1.009";

  src = ../../assets/berkeley-mono.zip;

  unpackPhase = ''
    runHook preUnpack
    ${pkgs.unzip}/bin/unzip $src

    runHook postUnpack
  '';

  installPhase = ''
    runHook preInstall

    install -Dm644 berkeley-mono-patched/*.ttf -t $out/share/fonts/truetype

    runHook postInstall
  '';
}
Enter fullscreen mode Exit fullscreen mode

Note that src part. It points to the zip file, which I put under the assets directory in my config repository.

I save this file as berkeley-mono-typeface.nix under the packages directory alongside my other custom packages.

Now we need to access this package from our configuration.nix file:

let
  berkeley-mono-typeface = pkgs.callPackage ./packages/berkeley-mono-typeface { inherit pkgs };
in
# other configurations
# ....

fonts = {
    fontDir.enable = true;
    enableGhostscriptFonts = true;
    packages = with pkgs; [
      cantarell-fonts
      hack-font
      inter
      jetbrains-mono
      liberation_ttf
      monaspace
      noto-fonts
      ubuntu_font_family
      (nerdfonts.override { fonts = [ "FiraCode" "DroidSansMono" "JetBrainsMono" ]; })
      berkeley-mono-typeface # It is here!
    ];
  };

Enter fullscreen mode Exit fullscreen mode

And that's it. Now rebuild your NixOS and you should be able to use your font.

Bonus: Patching the Font

Specifically, Berkeley Mono provides very few glyphs. To have all those fancy icons and symbols used in your editors and terminal, we
need to patch the font. Thankfully, it is very easy to patch the font with Nerd Font Patcher:

docker run --rm -v ./berkeley-mono:/in:Z -v ./berkeley-mono-patched:/out:Z nerdfonts/patcher --mono --adjust-line-height --progressbars --complete
Enter fullscreen mode Exit fullscreen mode

Assuming your berkeley-mono directory, which contains the TTF and OTF folders, this command will patch the fonts
and put them into the berkeley-mono-patched directory.

If you also want to patch your font before using it on your system, you can tweak the above command and use it.

With the above patch command, our font will be available with the name BerkeleyMono Nerd Font Mono. So you need to use this name when you want to use it in your editor or terminal. If you remove the --mono flag, then it will be BerkeleyMono Nerd Font. You get the idea.

Enjoy your new font!

nix Article's
30 articles in total
Favicon
Easy development environments with Nix and Nix flakes!
Favicon
A Conversation with Docker CTO Justin Cormack and Flux CEO Ron Efrani: The Future of Developer Environments
Favicon
Nice one
Favicon
NixOS - A Unique Linux Distribution
Favicon
Getting started with Nix and Nix Flakes
Favicon
My new Nix series!
Favicon
gRPC, Haskell, Nix, love, hate
Favicon
Easy way to setup Flutter Development Environment on NixOS without Flakes or dev-shell
Favicon
Dotfiles, the nix way
Favicon
Why Use Nix package manager, Even on macOS?
Favicon
Easy GitHub CLI Extensions with Nix
Favicon
Abusing Haskell: Executable Blog Posts
Favicon
Nix first steps
Favicon
Cross-Posting to Dev.to with API
Favicon
An Introduction to Nix for Ruby Developers
Favicon
Using niv to Manage Haskell Dependencies
Favicon
Don't Rebuild Yourself - an Intro to Nix Package Caches
Favicon
Packing Custom Fonts for NixOS
Favicon
Embrace the Power of Nix for Your Python + Rust Workflow
Favicon
How to Deploy Flutter on Upsun
Favicon
Declarative and reproducible environments with colima, nix and k8s
Favicon
Combining Nix with Terraform for better DevOps
Favicon
The Perfect System Configuration
Favicon
Azure Function app that runs Haskell Nix package
Favicon
A Journey to Find an Ultimate Development Environment
Favicon
The one thing I do not like about the Nix package manager (and a fix for it)
Favicon
Creating Repeatable Builds
Favicon
Develop R Packages under Nix Shell
Favicon
How I use Nix in my Elm projects
Favicon
Nix Quick Tips - Flake for OCaml

Featured ones: