Fonts: Difference between revisions

From NixOS Wiki
imported>Siers
m Tiny grammar updates
imported>Extends
Added an overlay for download specific nerd fonts.
Line 21: Line 21:
];
];
</syntaxhighlight>
</syntaxhighlight>
=== Installing specific fonts from nerdfonts ===
You probably want to install some fonts from nerdfonts repository and not the entiere repository (take much time to install all the fonts). To achieve that, you can use an overlay created by [https://github.com/lightdiscord/nix-nerd-fonts-overlay# LightDIscord]. Here is an example of how to use it:
<syntaxhighlight lang="nix">
{ pkgs, lib, config, ... }:
{
  nixpkgs.overlays = [ (import ./nix-nerd-fonts-overlay/default.nix) ]; #Assuming you cloned the repository on the same directory
  fonts.fonts = with pkgs; [
    nerd-fonts.firacode
    nerdfonts.jetbrainsmono
    nerdfonts.droidsansmono
  ];
}
</syntaxhighlight>
This will cause NixOS to download only fira code, jetbrains mono and droid sans mono fonts from nerd-fonts instead of the whole package.


=== Imperative installation of user fonts ===
=== Imperative installation of user fonts ===

Revision as of 08:58, 15 August 2020

Installing fonts on NixOS

NixOS has many font packages available, and you can easily search for your favourites on the NixOS packages site.

Despite looking like normal packages, simply adding these font packages to your environment.systemPackages won't make the fonts accessible to applications. To achieve that, put these packages in the fonts.fonts NixOS options list instead.


For example:

fonts.fonts = with pkgs; [
  noto-fonts
  noto-fonts-cjk
  noto-fonts-emoji
  liberation_ttf
  fira-code
  fira-code-symbols
  mplus-outline-fonts
  dina-font
  proggyfonts
];

Installing specific fonts from nerdfonts

You probably want to install some fonts from nerdfonts repository and not the entiere repository (take much time to install all the fonts). To achieve that, you can use an overlay created by LightDIscord. Here is an example of how to use it:

{ pkgs, lib, config, ... }:
{
  nixpkgs.overlays = [ (import ./nix-nerd-fonts-overlay/default.nix) ]; #Assuming you cloned the repository on the same directory
  fonts.fonts = with pkgs; [
    nerd-fonts.firacode
    nerdfonts.jetbrainsmono
    nerdfonts.droidsansmono
  ];
}

This will cause NixOS to download only fira code, jetbrains mono and droid sans mono fonts from nerd-fonts instead of the whole package.

Imperative installation of user fonts

This is useful for quick font experiments.

Example: Install SourceCodePro-Regular.

font=$(nix-build --no-out-link '<nixpkgs>' -A source-code-pro)/share/fonts/opentype/SourceCodePro-Regular.otf
cp $font ~/.local/share/fonts
fc-cache
# Verify that the font has been installed
fc-list -v | grep -i source


Set multiple fonts for different languages

If you are not a native English speaker and want to use another languages alongside English, you may want to set appropriate fonts for each language in your whole OS. In NixOS, it's so easy. Imagine I'm a Persian speaker and I want to set "Vazir" font to Persian texts and "Ubuntu" font for English texts. Just put these lines into your configuration.nix:

 #----=[ Fonts ]=----#
fonts = {
  enableDefaultFonts = true;
  fonts = [ 
    pkgs.ubuntu_font_family

    # Persian Fonts
    pkgs.vazir-fonts
  ];

  fontconfig = {
    penultimate.enable = false;
    defaultFonts = {
      serif = [ "Vazir" "Ubuntu" ];
      sansSerif = [ "Vazir" "Ubuntu" ];
      monospace = [ "Ubuntu" ];
    };
  };
};

Troubleshooting

What font names can be used in fonts.fontconfig.defaultFonts.monospace?

Those that fontconfig will understand. This can be queried from a font file using fc-query.

$ cd /nix/var/nix/profiles/system/sw/share/X11-fonts
$ fc-query DejaVuSans.ttf | grep '^\s\+family:' | cut -d'"' -f2

Adding personal fonts to ~/.fonts doesn't work

The ~/.fonts is being deprecated upstream[1]. It already is not working with NixOS.

The new preferred location is in $XDG_DATA_HOME/fonts, which for most users will resolve to ~/.local/share/fonts[2]