From f2c5ba5e720fd584d83f2f97399dac0d26ae60b9 Mon Sep 17 00:00:00 2001 From: ilkecan Date: Fri, 18 Feb 2022 23:34:39 +0000 Subject: [PATCH] fontconfig: add defaultFonts.* options --- modules/misc/fontconfig.nix | 100 +++++++++++++++++++++++++++++++----- 1 file changed, 87 insertions(+), 13 deletions(-) diff --git a/modules/misc/fontconfig.nix b/modules/misc/fontconfig.nix index 49fef96a9..9bc6ac47b 100644 --- a/modules/misc/fontconfig.nix +++ b/modules/misc/fontconfig.nix @@ -1,3 +1,7 @@ +# This module is heavily inspired by the corresponding NixOS module. See +# +# https://github.com/NixOS/nixpkgs/blob/23.11/nixos/modules/config/fonts/fontconfig.nix + { config, lib, pkgs, ... }: with lib; @@ -32,6 +36,50 @@ in { {command}`nix-env`. ''; }; + + defaultFonts = { + monospace = mkOption { + type = with types; listOf str; + default = [ ]; + description = '' + Per-user default monospace font(s). Multiple fonts may be listed in + case multiple languages must be supported. + ''; + }; + + sansSerif = mkOption { + type = with types; listOf str; + default = [ ]; + description = '' + Per-user default sans serif font(s). Multiple fonts may be listed + in case multiple languages must be supported. + ''; + }; + + serif = mkOption { + type = with types; listOf str; + default = [ ]; + description = '' + Per-user default serif font(s). Multiple fonts may be listed in + case multiple languages must be supported. + ''; + }; + + emoji = mkOption { + type = with types; listOf str; + default = [ ]; + description = '' + Per-user default emoji font(s). Multiple fonts may be listed in + case a font does not support all emoji. + + Note that fontconfig matches color emoji fonts preferentially, + so if you want to use a black and white font while having + a color font installed (eg. Noto Color Emoji installed alongside + Noto Emoji), fontconfig will still choose the color font even + when it is later in the list. + ''; + }; + }; }; }; @@ -71,27 +119,53 @@ in { fi ''; - xdg.configFile = { - "fontconfig/conf.d/10-hm-fonts.conf".text = '' + xdg.configFile = let + mkFontconfigConf = conf: '' - Add fonts in the Nix user profile - - ${config.home.path}/etc/fonts/conf.d - ${config.home.path}/etc/fonts/fonts.conf - - ${config.home.path}/lib/X11/fonts - ${config.home.path}/share/fonts - ${profileDirectory}/lib/X11/fonts - ${profileDirectory}/share/fonts - - ${config.home.path}/lib/fontconfig/cache + ${conf} ''; + in { + "fontconfig/conf.d/10-hm-fonts.conf".text = mkFontconfigConf '' + Add fonts in the Nix user profile + + ${config.home.path}/etc/fonts/conf.d + ${config.home.path}/etc/fonts/fonts.conf + + ${config.home.path}/lib/X11/fonts + ${config.home.path}/share/fonts + ${profileDirectory}/lib/X11/fonts + ${profileDirectory}/share/fonts + + ${config.home.path}/lib/fontconfig/cache + ''; + + "fontconfig/conf.d/52-hm-default-fonts.conf".text = let + genDefault = fonts: name: + optionalString (fonts != [ ]) '' + + ${name} + + ${ + concatStringsSep "" (map (font: '' + ${font} + '') fonts) + } + + + ''; + in mkFontconfigConf '' + + ${genDefault cfg.defaultFonts.sansSerif "sans-serif"} + ${genDefault cfg.defaultFonts.serif "serif"} + ${genDefault cfg.defaultFonts.monospace "monospace"} + ${genDefault cfg.defaultFonts.emoji "emoji"} + ''; }; }; }