Fonts: Difference between revisions

Phobos (talk | contribs)
mNo edit summary
Zetsu (talk | contribs)
m fix link
 
(7 intermediate revisions by 5 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.
Line 19: 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. See [https://discourse.nixos.org/t/guidelines-on-packaging-fonts/7683/2&#124; Guidelines for font packaging]}}
];|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]}}


=== Shorthands for fonts ===
=== Shorthands for fonts ===
Line 28: Line 28:
You can make use of all TeX/LaTeX fonts from CTAN and [[TexLive]] by passing
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>:
the <code>fonts</code> attribute of your TexLive package to <code>fonts.package</code>:
<syntaxhighlight lang="nix">
 
{ pkgs, ... }:
{{File|3={ pkgs, ... }:
let
let
   mytex =
   mytex =
Line 45: Line 45:
       julia-mono;
       julia-mono;
   } ++ [ mytex.fonts ];
   } ++ [ mytex.fonts ];
}
}|name=/etc/nixos/configuration.nix|lang=nix}}
 
</syntaxhighlight>


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


<syntaxhighlight lang="nix">fonts.packages = with pkgs; [
{{File|3=fonts.packages = with pkgs; [
   nerd-fonts.fira-code
   nerd-fonts.fira-code
   nerd-fonts.droid-sans-mono
   nerd-fonts.droid-sans-mono
];</syntaxhighlight>
];|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: <pre>nix-instantiate --eval --expr "with (import <nixpkgs> {}); lib.attrNames (lib.filterAttrs (_: lib.isDerivation) nerd-fonts)"</pre>
 
 
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>


==== 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>}}
 
# or if you already have fonts.packages set
 
{
  fonts.packages = [
    # ... some fonts
  ]
  ++ builtins.filter lib.attrsets.isDerivation (builtins.attrValues pkgs.nerd-fonts);
}
</nowiki>|name=/etc/nixos/configuration.nix|lang=nix}}


=== Patching nerdfonts into fonts ===
=== Patching nerdfonts into fonts ===
Line 79: Line 90:
     done
     done
   '';
   '';
}))|name=fonts.nix|lang=nix}}
}))|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 108: Line 119:
fc-list -v | grep -i source
fc-list -v | grep -i source
</syntaxhighlight>
</syntaxhighlight>
=== Install fonts in nix-shells ===
<code>fonts</code> is not available as set-valued option in <code>mkshell</code> (gives you an error because it tries to coerce an attribute set into a string). Instead, insert the following:<ref>https://programming.dev/post/32484220</ref>
<syntaxhighlight lang="nix">
{pkgs ? import <nixpkgs> {} }:
let
  fontsConf = pkgs.makeFontsConf {
    fontDirectories = [
      # your needed fonts here, e.g.:
      pkgs.font-awesome
      pkgs.atkinson-hyperlegible-next
    ];
  };
in
  pkgs.mkShell {
    packages = with pkgs; [
      # your font-dependent packages, e.g.:
      typst
    ];
    shellHook = ''
      export FONTCONFIG_FILE="${fontsConf}"
    '';
  }
</syntaxhighlight>
Then <code>typst fonts</code> finds the installed fonts in the nix-shell.


== Configuring fonts ==
== Configuring fonts ==
Line 116: Line 156:
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 134: Line 173:
     };
     };
   };
   };
};
};|name=/etc/nixos/configuration.nix|lang=nix}}
</syntaxhighlight>


NB:
NB:
Line 149: Line 187:
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 161: Line 198:
     '';
     '';
   };
   };
};
};|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 195: Line 231:


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 258: Line 295:
Alternatively, you can expose relevant packages directly under <code>/usr/share/...</code> paths. This will also enable Flatpak to use a custom cursor theme if you have one. This solution doesn't require <code>fonts.fontDir.enable</code> to be enabled.<syntaxhighlight lang="nix">
Alternatively, you can expose relevant packages directly under <code>/usr/share/...</code> paths. This will also enable Flatpak to use a custom cursor theme if you have one. This solution doesn't require <code>fonts.fontDir.enable</code> to be enabled.<syntaxhighlight lang="nix">
   system.fsPackages = [ pkgs.bindfs ];
   system.fsPackages = [ pkgs.bindfs ];
   fileSystems = let
   fileSystems = let
     mkRoSymBind = path: {
     mkRoSymBind = path: {
Line 265: Line 301:
       options = [ "ro" "resolve-symlinks" "x-gvfs-hide" ];
       options = [ "ro" "resolve-symlinks" "x-gvfs-hide" ];
     };
     };
     aggregated = pkgs.buildEnv {
     fontsPkgs = config.fonts.packages ++ (with pkgs; [
        name = "system-fonts-and-icons";
        # Add your cursor themes and icon packages here
        paths = config.fonts.packages ++ (with pkgs; [
        bibata-cursors
          # Add your cursor themes and icon packages here
        gnome.gnome-themes-extra
          bibata-cursors
        # etc.
          gnome.gnome-themes-extra
      ]);
          # etc.
    x11Fonts = pkgs.runCommand "X11-fonts"
        ]);
      {
         pathsToLink = [ "/share/fonts" "/share/icons" ];
         preferLocalBuild = true;
        nativeBuildInputs = with pkgs; [
          gzip
          xorg.mkfontscale
          xorg.mkfontdir
        ];
      }
      (''
        mkdir -p "$out/share/fonts"
        font_regexp='.*\.\(ttf\|ttc\|otb\|otf\|pcf\|pfa\|pfb\|bdf\)\(\.gz\)?'
      ''
      + (builtins.concatStringsSep "\n" (builtins.map (pkg: ''
          find ${toString pkg} -regex "$font_regexp" \
            -exec ln -sf -t "$out/share/fonts" '{}' \;
        '') fontsPkgs
        ))
      + ''
        cd "$out/share/fonts"
        mkfontscale
        mkfontdir
        cat $(find ${pkgs.xorg.fontalias}/ -name fonts.alias) >fonts.alias
      '');
    aggregatedIcons = pkgs.buildEnv {
      name = "system-icons";
      paths = fontsPkgs;
      pathsToLink = [
        "/share/icons"
      ];
     };
     };
   in {
   in {
     "/usr/share/fonts" = mkRoSymBind "${aggregated}/share/fonts";
     "/usr/share/icons" = mkRoSymBind (aggregatedIcons + "/share/icons");
     "/usr/share/icons" = mkRoSymBind "${aggregated}/share/icons";
     "/usr/share/fonts" = mkRoSymBind (x11Fonts + "/share/fonts");
   };
   };


Line 285: Line 348:
     noto-fonts-cjk
     noto-fonts-cjk
   ];
   ];
</syntaxhighlight>
</syntaxhighlight>Note that font cache inside flatpak container may not be recreated after changes to fonts in <code>/usr/share/fonts</code>, because font cache seem to be relying on file timestamps that are missing in <code>/nix/store</code>.
 
You can make sure that font directory is bind-mounted properly inside flatpak container by running <code>flatpak enter <instance> findmnt  | grep /run/host/fonts</code>, or by running <code>flatpak enter <instance> ls -alh /run/host/fonts</code> and compare it to <code>ls -alh /usr/share/fonts</code>.
 
If everything is mounted properly, but you still do not see fonts in flatpak app - force font cache recreation inside flatpak container: <code>flatpak run --command=fc-cache <application id> -f -v</code>
 
=== Noto Color Emoji doesn't render on Firefox ===
=== Noto Color Emoji doesn't render on Firefox ===