# 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, ... }: let cfg = config.fonts.fontconfig; profileDirectory = config.home.profileDirectory; in { meta.maintainers = with lib.maintainers; [ bmrips rycee ]; imports = [ (lib.mkRenamedOptionModule [ "fonts" "fontconfig" "enableProfileFonts" ] [ "fonts" "fontconfig" "enable" ] ) ]; options = { fonts.fontconfig = { enable = lib.mkOption { type = lib.types.bool; default = false; description = '' Whether to enable fontconfig configuration. This will, for example, allow fontconfig to discover fonts and configurations installed through {var}`home.packages` and {command}`nix-env`. ''; }; defaultFonts = { monospace = lib.mkOption { type = with lib.types; listOf str; default = [ ]; description = '' Per-user default monospace font(s). Multiple fonts may be listed in case multiple languages must be supported. ''; }; sansSerif = lib.mkOption { type = with lib.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 = lib.mkOption { type = with lib.types; listOf str; default = [ ]; description = '' Per-user default serif font(s). Multiple fonts may be listed in case multiple languages must be supported. ''; }; emoji = lib.mkOption { type = with lib.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. ''; }; }; antialiasing = lib.mkOption { type = with lib.types; nullOr bool; default = null; description = "Whether to enable font antialiasing."; example = true; }; hinting = lib.mkOption { type = with lib.types; nullOr (enum [ "none" "slight" "medium" "full" ]); default = null; description = "The font hinting mode."; example = "slight"; }; subpixelRendering = lib.mkOption { type = with lib.types; nullOr (enum [ "none" "rgb" "bgr" "vertical-rgb" "vertical-bgr" ]); default = null; description = "The sub-pixel rendering mode."; example = "rgb"; }; }; }; config = lib.mkIf cfg.enable { home.packages = [ # Make sure that buildEnv creates a real directory path so that we avoid # trying to write to a read-only location. (pkgs.runCommandLocal "dummy-fc-dir1" { } "mkdir -p $out/lib/fontconfig") (pkgs.runCommandLocal "dummy-fc-dir2" { } "mkdir -p $out/lib/fontconfig") ]; home.extraProfileCommands = '' if [[ -d $out/lib/X11/fonts || -d $out/share/fonts ]]; then export FONTCONFIG_FILE="$(pwd)/fonts.conf" cat > $FONTCONFIG_FILE << EOF $out/lib/X11/fonts $out/share/fonts $out/lib/fontconfig/cache EOF ${lib.getBin pkgs.fontconfig}/bin/fc-cache -f rm -f $out/lib/fontconfig/cache/CACHEDIR.TAG rmdir --ignore-fail-on-non-empty -p $out/lib/fontconfig/cache rm "$FONTCONFIG_FILE" unset FONTCONFIG_FILE fi # Remove the fontconfig directory if no files were available. if [[ -d $out/lib/fontconfig ]] ; then rmdir --ignore-fail-on-non-empty -p $out/lib/fontconfig fi ''; xdg.configFile = let mkFontconfigConf = conf: '' ${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/10-hm-rendering.conf" = let set = name: value: let xmlValue = if builtins.isBool value then "${lib.boolToString value}" else if builtins.isString value then "${value}" else throw ("expected bool or string but got ${builtins.typeOf value}: ${toString value}"); in '' ${xmlValue} ''; content = lib.optional (cfg.antialiasing != null) (set "antialias" cfg.antialiasing) ++ lib.optionals (cfg.hinting != null) [ (set "hinting" true) (set "hintstyle" ("hint" + cfg.hinting)) ] ++ lib.optional (cfg.subpixelRendering != null) ( set "rgba" (builtins.replaceStrings [ "ertical-" ] [ "" ] cfg.subpixelRendering) ); in lib.mkIf (builtins.length content > 0) { text = mkFontconfigConf ( lib.concatStrings ([ "Set the rendering mode\n" ] ++ content) ); }; "fontconfig/conf.d/52-hm-default-fonts.conf".text = let genDefault = fonts: name: lib.optionalString (fonts != [ ]) '' ${name} ${lib.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"} ''; }; }; }