  A search for long-lived heavy neutrinos (N) in the decays of B mesons
produced in proton-proton collisions at $\sqrt{s}$ = 13 TeV is presented. The
data sample corresponds to an integrated luminosity of 41.6 fb$^{-1}$ collected
in 2018 by the CMS experiment at the CERN LHC, using a dedicated data stream
that enhances the number of recorded events containing B mesons. The search
probes heavy neutrinos with masses in the range 1 $\lt$ $m_\mathrm{N}$ $\lt$ 3
GeV and decay lengths in the range 10$^{-2}$ $\lt$ $c\tau_\mathrm{N}$ $\lt$
10$^{4}$ mm, where $\tau_\mathrm{N}$ is the N proper mean lifetime. Signal
events are defined by the signature B $\to$ $\ell_\mathrm{B}$NX; N $\to$
$\ell^{\pm} \pi^{\mp}$, where the leptons $\ell_\mathrm{B}$ and $\ell$ can be
either a muon or an electron, provided that at least one of them is a muon. The
hadronic recoil system, X, is treated inclusively and is not reconstructed. No
significant excess of events over the standard model background is observed in
any of the $\ell^{\pm} \pi^{\mp}$ invariant mass distributions. Limits at 95%
confidence level on the sum of the squares of the mixing amplitudes between
heavy and light neutrinos, $\vert V_\mathrm{N}\vert^2$, and on $c\tau$ are
obtained in different mixing scenarios for both Majorana and Dirac-like N
particles. The most stringent upper limit $\vert V_\mathrm{N}\vert^2$ $\lt$ 2.0
$\times$ 10$^{-5}$ is obtained at $m_\mathrm{N}$ = 1.95 GeV for the Majorana
case where N mixes exclusively with muon neutrinos. The limits on $\vert
V_\mathrm{N}\vert^2$ for masses 1 $\lt$ $m_\mathrm{N}$ $\lt$ 1.7 GeV are the
most stringent from a collider experiment to date.
