Fonts: Difference between revisions

Previous hack does not work in case there are same font subpaths in multiple font packages, which seem to be occurring more likely in NixOS 25.11. This solution instead partly replicates fontDir logic and creates flat dir with all fonts symlinked. related discussion happened here: https://discourse.nixos.org/t/25-11-upgrade-error-two-given-paths-contain-a-conflicting-subpath/72972/14
m Fixed broken link.
 
(7 intermediate revisions by 6 users not shown)
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. </br>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 Guidelines for font packaging]}}


=== Shorthands for fonts ===
=== Shorthands for fonts ===
Line 67: Line 67:
{  
{  
   fonts.packages = builtins.filter lib.attrsets.isDerivation (builtins.attrValues pkgs.nerd-fonts);
   fonts.packages = builtins.filter lib.attrsets.isDerivation (builtins.attrValues pkgs.nerd-fonts);
}
# 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}}
</nowiki>|name=/etc/nixos/configuration.nix|lang=nix}}
Line 110: 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 122: Line 160:
   enableDefaultPackages = true;
   enableDefaultPackages = true;
   packages = with pkgs; [  
   packages = with pkgs; [  
     ubuntu_font_family
     ubuntu-classic
     liberation_ttf
     liberation_ttf
     # Persian Font
     # Persian Font
Line 310: 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 ===