Difference between revisions of "Fonts"

From NixOS Wiki
Jump to: navigation, search
m (This snippet uses `with pkgs;`, so the extra `pkgs` qualifiers are redundant)
(Fix troubleshooting about fonts problem of flatpak applications)
 
(16 intermediate revisions by 10 users not shown)
Line 3: Line 3:
 
NixOS has many font packages available, and you can easily search for your favourites on the [https://search.nixos.org/packages NixOS packages site].  
 
NixOS has many font packages available, and you can easily search for your favourites on the [https://search.nixos.org/packages NixOS packages site].  
  
Despite looking like normal packages, simply adding these font packages to your <code>environment.systemPackages</code> won't make the fonts accessible to applications. To achieve that, put these packages in the <code>[https://search.nixos.org/options/?query=fonts.fonts fonts.fonts]</code> NixOS options list instead.
+
Despite looking like normal packages, simply adding these font packages to your <code>environment.systemPackages</code> won't make the fonts accessible to applications. To achieve that, put these packages in the <code>[https://search.nixos.org/options?channel=unstable&show=fonts.packages&from=0&size=50&sort=relevance&type=packages&query=fonts.packages fonts.packages]</code> NixOS options list instead.
  
 +
'''Note''': for 23.05 or older, <code>fonts.packages</code> is called <code>fonts.fonts</code> instead.
  
 
For example:  
 
For example:  
  
 
<syntaxhighlight lang="nix">
 
<syntaxhighlight lang="nix">
fonts.fonts = with pkgs; [
+
fonts.packages = with pkgs; [
 
   noto-fonts
 
   noto-fonts
 
   noto-fonts-cjk
 
   noto-fonts-cjk
Line 16: Line 17:
 
   fira-code
 
   fira-code
 
   fira-code-symbols
 
   fira-code-symbols
   mplus-outline-fonts
+
   mplus-outline-fonts.githubRelease
 
   dina-font
 
   dina-font
 
   proggyfonts
 
   proggyfonts
Line 27: Line 28:
  
 
<syntaxhighlight lang="nix">
 
<syntaxhighlight lang="nix">
fonts.fonts = with pkgs; [
+
fonts.packages = with pkgs; [
 
   (nerdfonts.override { fonts = [ "FiraCode" "DroidSansMono" ]; })
 
   (nerdfonts.override { fonts = [ "FiraCode" "DroidSansMono" ]; })
 
];
 
];
Line 47: Line 48:
  
 
=== Set multiple fonts for different languages ===
 
=== Set multiple fonts for different languages ===
If you want to use other languages alongside English, you may want to set appropriate fonts for each language in your whole OS. For example, a Persian speaker might want to use the [https://rastikerdar.github.io/vazir-font/ Vazir] font for Persian texts and the [https://design.ubuntu.com/font/ Ubuntu] font for English texts. Just put these lines into your <code>configuration.nix</code>:
+
If you want to use other languages alongside English, you may want to set appropriate fonts for each language in your whole OS. For example, a Persian speaker might want to use the [https://rastikerdar.github.io/vazirmatn/ Vazirmatn] font for Persian texts and the [https://design.ubuntu.com/font/ Ubuntu] font for English texts. Just put these lines into your <code>configuration.nix</code>:
  
 
<syntaxhighlight lang="nix">
 
<syntaxhighlight lang="nix">
 
  #----=[ Fonts ]=----#
 
  #----=[ Fonts ]=----#
 
fonts = {
 
fonts = {
   enableDefaultFonts = true;
+
   enableDefaultPackages = true;
   fonts = with pkgs; [  
+
   packages = with pkgs; [  
 
     ubuntu_font_family
 
     ubuntu_font_family
 
+
     # Persian Font
     # Persian Fonts
 
 
     vazir-fonts
 
     vazir-fonts
    vazir-code-font
 
 
   ];
 
   ];
  
 
   fontconfig = {
 
   fontconfig = {
 
     defaultFonts = {
 
     defaultFonts = {
       serif = [ "Vazir" "Ubuntu" ];
+
       serif = [ "Vazirmatn" "Ubuntu" ];
       sansSerif = [ "Vazir" "Ubuntu" ];
+
       sansSerif = [ "Vazirmatn" "Ubuntu" ];
       monospace = [ "Vazir Code" "Ubuntu" ];
+
       monospace = [ "Ubuntu" ];
 
     };
 
     };
 
   };
 
   };
Line 80: Line 79:
 
<syntaxhighlight lang="nix">
 
<syntaxhighlight lang="nix">
 
fonts = {
 
fonts = {
   fonts = with pkgs; [ gyre-fonts ];
+
   packages = with pkgs; [ gyre-fonts ];
 
   fontconfig = {
 
   fontconfig = {
 
     localConf = ''
 
     localConf = ''
Line 98: Line 97:
 
* https://wiki.archlinux.org/index.php/Font_configuration
 
* https://wiki.archlinux.org/index.php/Font_configuration
 
* https://wiki.archlinux.org/index.php/Font_configuration/Examples
 
* https://wiki.archlinux.org/index.php/Font_configuration/Examples
 
  
 
== Troubleshooting ==
 
== Troubleshooting ==
Line 129: Line 127:
 
$ ln -s /run/current-system/sw/share/X11/fonts ~/.local/share/fonts
 
$ ln -s /run/current-system/sw/share/X11/fonts ~/.local/share/fonts
 
</syntaxhighlight>
 
</syntaxhighlight>
Finally, either using the Flatpak CLI or Flatseal make <code>All system files</code> or equivalently <code>filesystem=host</code> available to your application.
+
Now you have two options.
 +
 
 +
==== Option 1: allow the Flatpaks to access the font folder and <code>/nix/store</code> ====
 +
By using the Flatpak CLI or the Flatseal Flatpak make the following directory available to all Flatpaks <code>$HOME/.local/share/fonts</code> and <code>$HOME/.icons</code> the appropriate commands for this are:
 +
 
 +
<syntaxhighlight lang="bash">
 +
flatpak --user override --filesystem=$HOME/.local/share/fonts:ro
 +
flatpak --user override --filesystem=$HOME/.icons:ro
 +
</syntaxhighlight>
 +
 
 +
And, because <code>~/.local/share/fonts</code> is linked to <code>/run/current-system/sw/share/X11/fonts</code>, which in turn is linked to content in <code>/nix/store</code>.
 +
You need to grant flatpak applications access to the <code>/nix/store</code> directory,
 +
so that they can load fonts correctly.
 +
 
 +
<syntaxhighlight lang="bash">
 +
flatpak --user override --filesystem=/nix/store:ro
 +
</syntaxhighlight>
 +
 
 +
==== Option 2: allow the Flatpaks to access the WHOLE filesystem ====
 +
 
 +
Allow them access the WHOLE filesystem of yours: <code>All system files</code> in Flatseal or equivalently <code>filesystem=host</code> available to your application, the command for this is:
 +
 
 +
<syntaxhighlight lang="bash">
 +
flatpak --user override --filesystem=host
 +
</syntaxhighlight>
 +
 
 +
It is important to keep in mind that some flatpak apps may refuse to launch if given certain permissions, such as the Steam flatpak.
 +
 
 +
=== Using bindfs for font support ===
 +
 
 +
<syntaxhighlight lang="nix">
 +
  system.fsPackages = [ pkgs.bindfs ];
 +
  fileSystems = let
 +
    mkRoSymBind = path: {
 +
      device = path;
 +
      fsType = "fuse.bindfs";
 +
      options = [ "ro" "resolve-symlinks" "x-gvfs-hide" ];
 +
    };
 +
    aggregatedIcons = pkgs.buildEnv {
 +
      name = "system-icons";
 +
      paths = with pkgs; [
 +
        #libsForQt5.breeze-qt5  # for plasma
 +
        gnome.gnome-themes-extra
 +
      ];
 +
      pathsToLink = [ "/share/icons" ];
 +
    };
 +
    aggregatedFonts = pkgs.buildEnv {
 +
      name = "system-fonts";
 +
      paths = config.fonts.packages;
 +
      pathsToLink = [ "/share/fonts" ];
 +
    };
 +
  in {
 +
    "/usr/share/icons" = mkRoSymBind "${aggregatedIcons}/share/icons";
 +
    "/usr/local/share/fonts" = mkRoSymBind "${aggregatedFonts}/share/fonts";
 +
  };
 +
 
 +
  fonts = {
 +
    fontDir.enable = true;
 +
    packages = with pkgs; [
 +
      noto-fonts
 +
      noto-fonts-emoji
 +
      noto-fonts-cjk
 +
    ];
 +
  };
 +
</syntaxhighlight>
  
 
<hr />
 
<hr />
 
[[Category:Configuration]]
 
[[Category:Configuration]]

Latest revision as of 07:07, 23 April 2024

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.packages NixOS options list instead.

Note: for 23.05 or older, fonts.packages is called fonts.fonts instead.

For example:

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

Be aware that sometimes font names and packages name differ and there is no universal convention in NixOS. See Guidelines for font packaging

Installing specific fonts from nerdfonts

The nerdfonts package, which contains all fonts from the nerdfonts repository is quite large and contains a large number of fonts which take some time to install. If you only need a selection of fonts from the package, you can overwrite the font selection on Stable 20.09 like so:

fonts.packages = with pkgs; [
  (nerdfonts.override { fonts = [ "FiraCode" "DroidSansMono" ]; })
];

This will cause NixOS to download only the Fira Code 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 want to use other languages alongside English, you may want to set appropriate fonts for each language in your whole OS. For example, a Persian speaker might want to use the Vazirmatn font for Persian texts and the Ubuntu font for English texts. Just put these lines into your configuration.nix:

 #----=[ Fonts ]=----#
fonts = {
  enableDefaultPackages = true;
  packages = with pkgs; [ 
    ubuntu_font_family
    # Persian Font
    vazir-fonts
  ];

  fontconfig = {
    defaultFonts = {
      serif = [ "Vazirmatn" "Ubuntu" ];
      sansSerif = [ "Vazirmatn" "Ubuntu" ];
      monospace = [ "Ubuntu" ];
    };
  };
};

Use custom font substitutions

Sometimes, documents may appear to have bad kerning or hard-to-read letter spacing, due to a bad substitution. For example, Okular may show in the Document Properties dialog that it has substituted DejaVu Sans Mono (a sans-serif font) in place of "NewCenturySchlbk". fc-match NewCenturySchlbk would display similiar info.

Adding this to your /etc/nixos/configuration.nix should prompt it to use the nicer serif *Schola* font instead:

fonts = {
  packages = with pkgs; [ gyre-fonts ];
  fontconfig = {
    localConf = ''
      <!-- use a less horrible font substition for pdfs such as https://www.bkent.net/Doc/mdarchiv.pdf -->
      <match target="pattern">
        <test qual="any" name="family"><string>NewCenturySchlbk</string></test>
        <edit name="family" mode="assign" binding="same"><string>TeX Gyre Schola</string></edit>
      </match>
    '';
  };
};

For more information and examples on the xml configuration language:

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

Note that you may need to set fonts.fontDir.enable = true; for that X11/fonts directory to exist.

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

The ~/.fonts directory is being deprecated upstream[1]. It already doesn't work in NixOS.

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

Flatpak applications can't find system fonts

Enable fontDir in your NixOS configuration:

fonts.fontDir.enable = true;

Then, create a symlink in XDG_DATA_HOME/fonts pointing to /run/current-system/sw/share/X11/fonts, e. g.

$ ln -s /run/current-system/sw/share/X11/fonts ~/.local/share/fonts

Now you have two options.

Option 1: allow the Flatpaks to access the font folder and /nix/store

By using the Flatpak CLI or the Flatseal Flatpak make the following directory available to all Flatpaks $HOME/.local/share/fonts and $HOME/.icons the appropriate commands for this are:

flatpak --user override --filesystem=$HOME/.local/share/fonts:ro
flatpak --user override --filesystem=$HOME/.icons:ro

And, because ~/.local/share/fonts is linked to /run/current-system/sw/share/X11/fonts, which in turn is linked to content in /nix/store. You need to grant flatpak applications access to the /nix/store directory, so that they can load fonts correctly.

flatpak --user override --filesystem=/nix/store:ro

Option 2: allow the Flatpaks to access the WHOLE filesystem

Allow them access the WHOLE filesystem of yours: All system files in Flatseal or equivalently filesystem=host available to your application, the command for this is:

flatpak --user override --filesystem=host

It is important to keep in mind that some flatpak apps may refuse to launch if given certain permissions, such as the Steam flatpak.

Using bindfs for font support

  system.fsPackages = [ pkgs.bindfs ];
  fileSystems = let
    mkRoSymBind = path: {
      device = path;
      fsType = "fuse.bindfs";
      options = [ "ro" "resolve-symlinks" "x-gvfs-hide" ];
    };
    aggregatedIcons = pkgs.buildEnv {
      name = "system-icons";
      paths = with pkgs; [
        #libsForQt5.breeze-qt5  # for plasma
        gnome.gnome-themes-extra
      ];
      pathsToLink = [ "/share/icons" ];
    };
    aggregatedFonts = pkgs.buildEnv {
      name = "system-fonts";
      paths = config.fonts.packages;
      pathsToLink = [ "/share/fonts" ];
    };
  in {
    "/usr/share/icons" = mkRoSymBind "${aggregatedIcons}/share/icons";
    "/usr/local/share/fonts" = mkRoSymBind "${aggregatedFonts}/share/fonts";
  };

  fonts = {
    fontDir.enable = true;
    packages = with pkgs; [
      noto-fonts
      noto-fonts-emoji
      noto-fonts-cjk
    ];
  };