From 44414466726dce8b516048156c7489f5670b3ce1 Mon Sep 17 00:00:00 2001 From: Benedikt Rips Date: Sat, 25 Apr 2026 14:04:16 +0200 Subject: [PATCH] fontconfig: generate config with `pkgs.formats.xml` --- modules/misc/fontconfig.nix | 136 ++++++++---------- .../misc/fontconfig/custom-rendering.nix | 34 ++--- .../modules/misc/fontconfig/default-fonts.nix | 41 ++---- tests/modules/misc/fontconfig/fonts.nix | 26 ++-- 4 files changed, 98 insertions(+), 139 deletions(-) diff --git a/modules/misc/fontconfig.nix b/modules/misc/fontconfig.nix index 5c1856d37..6ff10cfab 100644 --- a/modules/misc/fontconfig.nix +++ b/modules/misc/fontconfig.nix @@ -324,99 +324,89 @@ in fonts.fontconfig.configFile = let - mkFontconfigConf = conf: '' - - - - - - - ${conf} - - ''; + xml = pkgs.formats.xml { }; + mkFontconfigConf = name: conf: xml.generate name { fontconfig = conf; }; in { - fonts = { - enable = true; - priority = 10; - 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 - ${config.home.profileDirectory}/lib/X11/fonts - ${config.home.profileDirectory}/share/fonts - - ${config.home.path}/lib/fontconfig/cache - ''; - }; + fonts = + let + mkInclude = path: { + "@ignore_missing" = "yes"; + "#text" = path; + }; + in + { + enable = true; + priority = 10; + source = mkFontconfigConf "fonts" { + description = "Add fonts in the Nix user profile"; + include = map mkInclude [ + "${config.home.path}/etc/fonts/conf.d" + "${config.home.path}/etc/fonts/fonts.conf" + ]; + dir = [ + "${config.home.path}/lib/X11/fonts" + "${config.home.path}/share/fonts" + "${config.home.profileDirectory}/lib/X11/fonts" + "${config.home.profileDirectory}/share/fonts" + ]; + cachedir = "${config.home.path}/lib/fontconfig/cache"; + }; + }; rendering = let - set = + toXmlValue = + value: + if lib.isBool value then + { bool = lib.boolToString value; } + else if lib.isString value then + { const = value; } + else + throw "expected bool or string but got ${lib.typeOf value}: ${toString value}"; + assign = 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} - - - ''; + toXmlValue value + // { + "@mode" = "assign"; + "@name" = name; + }; content = - lib.optional (cfg.antialiasing != null) (set "antialias" cfg.antialiasing) + lib.optional (cfg.antialiasing != null) (assign "antialias" cfg.antialiasing) ++ lib.optionals (cfg.hinting != null) [ - (set "hinting" true) - (set "hintstyle" ("hint" + cfg.hinting)) + (assign "hinting" true) + (assign "hintstyle" ("hint" + cfg.hinting)) ] ++ lib.optional (cfg.subpixelRendering != null) ( - set "rgba" (builtins.replaceStrings [ "ertical-" ] [ "" ] cfg.subpixelRendering) + assign "rgba" (lib.replaceStrings [ "ertical-" ] [ "" ] cfg.subpixelRendering) ); in { - enable = builtins.length content > 0; + enable = lib.length content > 0; priority = 10; - text = mkFontconfigConf ( - lib.concatStrings ([ "Set the rendering mode\n" ] ++ content) - ); + source = mkFontconfigConf "rendering" { + description = "Set the rendering mode"; + match = { + "@target" = "font"; + edit = content; + }; + }; }; default-fonts = let - genDefault = - fonts: name: - lib.optionalString (fonts != [ ]) '' - - ${name} - - ${lib.concatStringsSep "" ( - map (font: '' - ${font} - '') fonts - )} - - - ''; + filterNonEmpty = lib.filterAttrs (_: fonts: fonts != [ ]); + mkAlias = name: fonts: { + "@binding" = "same"; + family = if name == "sansSerif" then "sans-serif" else name; + prefer.family = fonts; + }; in { enable = true; priority = 52; - text = mkFontconfigConf '' - - ${genDefault cfg.defaultFonts.sansSerif "sans-serif"} - ${genDefault cfg.defaultFonts.serif "serif"} - ${genDefault cfg.defaultFonts.monospace "monospace"} - ${genDefault cfg.defaultFonts.emoji "emoji"} - ''; + source = mkFontconfigConf "default-fonts" { + description = "Set default fonts"; + alias = lib.mapAttrsToList mkAlias (filterNonEmpty cfg.defaultFonts); + }; }; }; diff --git a/tests/modules/misc/fontconfig/custom-rendering.nix b/tests/modules/misc/fontconfig/custom-rendering.nix index 113884ff8..99bab54e5 100644 --- a/tests/modules/misc/fontconfig/custom-rendering.nix +++ b/tests/modules/misc/fontconfig/custom-rendering.nix @@ -1,3 +1,6 @@ +let + configFile = "home-files/.config/fontconfig/conf.d/10-hm-rendering.conf"; +in { fonts.fontconfig = { enable = true; @@ -6,42 +9,27 @@ subpixelRendering = "vertical-bgr"; }; - nmt.script = - let - configFile = "home-files/.config/fontconfig/conf.d/10-hm-rendering.conf"; - in - '' - assertFileExists ${configFile} - assertFileContent ${configFile} ${builtins.toFile "rendering.conf" '' - - - - - - + nmt.script = '' + assertFileExists ${configFile} + assertFileContent ${configFile} ${builtins.toFile "rendering.conf" '' + + Set the rendering mode false - - true - - hintnone - - vbgr - - - ''} - ''; + + ''} + ''; } diff --git a/tests/modules/misc/fontconfig/default-fonts.nix b/tests/modules/misc/fontconfig/default-fonts.nix index 454e6bf12..418340447 100644 --- a/tests/modules/misc/fontconfig/default-fonts.nix +++ b/tests/modules/misc/fontconfig/default-fonts.nix @@ -16,33 +16,22 @@ in nmt.script = '' assertFileExists ${configFile} assertFileContent ${configFile} ${builtins.toFile "fonts.conf" '' - - - - - + - - - sans-serif - - A - B - - - - - - - - emoji - - C - - - - - + + emoji + + C + + + + sans-serif + + A + B + + + Set default fonts ''} ''; diff --git a/tests/modules/misc/fontconfig/fonts.nix b/tests/modules/misc/fontconfig/fonts.nix index 120c59eee..cf7156583 100644 --- a/tests/modules/misc/fontconfig/fonts.nix +++ b/tests/modules/misc/fontconfig/fonts.nix @@ -9,24 +9,16 @@ in nmt.script = '' assertFileExists ${configFile} assertFileContent ${configFile} ${pkgs.writeText "fonts.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 - ${config.home.profileDirectory}/lib/X11/fonts - ${config.home.profileDirectory}/share/fonts - - ${config.home.path}/lib/fontconfig/cache - + ${config.home.path}/lib/fontconfig/cache + Add fonts in the Nix user profile + ${config.home.path}/lib/X11/fonts + ${config.home.path}/share/fonts + ${config.home.profileDirectory}/lib/X11/fonts + ${config.home.profileDirectory}/share/fonts + ${config.home.path}/etc/fonts/conf.d + ${config.home.path}/etc/fonts/fonts.conf ''} '';