Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Ali Oğuzhan Yıldız
Ali Oğuzhan Yıldız

Posted on

Packing Custom Fonts for NixOS

Originally published atyildiz.dev on April 5, 2024.

I have been usingNixOS 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 inconfiguration.nix:

fonts={fontDir.enable=true;enableGhostscriptFonts=true;packages=withpkgs;[cantarell-fontshack-fontinterjetbrains-monoliberation_ttfmonaspacenoto-fontsubuntu_font_family(nerdfonts.override{fonts=["FiraCode""DroidSansMono""JetBrainsMono"];})];};
Enter fullscreen modeExit 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 usingBerkeley 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 modeExit fullscreen mode

Note thatsrc part. It points to the zip file, which I put under theassets directory in my config repository.

I save this file asberkeley-mono-typeface.nix under thepackages directory alongside my other custom packages.

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

letberkeley-mono-typeface=pkgs.callPackage./packages/berkeley-mono-typeface{inheritpkgs};in# other configurations# ....fonts={fontDir.enable=true;enableGhostscriptFonts=true;packages=withpkgs;[cantarell-fontshack-fontinterjetbrains-monoliberation_ttfmonaspacenoto-fontsubuntu_font_family(nerdfonts.override{fonts=["FiraCode""DroidSansMono""JetBrainsMono"];})berkeley-mono-typeface# It is here!];};
Enter fullscreen modeExit 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 withNerd 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 modeExit fullscreen mode

Assuming yourberkeley-mono directory, which contains theTTF andOTF folders, this command will patch the fonts
and put them into theberkeley-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 nameBerkeleyMono 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 beBerkeleyMono Nerd Font. You get the idea.

Enjoy your new font!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Developer
  • Location
    Istanbul
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp