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!

nixos Article's
30 articles in total
Favicon
How to Set Up Kanata on NixOS: A Step-by-Step Guide
Favicon
NixOS - A Unique Linux Distribution
Favicon
NixOS Full Disk Encryption with USB/SD-Card/Password Unlock
Favicon
Choosing my distro: Ubuntu, NixOS, Elementary OS or Pop!_OS
Favicon
Nix first steps
Favicon
OpenResty on NixOS for an API Gateway
Favicon
Why Consider NixOS for Your Environment?
Favicon
Managing NixOS on DigitalOcean with Colmena
Favicon
I Changed My Mind - NixOS is NOT the Best Linux
Favicon
Backup GitHub Repositories with gidek
Favicon
Packing Custom Fonts for NixOS
Favicon
Managing NixOS Secrets via SOPS, sops-nix and opsops
Favicon
Running NixOS Guests on QEMU
Favicon
Archiving PostgreSQL Backups on NixOS
Favicon
The Ultimate NixOS Homelab Guide - Flakes, Modules and Fail2Ban w/ Cloudflare
Favicon
The radical concept of NixOS and why I love it!
Favicon
The Ultimate NixOS Homelab Guide - The Install
Favicon
The One Man Stack
Favicon
I use NixOS for my home-server, and you should too!
Favicon
This distro finally fixes Linux
Favicon
Running a Terraria Dedicated Server on NixOS
Favicon
NixOS on Raspberry Pi 4 with Encrypted Filesystem
Favicon
Day 7: Uninstalling NixOS from my Macbook Pro
Favicon
Nixpkgs: A community-maintained package lock file for all of Unix
Favicon
Writing a (Nix-Friendly) Hello World Web App in Haskell with Scotty
Favicon
Kick the Mix-ups to the Curb: Nix the Tricks and Juggle Go Projects like a Pro
Favicon
Day 6: Making rEFInd Look Good
Favicon
Day 5: NixOS & Git - version control for config.nix
Favicon
Day 4: (Re)Installing NixOS on my Macbook Pro
Favicon
Day 3: Fighting with network interfaces

Featured ones: