Fonts: Difference between revisions

Nerd fonts: update to 25.05.
Phobos (talk | contribs)
m Updated Formating
 
(7 intermediate revisions by 7 users not shown)
Line 3: Line 3:
== Installing fonts on NixOS ==
== Installing fonts on NixOS ==


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 favorites 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?channel=unstable&show=fonts.packages&from=0&size=50&sort=relevance&type=packages&query=fonts.packages fonts.packages]</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.


''For example:''  
''For example:''


<syntaxhighlight lang="nix">
{{File|3=fonts.packages = with pkgs; [
fonts.packages = with pkgs; [
   noto-fonts
   noto-fonts
   noto-fonts-cjk-sans
   noto-fonts-cjk-sans
   noto-fonts-emoji
   noto-fonts-color-emoji
   liberation_ttf
   liberation_ttf
   fira-code
   fira-code
Line 20: Line 19:
   dina-font
   dina-font
   proggyfonts
   proggyfonts
];
];|name=/etc/nixos/configuration.nix|lang=nix}}{{Note|Be aware that sometimes font names and packages name differ and there is no universal convention in NixOS. </br>See: [https://discourse.nixos.org/t/guidelines-on-packaging-fonts/7683/2&#124; Guidelines for font packaging]}}
</syntaxhighlight>
 
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 ===
=== 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.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.
* <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.
=== Using fonts from TexLive ===
You can make use of all TeX/LaTeX fonts from CTAN and [[TexLive]] by passing
the <code>fonts</code> attribute of your TexLive package to <code>fonts.package</code>:
{{File|3={ pkgs, ... }:
let
  mytex =
    pkgs.texliveConTeXt.withPackages
      (ps: with ps; [
        fandol
        libertinus-fonts
      ]);
in {
  fonts.packages = builtins.attrValues {
    inherit (pkgs)
      dejavu_fonts
      noto-fonts-cjk-serif
      noto-fonts-cjk-sans
      julia-mono;
  } ++ [ mytex.fonts ];
}|name=/etc/nixos/configuration.nix|lang=nix}}


=== Installing <code>nerdfonts</code> ===
=== Installing <code>nerdfonts</code> ===
Line 33: Line 51:
Individual Nerd Fonts can be installed like so:
Individual Nerd Fonts can be installed like so:


<syntaxhighlight lang="nix">
{{File|3=fonts.packages = with pkgs; [
fonts.packages = with pkgs; [
   nerd-fonts.fira-code
   nerd-fonts.fira-code
   nerd-fonts.droid-sans-mono
   nerd-fonts.droid-sans-mono
];
];|name=/etc/nixos/configuration.nix|lang=nix}}
 
 
 
The available Nerd Font subpackages can be listed by searching for {{nixos:package|nerd-fonts.*}} on the [[Searching packages|NixOS Package Search]] or by running the following command: <syntaxhighlight lang="console">
$ nix-instantiate --eval --expr "with (import <nixpkgs> {}); lib.attrNames (lib.filterAttrs (_: lib.isDerivation) nerd-fonts)"
</syntaxhighlight>
</syntaxhighlight>
The available Nerd Font subpackages can be listed by searching for {{nixos:package|nerd-fonts.*}} on the [[Searching packages|NixOS Package Search]] or by running the following command: <pre>nix-instantiate --eval --expr "builtins.filter (font: font != \"override\" && font != \"overrideDerivation\" && font != \"recurseForDerivations\") (builtins.attrNames (import <nixpkgs> {}).nerd-fonts)"</pre>


==== Installing all <code>nerdfonts</code> ====
==== Installing all <code>nerdfonts</code> ====


Installing all fonts from the [https://www.nerdfonts.com/ Nerd Fonts repository] is as simple as adding all of the individual packages to the NixOS configuration. The following line will do exactly that, by searching for all derivations under the <code>nerd-font</code> attribute:{{file|/etc/nixos/configuration.nix|nix|<nowiki>
Installing all fonts from the [https://www.nerdfonts.com/ Nerd Fonts repository] is as simple as adding all of the individual packages to the NixOS configuration. The following line will do exactly that, by searching for all derivations under the <code>nerd-font</code> attribute:{{file|||<nowiki>
{  
{  
   fonts.packages = builtins.filter lib.attrsets.isDerivation (builtins.attrValues pkgs.nerd-fonts)
   fonts.packages = builtins.filter lib.attrsets.isDerivation (builtins.attrValues pkgs.nerd-fonts);
}
}
</nowiki>}}
</nowiki>|name=/etc/nixos/configuration.nix|lang=nix}}
 
=== Patching nerdfonts into fonts ===
Not all fonts have Nerd Fonts variants, thankfully you can easily patch them in yourself.
{{File|3=(pkgs.scientifica.overrideAttrs (o: {
  nativeBuildInputs = [ pkgs.nerd-font-patcher ];
  postInstall = ''
    mkdir -p $out/share/fonts/truetype/{scientifica,scientifica-nerd}
    mv $out/share/fonts/truetype/*.ttf $out/share/fonts/truetype/scientifica/
    for f in $out/share/fonts/truetype/scientifica/*.ttf; do
      nerd-font-patcher --complete --outputdir $out/share/fonts/truetype/scientifica-nerd/ $f
    done
  '';
}))|name=/etc/nixos/configuration.nix|lang=nix}}


=== Let Fontconfig know the fonts within your Nix profile ===
=== Let Fontconfig know the fonts within your Nix profile ===
Line 85: Line 118:
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>:
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">
{{File|3=#----=[ Fonts ]=----#
#----=[ Fonts ]=----#
fonts = {
fonts = {
   enableDefaultPackages = true;
   enableDefaultPackages = true;
Line 103: Line 135:
     };
     };
   };
   };
};
};|name=/etc/nixos/configuration.nix|lang=nix}}
</syntaxhighlight>


NB:
NB:
Line 118: Line 149:
Adding this to your <code>/etc/nixos/configuration.nix</code> should prompt it to use the more similar (and 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">
{{File|3=fonts = {
fonts = {
   packages = with pkgs; [ gyre-fonts ];
   packages = with pkgs; [ gyre-fonts ];
   fontconfig = {
   fontconfig = {
Line 130: Line 160:
     '';
     '';
   };
   };
};
};|name=/etc/nixos/configuration.nix|lang=nix}}
</syntaxhighlight>


For more information and examples on the xml configuration language:
For more information and examples on the xml configuration language:
Line 164: Line 193:


To expose available fonts under <code>/run/current-system/sw/share/X11/fonts</code>, enable <code>fontDir</code> in your NixOS configuration.
To expose available fonts under <code>/run/current-system/sw/share/X11/fonts</code>, enable <code>fontDir</code> in your NixOS configuration.
<syntaxhighlight lang="nix">
 
fonts.fontDir.enable = true;
{{File|3=fonts.fontDir.enable = true;|name=/etc/nixos/configuration.nix|lang=nix}}
</syntaxhighlight>You will then need to link/copy this folder to one of the Flatpak-supported locations - see below.
 
You will then need to link/copy this folder to one of the Flatpak-supported locations - see below.


==== Solution 1: Copy fonts to <code>$HOME/.local/share/fonts</code> ====
==== Solution 1: Copy 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>. You will need to repeat this step whenever the fonts change.<syntaxhighlight lang="bash">
Create fonts directory <code>$HOME/.local/share/fonts</code> and copy system fonts with option <code>-L, --dereference</code>. You will need to repeat this step whenever the fonts change.<syntaxhighlight lang="console">
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>Note: There is no need to grant flatpak applications access to <code>$HOME/.local/share/fonts</code>.  
</syntaxhighlight>Note: There is no need to grant flatpak applications access to <code>$HOME/.local/share/fonts</code>.  


Line 257: Line 287:
=== Noto Color Emoji doesn't render on Firefox ===
=== Noto Color Emoji doesn't render on Firefox ===


Enable <code>useEmbeddedBitmaps</code> in your NixOs configuration.
Enable <code>useEmbeddedBitmaps</code> in your NixOS configuration.
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
fonts.fontconfig.useEmbeddedBitmaps = true;
fonts.fontconfig.useEmbeddedBitmaps = true;