fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
2.7 KiB97 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.fonts.fontconfig;
8
9  profileDirectory = config.home.profileDirectory;
10
11in {
12  meta.maintainers = [ maintainers.rycee ];
13
14  imports = [
15    (mkRenamedOptionModule [ "fonts" "fontconfig" "enableProfileFonts" ] [
16      "fonts"
17      "fontconfig"
18      "enable"
19    ])
20  ];
21
22  options = {
23    fonts.fontconfig = {
24      enable = mkOption {
25        type = types.bool;
26        default = false;
27        description = ''
28          Whether to enable fontconfig configuration. This will, for
29          example, allow fontconfig to discover fonts and
30          configurations installed through
31          {var}`home.packages` and
32          {command}`nix-env`.
33        '';
34      };
35    };
36  };
37
38  config = mkIf cfg.enable {
39    home.packages = [
40      # Make sure that buildEnv creates a real directory path so that we avoid
41      # trying to write to a read-only location.
42      (pkgs.runCommandLocal "dummy-fc-dir1" { } "mkdir -p $out/lib/fontconfig")
43      (pkgs.runCommandLocal "dummy-fc-dir2" { } "mkdir -p $out/lib/fontconfig")
44    ];
45
46    home.extraProfileCommands = ''
47      if [[ -d $out/lib/X11/fonts || -d $out/share/fonts ]]; then
48        export FONTCONFIG_FILE="$(pwd)/fonts.conf"
49
50        cat > $FONTCONFIG_FILE << EOF
51      <?xml version='1.0'?>
52      <!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>
53      <fontconfig>
54        <dir>$out/lib/X11/fonts</dir>
55        <dir>$out/share/fonts</dir>
56        <cachedir>$out/lib/fontconfig/cache</cachedir>
57      </fontconfig>
58      EOF
59
60        ${getBin pkgs.fontconfig}/bin/fc-cache -f
61        rm -f $out/lib/fontconfig/cache/CACHEDIR.TAG
62        rmdir --ignore-fail-on-non-empty -p $out/lib/fontconfig/cache
63
64        rm "$FONTCONFIG_FILE"
65        unset FONTCONFIG_FILE
66      fi
67
68      # Remove the fontconfig directory if no files were available.
69      if [[ -d $out/lib/fontconfig ]] ; then
70        rmdir --ignore-fail-on-non-empty -p $out/lib/fontconfig
71      fi
72    '';
73
74    xdg.configFile = {
75      "fontconfig/conf.d/10-hm-fonts.conf".text = ''
76        <?xml version='1.0'?>
77
78        <!-- Generated by Home Manager. -->
79
80        <!DOCTYPE fontconfig SYSTEM 'urn:fontconfig:fonts.dtd'>
81        <fontconfig>
82          <description>Add fonts in the Nix user profile</description>
83
84          <include ignore_missing="yes">${config.home.path}/etc/fonts/conf.d</include>
85          <include ignore_missing="yes">${config.home.path}/etc/fonts/fonts.conf</include>
86
87          <dir>${config.home.path}/lib/X11/fonts</dir>
88          <dir>${config.home.path}/share/fonts</dir>
89          <dir>${profileDirectory}/lib/X11/fonts</dir>
90          <dir>${profileDirectory}/share/fonts</dir>
91
92          <cachedir>${config.home.path}/lib/fontconfig/cache</cachedir>
93        </fontconfig>
94      '';
95    };
96  };
97}