fontconfig: generate config with pkgs.formats.xml
This commit is contained in:
committed by
Austin Horstman
parent
27a3154f42
commit
4441446672
+63
-73
@@ -324,99 +324,89 @@ in
|
||||
|
||||
fonts.fontconfig.configFile =
|
||||
let
|
||||
mkFontconfigConf = conf: ''
|
||||
<?xml version='1.0'?>
|
||||
|
||||
<!-- Generated by Home Manager. -->
|
||||
|
||||
<!DOCTYPE fontconfig SYSTEM 'urn:fontconfig:fonts.dtd'>
|
||||
<fontconfig>
|
||||
${conf}
|
||||
</fontconfig>
|
||||
'';
|
||||
xml = pkgs.formats.xml { };
|
||||
mkFontconfigConf = name: conf: xml.generate name { fontconfig = conf; };
|
||||
in
|
||||
{
|
||||
fonts = {
|
||||
enable = true;
|
||||
priority = 10;
|
||||
text = mkFontconfigConf ''
|
||||
<description>Add fonts in the Nix user profile</description>
|
||||
|
||||
<include ignore_missing="yes">${config.home.path}/etc/fonts/conf.d</include>
|
||||
<include ignore_missing="yes">${config.home.path}/etc/fonts/fonts.conf</include>
|
||||
|
||||
<dir>${config.home.path}/lib/X11/fonts</dir>
|
||||
<dir>${config.home.path}/share/fonts</dir>
|
||||
<dir>${config.home.profileDirectory}/lib/X11/fonts</dir>
|
||||
<dir>${config.home.profileDirectory}/share/fonts</dir>
|
||||
|
||||
<cachedir>${config.home.path}/lib/fontconfig/cache</cachedir>
|
||||
'';
|
||||
};
|
||||
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
|
||||
"<bool>${lib.boolToString value}</bool>"
|
||||
else if builtins.isString value then
|
||||
"<const>${value}</const>"
|
||||
else
|
||||
throw "expected bool or string but got ${builtins.typeOf value}: ${toString value}";
|
||||
in
|
||||
''
|
||||
<match target="font">
|
||||
<edit mode="assign" name="${name}">
|
||||
${xmlValue}
|
||||
</edit>
|
||||
</match>
|
||||
'';
|
||||
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 ([ "<description>Set the rendering mode</description>\n" ] ++ content)
|
||||
);
|
||||
source = mkFontconfigConf "rendering" {
|
||||
description = "Set the rendering mode";
|
||||
match = {
|
||||
"@target" = "font";
|
||||
edit = content;
|
||||
};
|
||||
};
|
||||
};
|
||||
default-fonts =
|
||||
let
|
||||
genDefault =
|
||||
fonts: name:
|
||||
lib.optionalString (fonts != [ ]) ''
|
||||
<alias binding="same">
|
||||
<family>${name}</family>
|
||||
<prefer>
|
||||
${lib.concatStringsSep "" (
|
||||
map (font: ''
|
||||
<family>${font}</family>
|
||||
'') fonts
|
||||
)}
|
||||
</prefer>
|
||||
</alias>
|
||||
'';
|
||||
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 ''
|
||||
<!-- Default fonts -->
|
||||
${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);
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -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" ''
|
||||
<?xml version='1.0'?>
|
||||
|
||||
<!-- Generated by Home Manager. -->
|
||||
|
||||
<!DOCTYPE fontconfig SYSTEM 'urn:fontconfig:fonts.dtd'>
|
||||
<fontconfig>
|
||||
nmt.script = ''
|
||||
assertFileExists ${configFile}
|
||||
assertFileContent ${configFile} ${builtins.toFile "rendering.conf" ''
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<fontconfig>
|
||||
<description>Set the rendering mode</description>
|
||||
<match target="font">
|
||||
<edit mode="assign" name="antialias">
|
||||
<bool>false</bool>
|
||||
</edit>
|
||||
</match>
|
||||
<match target="font">
|
||||
<edit mode="assign" name="hinting">
|
||||
<bool>true</bool>
|
||||
</edit>
|
||||
</match>
|
||||
<match target="font">
|
||||
<edit mode="assign" name="hintstyle">
|
||||
<const>hintnone</const>
|
||||
</edit>
|
||||
</match>
|
||||
<match target="font">
|
||||
<edit mode="assign" name="rgba">
|
||||
<const>vbgr</const>
|
||||
</edit>
|
||||
</match>
|
||||
|
||||
</fontconfig>
|
||||
''}
|
||||
'';
|
||||
</fontconfig>
|
||||
''}
|
||||
'';
|
||||
}
|
||||
|
||||
@@ -16,33 +16,22 @@ in
|
||||
nmt.script = ''
|
||||
assertFileExists ${configFile}
|
||||
assertFileContent ${configFile} ${builtins.toFile "fonts.conf" ''
|
||||
<?xml version='1.0'?>
|
||||
|
||||
<!-- Generated by Home Manager. -->
|
||||
|
||||
<!DOCTYPE fontconfig SYSTEM 'urn:fontconfig:fonts.dtd'>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<fontconfig>
|
||||
<!-- Default fonts -->
|
||||
<alias binding="same">
|
||||
<family>sans-serif</family>
|
||||
<prefer>
|
||||
<family>A</family>
|
||||
<family>B</family>
|
||||
|
||||
</prefer>
|
||||
</alias>
|
||||
|
||||
|
||||
|
||||
<alias binding="same">
|
||||
<family>emoji</family>
|
||||
<prefer>
|
||||
<family>C</family>
|
||||
|
||||
</prefer>
|
||||
</alias>
|
||||
|
||||
|
||||
<alias binding="same">
|
||||
<family>emoji</family>
|
||||
<prefer>
|
||||
<family>C</family>
|
||||
</prefer>
|
||||
</alias>
|
||||
<alias binding="same">
|
||||
<family>sans-serif</family>
|
||||
<prefer>
|
||||
<family>A</family>
|
||||
<family>B</family>
|
||||
</prefer>
|
||||
</alias>
|
||||
<description>Set default fonts</description>
|
||||
</fontconfig>
|
||||
''}
|
||||
'';
|
||||
|
||||
@@ -9,24 +9,16 @@ in
|
||||
nmt.script = ''
|
||||
assertFileExists ${configFile}
|
||||
assertFileContent ${configFile} ${pkgs.writeText "fonts.conf" ''
|
||||
<?xml version='1.0'?>
|
||||
|
||||
<!-- Generated by Home Manager. -->
|
||||
|
||||
<!DOCTYPE fontconfig SYSTEM 'urn:fontconfig:fonts.dtd'>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<fontconfig>
|
||||
<description>Add fonts in the Nix user profile</description>
|
||||
|
||||
<include ignore_missing="yes">${config.home.path}/etc/fonts/conf.d</include>
|
||||
<include ignore_missing="yes">${config.home.path}/etc/fonts/fonts.conf</include>
|
||||
|
||||
<dir>${config.home.path}/lib/X11/fonts</dir>
|
||||
<dir>${config.home.path}/share/fonts</dir>
|
||||
<dir>${config.home.profileDirectory}/lib/X11/fonts</dir>
|
||||
<dir>${config.home.profileDirectory}/share/fonts</dir>
|
||||
|
||||
<cachedir>${config.home.path}/lib/fontconfig/cache</cachedir>
|
||||
|
||||
<cachedir>${config.home.path}/lib/fontconfig/cache</cachedir>
|
||||
<description>Add fonts in the Nix user profile</description>
|
||||
<dir>${config.home.path}/lib/X11/fonts</dir>
|
||||
<dir>${config.home.path}/share/fonts</dir>
|
||||
<dir>${config.home.profileDirectory}/lib/X11/fonts</dir>
|
||||
<dir>${config.home.profileDirectory}/share/fonts</dir>
|
||||
<include ignore_missing="yes">${config.home.path}/etc/fonts/conf.d</include>
|
||||
<include ignore_missing="yes">${config.home.path}/etc/fonts/fonts.conf</include>
|
||||
</fontconfig>
|
||||
''}
|
||||
'';
|
||||
|
||||
Reference in New Issue
Block a user