Skip to content

texlive: fix texlive fonts for xetex#228196

Merged
veprbl merged 3 commits intoNixOS:masterfrom
apfelkuchen6:texlive-xetex-fonts
Apr 27, 2023
Merged

texlive: fix texlive fonts for xetex#228196
veprbl merged 3 commits intoNixOS:masterfrom
apfelkuchen6:texlive-xetex-fonts

Conversation

@apfelkuchen6
Copy link
Contributor

@apfelkuchen6 apfelkuchen6 commented Apr 25, 2023

Description of changes

XeTeX relies on the system's font managent tooling to locate fonts. This means that XeTeX was unable to find the fonts distributed with texlive as these weren't installed in paths searched by the system's font configuration.

This PR fixes this for linux by wrapping xetex such that the FONTCONFIG_FILE environment value is set to a suitable fontconfig file generated by makeFontsConf. This file is almost identical to the one from the fontconfig package and
/etc/fonts/fonts.conf on NixOS -- it only adds the texlive font directories. In particular this means that all the actual
configuration files in /etc/fonts/conf.d are still included and there also is a /usr/share/fonts font directory entry for use with non-NixOS linuxes, so it's unlikely that this breaks someone's setup.

Darwin remains broken since XeTeX doesn't use fontconfig there.

This also exposes texlive's font directory as a passthru-attribute, which makes adding texlive's fonts to the fonts.fonts. nixos-option (or symlinking the directory to ~/Library/fonts on MacOS with home-manager) a lot easier.

As the wrapper uses --set-default, devShells can easily override the font directories to add more fonts. For example in the shell

{ pkgs ? import <nixpkgs> {} }:
let
  texlive = pkgs.texlive.combined.scheme-full;
  systemfontdirs = with pkgs; map toString [ comic-mono crimson ];
in pkgs.mkShell {
    nativeBuildInputs = [ texlive ];
    # used by xetex and mtx-fonts (context)
    FONTCONFIG_FILE = pkgs.makeFontsConf { fontDirectories = systemfontdirs ++ [ texlive.fonts ]; };
    # used by luaotfload (lualatex)
    OSFONTDIR = pkgs.lib.concatStringsSep "//:" systemfontdirs;
}

the document

\documentclass{article}
\usepackage{mwe,fontspec}
\begin{document}
\fontspec{Crimson} \blindtext % "system" font
\fontspec{STIX Two Text} \blindtext % texlive font
\end{document}

