Fonts: Difference between revisions

Fix solutions for tufo fonts problem in flatpak applications
m improved guidance on where to determine font names for nerdfonts
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
NixOS handles fonts like it handles many different parts of the system: they are not in an environment unless explicitly marked to be part of it. This guide covers the installation, configuration and troubleshooting of fonts.
== Installing fonts on NixOS ==
== Installing fonts on NixOS ==


Line 7: Line 9:
'''Note''': for 23.05 or older, <code>fonts.packages</code> is called <code>fonts.fonts</code> 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">
Line 24: Line 26:


Be aware that sometimes font names and packages name differ and there is no universal convention in NixOS. See [https://discourse.nixos.org/t/guidelines-on-packaging-fonts/7683/2 Guidelines for font packaging]
Be aware that sometimes font names and packages name differ and there is no universal convention in NixOS. See [https://discourse.nixos.org/t/guidelines-on-packaging-fonts/7683/2 Guidelines for font packaging]
=== Shorthands for fonts ===
* <code>fonts.enableDefaultPackages</code>: when set to <code>true</code>, causes some "basic" fonts to be installed for reasonable Unicode coverage. Set to <code>true</code> if you are unsure about what languages you might end up reading.
* <code>fonts.enableGhostscriptFonts</code>: affects the <code>ghostscript</code> package. Ghostscript packages some URW fonts for the standard PostScript typefaces. If <code>true</code>, these fonts will be visible to GUI applications. You could set it to <code>true</code> if you want these fonts, but <code>gyre-fonts</code> (part of <code>fonts.enableDefaultPackages</code>) might be higher-quality depending on your judgement.
=== Installing specific fonts from nerdfonts ===
=== Installing specific fonts from nerdfonts ===
The <code>nerdfonts</code> package, which contains all fonts from the [https://www.nerdfonts.com/ 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:
The <code>nerdfonts</code> package, which contains all fonts from the [https://www.nerdfonts.com/ 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:
Line 33: Line 40:
</syntaxhighlight>
</syntaxhighlight>


This will cause NixOS to download only the [https://github.com/ryanoasis/nerd-fonts/tree/master/patched-fonts/FiraCode Fira Code] and [https://github.com/ryanoasis/nerd-fonts/tree/master/patched-fonts/DroidSansMono Droid Sans Mono] fonts from [https://github.com/ryanoasis/nerd-fonts nerd-fonts] instead of the whole package.
This will cause NixOS to download only the [https://github.com/ryanoasis/nerd-fonts/tree/master/patched-fonts/FiraCode Fira Code] and [https://github.com/ryanoasis/nerd-fonts/tree/master/patched-fonts/DroidSansMono Droid Sans Mono] fonts from [https://github.com/ryanoasis/nerd-fonts nerd-fonts] instead of the whole package. The relevant names can be found by looking at the file name for your relevant font on the [https://github.com/ryanoasis/nerd-fonts/releases official release page] assets.


=== Imperative installation of user fonts ===
=== Imperative installation of user fonts ===
This is useful for quick font experiments.
This is useful for quick font experiments.


Example: Install <code>SourceCodePro-Regular</code>.
''Example'': Install <code>SourceCodePro-Regular</code>.
<syntaxhighlight lang="bash">font=$(nix-build --no-out-link '<nixpkgs>' -A source-code-pro)/share/fonts/opentype/SourceCodePro-Regular.otf
<syntaxhighlight lang="bash">font=$(nix-build --no-out-link '<nixpkgs>' -A source-code-pro)/share/fonts/opentype/SourceCodePro-Regular.otf
cp $font ~/.local/share/fonts
cp $font ~/.local/share/fonts
Line 46: Line 53:
</syntaxhighlight>
</syntaxhighlight>


== Configuring fonts ==
The nixos key [https://search.nixos.org/options?channel=unstable&from=0&size=50&sort=relevance&type=packages&query=fonts.fontconfig <code>fonts.fontconfig</code>] (click to see the full list!) handles the fontconfig options. Some options are nicely wrapped in nix; there's always <code>localConf</code> to go straight to the XML.


=== 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/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>:
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, but [https://design.ubuntu.com/font/ Ubuntu] and Liberation Serif fonts for English texts. Just put these lines into your <code>configuration.nix</code>:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
#----=[ Fonts ]=----#
#----=[ Fonts ]=----#
fonts = {
fonts = {
   enableDefaultPackages = true;
   enableDefaultPackages = true;
   packages = with pkgs; [  
   packages = with pkgs; [  
     ubuntu_font_family
     ubuntu_font_family
    liberation_ttf
     # Persian Font
     # Persian Font
     vazir-fonts
     vazir-fonts
Line 62: Line 73:
   fontconfig = {
   fontconfig = {
     defaultFonts = {
     defaultFonts = {
       serif = [ "Vazirmatn" "Ubuntu" ];
       serif = [ "Liberation Serif" "Vazirmatn" ];
       sansSerif = [ "Vazirmatn" "Ubuntu" ];
       sansSerif = [ "Ubuntu" "Vazirmatn" ];
       monospace = [ "Ubuntu" ];
       monospace = [ "Ubuntu Mono" ];
     };
     };
   };
   };
};
};
</syntaxhighlight>
</syntaxhighlight>
NB:
* This actually just sets the font fallback order so that fontconfig tries using the English font first, then falls back to another font if the character set is not covered. You ''usually'' want to write the English font ''before'' the other-language font, because the other-language font might cover Latin characters too, preventing the English font from showing up.
* <code>defaultFonts</code> translates to <code>&lt;prefer&gt;</code> in the actual fontconfig file. See https://github.com/NixOS/nixpkgs/blob/nixos-23.11/nixos/modules/config/fonts/fontconfig.nix for how NixOS does it, and the links below for how fontconfig interpret it.
* Vazirmatn is actually a "sans-serif" font; using it for <code>serif</code> is not a good visual match. You might need not one, but two (or if you count monospace, three!) font packages for a language.


=== Use custom font substitutions ===
=== Use custom font substitutions ===
Line 75: Line 91:
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". <code>fc-match NewCenturySchlbk</code> would display similiar info.
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". <code>fc-match NewCenturySchlbk</code> would display similiar info.


Adding this to your <code>/etc/nixos/configuration.nix</code> should prompt it to use the nicer serif *Schola* font instead:
Adding this to your <code>/etc/nixos/configuration.nix</code> should prompt it to use the more similar (and nicer) serif ''Schola'' font instead:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 94: Line 110:
For more information and examples on the xml configuration language:
For more information and examples on the xml configuration language:


* https://linux.die.net/man/5/fonts-conf
* https://www.mankier.com/5/fonts-conf
* 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
For a list of suitable replacement fonts:
* https://wiki.archlinux.org/title/Metric-compatible_fonts


== Troubleshooting ==
== Troubleshooting ==
Line 124: Line 143:
</syntaxhighlight>
</syntaxhighlight>


==== Best Solution: Copy all your fonts to $HOME and allow flatpak applications to access ====
==== Best Solution: Just copy necessary fonts to <code>$HOME/.local/share/fonts</code> ====
Create fonts directory <code>$HOME/.local/share/fonts</code> and copy system fonts with option <code>-L, --dereference</code>  .<syntaxhighlight lang="bash">
Create fonts directory <code>$HOME/.local/share/fonts</code> and copy system fonts with option <code>-L, --dereference</code>  .<syntaxhighlight lang="bash">
mkdir $HOME/.local/share/fonts && cp -L /run/current-system/sw/share/X11/fonts/* $HOME/.local/share/fonts/
mkdir $HOME/.local/share/fonts && cp -L /run/current-system/sw/share/X11/fonts/* $HOME/.local/share/fonts/
</syntaxhighlight>Grant flatpak applications access to your fonts directory.<syntaxhighlight lang="bash">
</syntaxhighlight>Note: There is '''no need''' to grant flatpak applications access to <code>$HOME/.local/share/fonts</code>.
flatpak --user override --filesystem=$HOME/.local/share/fonts:ro
 
</syntaxhighlight>
Instead, if you do that, some applications (for example, steam) won't work.<blockquote>Internals: How it works?
 
Flatpak applications run in sandboxes. When you start a flatpak application, flatpak builds a rootfs for it with bubblewrap.
 
With <code>findmnt --task {PID of flatpak app}</code> , you can explore the details of its rootfs.
 
By default, flatpak mounts <code>$HOME/.local/share/fonts</code> to <code>/run/host/user-fonts</code> in rootfs of an flatpak application.<syntaxhighlight lang="json">
{
  "target": "/run/host/user-fonts",
  "source": "/dev/disk/by-uuid/b2e1e6b5-738b-410b-b736-6d5c3dbbe31f[/home/username/.local/share/fonts]",
  "fstype": "ext4",
  "options": "ro,nosuid,nodev,relatime"
}
</syntaxhighlight>Then flatpak application can read fonts from that to display contents correctly.</blockquote>


==== Possible Method: Create symlink to system fonts at  <code>$HOME/.local/share/fonts</code> ====
==== Another Method: Create symlink to system fonts at  <code>$HOME/.local/share/fonts</code> ====
<blockquote>'''Note:''' this method doesn't work for some flatpak applications (for example, steam)!
<blockquote>'''Note:''' this method doesn't work for some flatpak applications (for example, steam)!


Line 208: Line 240:
<hr />
<hr />
[[Category:Configuration]]
[[Category:Configuration]]
[[Category:Desktop]]