MLModern with fontspec using LuaLaTeX

font-encodingsfontsfontspec

I'd like to typeset a document in MLModern's fonts (serif, sans serif, teletype, etc.), then later change the teletype font to a different custom font whose TTF I provide (Iosevka, in the example below). This is my attempt at doing it using fontspec, compiling with LuaLaTex:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{fontspec}
\usepackage{mlmodern}

\begin{document}
%\setmainfont{MLModern}
This should be in MLModern's roman font.

\texttt{This should be in MLModern's teletype font.}

\setmonofont{iosevka.ttf}
\texttt{This should be in Iosevka.}
\end{document}

Having loaded the mlmodern package, I expected the default font to be MLModern, but the first line seems to be in Latin Modern instead.
Curiously, the second line is also in roman and not teletype.
(Third line is fine.)

If I try to set the main font to MLModern with the commented out line above, compilation doesn't succeed:

This is LuaHBTeX, Version 1.13.2 (TeX Live 2021/Arch Linux) 
 restricted system commands enabled.
(./main.tex
LaTeX2e <2021-11-15> patch level 1
 L3 programming layer <2021-11-22>
(/usr/share/texmf-dist/tex/latex/base/article.cls
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class
(/usr/share/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texmf-dist/tex/latex/base/fontenc.sty
(/usr/share/texmf-dist/tex/latex/lm/t1lmr.fd))
(/usr/share/texmf-dist/tex/latex/fontspec/fontspec.sty
(/usr/share/texmf-dist/tex/latex/l3packages/xparse/xparse.sty
(/usr/share/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/share/texmf-dist/tex/latex/l3backend/l3backend-luatex.def)))
(/usr/share/texmf-dist/tex/latex/fontspec/fontspec-luatex.sty
(/usr/share/texmf-dist/tex/latex/base/fontenc.sty)
(/usr/share/texmf-dist/tex/latex/fontspec/fontspec.cfg)))
(/usr/share/texmf-dist/tex/latex/mlmodern/mlmodern.sty)

LaTeX Font Warning: Font shape `TU/mlmr/m/n' undefined
(Font)              using `TU/lmr/m/n' instead on input line 7.

(./main.aux) (/usr/share/texmf-dist/tex/latex/base/ts1cmr.fd)
luaotfload | db : Reload initiated (formats: otf,ttf,ttc); reason: Font "MLModern" not found.

Other names I've tried are mlmodern and mlm, with the same result. How do I set the main font to MLModern then?

Best Answer

(EDIT 1: In an earlier version of this answer, I said to use 'OT1' as the option to fontenc; I now think you can use T1 or OT1; you just need to put the packages in the right order...)

Apparently you need to load the packages in a certain order.

The following works for me (– I don't have Iosevka installed right now so I substituted Cousine –):

\documentclass{article}

\usepackage{fontspec}
\usepackage[T1]{fontenc}
\usepackage{mlmodern}

\begin{document}
This should be in MLModern's roman font.

\texttt{This should be in MLModern's teletype font.}

\setmonofont{Cousine}
\texttt{This should be in Cousine.}
\end{document}

fontspec needs to go first.

(Apparently it's not supposed to be necessary to load fontenc at all with lualatex: see here, but actually in this case, it seems to help because it looks for a TU encoded font first otherwise.)

EDIT 2: I came up with this answer by trial and error, not deep knowledge of best practices. For reasons Ulrike Fischer mentions in the comments to this answer, this may not be a great idea. You may encounter problems.

I haven't found any problems with hyphens, but you may.

For non-ascii characters, some of them work, some of them don't, but in my testing, the ascii LaTeX commands to produce non-ascii characters do generally work. For example, inserting ß directly doesn't work, but \ss does; using directly doesn't work, but \ldots does and so on. How big a problem this is obviously depends on your use case. But I find that even with the non-ascii characters, it works to load the newunicodechar package and issue commands such as these at the end of the preamble:

\usepackage{newunicodechar}
\newunicodechar{ß}{\ss}
\newunicodechar{…}{\ldots}
\newunicodechar{—}{---}
% and so on

This allows me to use the non-ascii characters in my source. But my testing of this is not exhaustive, so use this answer with caution.