compiles with both lualatex and xelatex. (It's a bit unfortunate that luaotfload hardcodes the fontconfig location to /etc/fonts/fonts.conf and /usr/local/etc/fonts/fonts.conf, so a separate environment variable has to be used, but at least it works).

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.05 Release Notes (or backporting 22.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

@github-actions github-actions bot added the 6.topic: TeX Issues regarding texlive and TeX in general label Apr 25, 2023
@apfelkuchen6 apfelkuchen6 requested review from veprbl and xworld21 April 25, 2023 20:47
@ofborg ofborg bot added 10.rebuild-darwin: 101-500 This PR causes between 101 and 500 packages to rebuild on Darwin. 10.rebuild-linux: 101-500 This PR causes between 101 and 500 packages to rebuild on Linux. labels Apr 25, 2023
@apfelkuchen6 apfelkuchen6 changed the title texlive: fix system fonts for xetex texlive: fix texlive fonts for xetex Apr 25, 2023
Copy link
Member

@veprbl veprbl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I don't see why it won't work on darwin.

@apfelkuchen6
Copy link
Contributor Author

apfelkuchen6 commented Apr 26, 2023

After digging a bit more into it, I think that it should probably also work for darwin.

TeXLive's binary distribution builds XeTeX with the font manager provided by Mac OS's native font framework (from ApplicationServices), but since we don't provide this as a build input, it should fall back to using fontconfig [in nixpkgs].

It would be great if someone with access to a Mac (@xworld21?) could confirm.

@lovesegfault
Copy link
Member

@apfelkuchen6 If you give me the steps, I have an aarch64-darwin system I can test on

@apfelkuchen6
Copy link
Contributor Author

@apfelkuchen6 If you give me the steps, I have an aarch64-darwin system I can test on

That would be great.
If the the following succeeds, it should also work on Darwin:

cd $(mktemp -d)
nix --experimental-features 'nix-command flakes' build github:apfelkuchen6/nixpkgs/texlive-xetex-fonts#texlive.combined.scheme-medium
cat > test.tex <<EOF
\documentclass{article}
\usepackage{fontspec}
\setmainfont{TeX Gyre Termes}
\begin{document}
This is a test document.
\end{document}
EOF
./result/bin/xelatex test.tex

@lovesegfault
Copy link
Member

@apfelkuchen6

Logs
❯ ./result/bin/xelatex test.tex
This is XeTeX, Version 3.141592653-2.6-0.999994 (Web2C 2022/nixos.org) (preloaded format=xelatex)
 restricted \write18 enabled.
entering extended mode
(./test.tex
LaTeX2e <2022-11-01> patch level 1
L3 programming layer <2023-02-22>
(/nix/store/r4lg5xmjaa413a8wsi58mgwscz78fbih-texlive-combined-medium-2022.20221
227-texmfroot/texmf-dist/tex/latex/base/article.cls
Document Class: article 2022/07/02 v1.4n Standard LaTeX document class

(/nix/store/r4lg5xmjaa413a8wsi58mgwscz78fbih-texlive-combined-medium-2022.20221
227-texmfroot/texmf-dist/tex/latex/base/size10.clo))
(/nix/store/r4lg5xmjaa413a8wsi58mgwscz78fbih-texlive-combined-medium-2022.20221
227-texmfroot/texmf-dist/tex/latex/fontspec/fontspec.sty
(/nix/store/r4lg5xmjaa413a8wsi58mgwscz78fbih-texlive-combined-medium-2022.20221
227-texmfroot/texmf-dist/tex/latex/l3packages/xparse/xparse.sty
(/nix/store/r4lg5xmjaa413a8wsi58mgwscz78fbih-texlive-combined-medium-2022.20221
227-texmfroot/texmf-dist/tex/latex/l3kernel/expl3.sty
(/nix/store/r4lg5xmjaa413a8wsi58mgwscz78fbih-texlive-combined-medium-2022.20221
227-texmfroot/texmf-dist/tex/latex/l3backend/l3backend-xetex.def)))
(/nix/store/r4lg5xmjaa413a8wsi58mgwscz78fbih-texlive-combined-medium-2022.20221
227-texmfroot/texmf-dist/tex/latex/fontspec/fontspec-xetex.sty
(/nix/store/r4lg5xmjaa413a8wsi58mgwscz78fbih-texlive-combined-medium-2022.20221
227-texmfroot/texmf-dist/tex/latex/base/fontenc.sty)
(/nix/store/r4lg5xmjaa413a8wsi58mgwscz78fbih-texlive-combined-medium-2022.20221
227-texmfroot/texmf-dist/tex/latex/fontspec/fontspec.cfg)))
No file test.aux.

(/nix/store/r4lg5xmjaa413a8wsi58mgwscz78fbih-texlive-combined-medium-2022.20221
227-texmfroot/texmf-dist/tex/latex/base/ts1cmr.fd) [1] (./test.aux) )
Output written on test.pdf (1 page).
Transcript written on test.log.

The resulting PDF:
test.pdf

@veprbl
Copy link
Member

veprbl commented Apr 26, 2023

Should we add a test for this?

@xworld21
Copy link
Contributor

Should we add a test for this?

Bonus points if we have a generic mkTeXLiveTest that takes combined scheme, engine, the content of a TeX file, and compiles (with things like -halt-on-error, -interaction-mode=nonstopmode). There are other tests we can run this way, e.g. hyphenation stuff, I have some of those stashed somewhere.

Copy link
Contributor

@xworld21 xworld21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! I tested this in a pure shell, and XeLaTeX works (Darwin x86-64). Great job. I find the hardcoded values in makeFontsConf slightly troubling, but if that's what Nixpkgs uses, then it's right to use it here as well.

However, always on Darwin, I am not able to use Lucida Grande. Probably because we are using fontconfig instead of the native solution, and we may have to improve on that. But this PR is only making things better, so let's start from here.

@xworld21
Copy link
Contributor

PS:

<!-- FHS paths for non-NixOS platforms -->
<dir>/usr/share/fonts</dir>
<dir>/usr/local/share/fonts</dir>

this is probably where /System/Library/Fonts, /Library/Fonts, ~/Library/Fonts should be added, guarded with an XSLT parameter so that they are used on Darwin only (list of folders taken from Homebrew's fontconfig).

This is neccessary for XeTeX to find the fonts distributed with
TeXLive.

The fontconfig file generated by makeFontsConf is almost identical to the one
from the fontconfig package and /etc/fonts/fonts.conf on NixOS -- it only adds
the specified font directories. In particular this means that all the actual
configuration files in /etc/fonts/conf.d are still included and there also is a
/usr/share/fonts font directory entry for use with non-NixOS linuxes, so
it's unlikely that this breaks someone's setup.
This is for example useful for ncluding the texlive font directory in the
`fonts.fonts` NixOS option.
@apfelkuchen6
Copy link
Contributor Author

Bonus points if we have a generic mkTeXLiveTest that takes combined scheme, engine, the content of a TeX file, and compiles (with things like -halt-on-error, -interaction-mode=nonstopmode). There are other tests we can run this way, e.g. hyphenation stuff, I have some of those stashed somewhere.

I really like this idea, but not in this PR :). I have piggy-backed a simple test onto the similar luaotfload-test for now.

@veprbl
Copy link
Member

veprbl commented Apr 26, 2023

@GrahamcOfBorg build tests.texlive

@ofborg ofborg bot requested a review from veprbl April 27, 2023 00:04
@veprbl veprbl merged commit af4cf4d into NixOS:master Apr 27, 2023
@apfelkuchen6 apfelkuchen6 deleted the texlive-xetex-fonts branch April 27, 2023 01:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: TeX Issues regarding texlive and TeX in general 10.rebuild-darwin: 101-500 This PR causes between 101 and 500 packages to rebuild on Darwin. 10.rebuild-linux: 101-500 This PR causes between 101 and 500 packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants