Fonts: Difference between revisions

m concatenate the mountOptions into a single variable with explanation for "resolve-symlinks"
Instead of being flatpak-specific, also mention distrobox and appimage for the bindmounting fix
Line 295: Line 295:
The new preferred location is in <code>$XDG_DATA_HOME/fonts</code>, which for most users will resolve to <code>~/.local/share/fonts</code></translate><ref>https://lists.freedesktop.org/archives/fontconfig/2014-July/005270.html</ref>
The new preferred location is in <code>$XDG_DATA_HOME/fonts</code>, which for most users will resolve to <code>~/.local/share/fonts</code></translate><ref>https://lists.freedesktop.org/archives/fontconfig/2014-July/005270.html</ref>


<translate>=== Flatpak applications can't find system fonts === <!--T:49--></translate>
<translate>
=== Flatpak, Distrobox, Appimage and other non-Nix applications can't find system fonts/icons/themes ===
Flatpak, Distrobox, Appimage and alot of other non-Nix applications sometimes have hardcoded FHS path assumptions for locating fonts, icons and themes. These applications usually expect fonts, icons and themes to be available under <code>/usr/share/fonts</code>, <code>/usr/share/icons</code> and <code>/usr/share/themes</code>, respectively.
 
For these applications, instead of using a complicated <code>fhsEnv</code> solution, users can choose to simply bindmount the directories from <code>/run/current-system/sw/share</code> to their respective locations inside <code>/usr/share</code>.
 
Note that the following fix uses <code>bindfs</code> instead of normal bindmounting or symlinking. This is because of edgecases like [https://github.com/ONLYOFFICE/DocumentServer/issues/1859 OnlyOffice] that will not follow symlinks while evaluating resources under <code>/usr/share</code>. Thus, the <code>[https://bindfs.org/docs/bindfs.1.html#sect10 resolve-symlink]</code> functionality of <code>bindfs</code> becomes crucial for covering even the most niche corner cases.<syntaxhighlight lang="nixos" line="1">
# misc-usr-fix.nix
{ pkgs, ... }:
 
let
  # Mount options for the bind mounts
  mountOptions = [
    "ro"
    "x-gvfs-hide"
    # Resolves symlinks as if they were real files
    # Needed for things like OnlyOffice
    "resolve-symlinks"
  ];
in
{
  # Bind mounts fonts, icons and themes from
  # The /run/current-system/sw/share/* paths
  # to their /usr/share/* equivalents
 
  # Expose all fonts
  # under /run/current-system/sw/share/X11/fonts
  fonts.fontDir.enable = true;
 
  # Flatpak: Bind mounts /usr/share/* directories
  system.fsPackages = [ pkgs.bindfs ];
 
  # Fonts
  fileSystems."/usr/share/fonts" = {
    device = "/run/current-system/sw/share/X11/fonts";
    fsType = "fuse.bindfs";
    options = mountOptions;
  };


<translate><!--T:50--> To expose available fonts under <code>/run/current-system/sw/share/X11/fonts</code>, enable <code>fontDir</code> in your NixOS configuration.</translate>
  # Icons
  fileSystems."/usr/share/icons" = {
    device = "/run/current-system/sw/share/icons";
    fsType = "fuse.bindfs";
    options = mountOptions;
  };


{{File|3=fonts.fontDir.enable = true;|name=/etc/nixos/configuration.nix|lang=nix}}
  # Themes
  fileSystems."/usr/share/themes" = {
    device = "/run/current-system/sw/share/themes";
    fsType = "fuse.bindfs";
    options = mountOptions;
  };
}


<translate><!--T:51--> You will then need to link/copy this folder to one of the Flatpak-supported locations - see below.</translate>
</syntaxhighlight>


<translate>==== Solution 1: Copy fonts to <code>$HOME/.local/share/fonts</code> ==== <!--T:52--></translate>
==== Alternative fixes for Flatpak apps ====
This section documents alternative fixes for Flatpak apps.
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>
 
===== Solution 1: Copy fonts to <code>$HOME/.local/share/fonts</code> =====<!--T:52-->
</translate>
<translate><!--T:53-->
<translate><!--T:53-->
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">
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">
Line 326: Line 383:
</translate>
</translate>


<translate>==== Solution 2: Symlink to system fonts at  <code>$HOME/.local/share/fonts</code> ==== <!--T:58--></translate>
<translate>
===== Solution 2: Symlink to system fonts at  <code>$HOME/.local/share/fonts</code> =====<!--T:58-->
</translate>
<translate><!--T:59--> <blockquote>'''Note:''' this method doesn't work for some flatpak applications (for example, steam)! </translate>
<translate><!--T:59--> <blockquote>'''Note:''' this method doesn't work for some flatpak applications (for example, steam)! </translate>


Line 339: Line 398:


<translate>
<translate>
===== Option 1: Allow access to the fonts folder and <code>/nix/store</code> ===== <!--T:61-->
====== Option 1: Allow access to the fonts folder and <code>/nix/store</code> ======<!--T:61-->
</translate>
</translate>
<translate>
<translate>
Line 358: Line 417:
</syntaxhighlight>
</syntaxhighlight>


<translate>===== Option 2: Allow access to the WHOLE filesystem ===== <!--T:64--></translate>
<translate>
====== Option 2: Allow access to the WHOLE filesystem ======<!--T:64-->
</translate>
<translate>
<translate>
<!--T:65-->
<!--T:65-->
Line 371: Line 432:
<!--T:66-->
<!--T:66-->
It is important to keep in mind that some flatpak apps may refuse to launch if given certain permissions, such as the Steam flatpak.  
It is important to keep in mind that some flatpak apps may refuse to launch if given certain permissions, such as the Steam flatpak.  
</translate>
<translate>==== Solution 3: Configure bindfs for fonts/cursors/icons support ==== <!--T:67--></translate>
<translate><!--T:68-->
Alternatively, you can expose relevant host paths in <code>/run/current-system/sw/share/</code> directly under <code>/usr/share/...</code> paths. This will also enable Flatpak to use a custom cursor theme if you have one.<syntaxhighlight lang="nixos" line="1">
# flatpak-usr-fix.nix
{ pkgs, ... }:
let
  # Mount options for the bind mounts
  mountOptions = [
    "ro"
    "x-gvfs-hide"
    # Resolves symlinks as if they were real files
    # Needed for things like OnlyOffice
    "resolve-symlinks"
  ];
in
{
  # Bind mounts fonts, icons and themes from
  # The /run/current-system/sw/share/* paths
  # to their /usr/share/* equivalents
  # Expose all fonts
  # under /run/current-system/sw/share/X11/fonts
  fonts.fontDir.enable = true;
  # Flatpak: Bind mounts /usr/share/* directories
  system.fsPackages = [ pkgs.bindfs ];
 
  # Fonts
  fileSystems."/usr/share/fonts" = {
    device = "/run/current-system/sw/share/X11/fonts";
    fsType = "fuse.bindfs";
    options = mountOptions;
  };
  # Icons
  fileSystems."/usr/share/icons" = {
    device = "/run/current-system/sw/share/icons";
    fsType = "fuse.bindfs";
    options = mountOptions;
  };
  # Themes
  fileSystems."/usr/share/themes" = {
    device = "/run/current-system/sw/share/themes";
    fsType = "fuse.bindfs";
    options = mountOptions;
  };
}
</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>.
</translate>
<translate>
<!--T:70-->
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>.
</translate>
<translate>
<!--T:71-->
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>
</translate>
</translate>