summary refs log tree commit diff
path: root/modules/config/fonts.nix
blob: af5ebde2d477352b9f47c078aad61660b7c2dfd9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
{ config, pkgs, ... }:

with pkgs.lib;

###### interface

let

  options = {

    fonts = {

      enableFontConfig = mkOption { # !!! should be enableFontconfig
        default = true;
        description = "
          If enabled, a Fontconfig configuration file will be built
          pointing to a set of default fonts.  If you don't care about
          running X11 applications or any other program that uses
          Fontconfig, you can turn this option off and prevent a
          dependency on all those fonts.
        ";
      };

      # Should be moved elsewhere.
      enableGhostscriptFonts = mkOption {
        default = false;
        description = "
          Whether to add the fonts provided by Ghostscript (such as
          various URW fonts and the ``Base-14'' Postscript fonts) to the
          list of system fonts, making them available to X11
          applications.
        ";
      };

      enableFontDir = mkOption {
        default = false;
        description = "
          Whether to create a directory with links to all fonts in share - 
          so user can configure vncserver script one time (I mean per-user 
          vncserver, so global service is not a good solution).
        ";
      };

      # TODO: find another name for it.
      fonts = mkOption {
        default = [
          # - the user's .fonts directory
          "~/.fonts"
          # - the user's current profile
          "~/.nix-profile/lib/X11/fonts"
          "~/.nix-profile/share/fonts"
          # - the default profile
          "/nix/var/nix/profiles/default/lib/X11/fonts"
          "/nix/var/nix/profiles/default/share/fonts"
        ];
        description = "
          List of primary font paths.
        ";
        apply = list: list ++ [
          # - a few statically built locations
          pkgs.xorg.fontbhttf
          pkgs.xorg.fontbhlucidatypewriter100dpi
          pkgs.xorg.fontbhlucidatypewriter75dpi
          pkgs.ttf_bitstream_vera
          pkgs.freefont_ttf
          pkgs.liberation_ttf
          pkgs.xorg.fontbh100dpi
          pkgs.xorg.fontmiscmisc
          pkgs.xorg.fontcursormisc
        ]
        ++ optional config.fonts.enableCoreFonts pkgs.corefonts
        ++ optional config.fonts.enableGhostscriptFonts "${pkgs.ghostscript}/share/ghostscript/fonts"
        ++ config.fonts.extraFonts;
      };

      extraFonts = mkOption {
        default = [];
        example = [ pkgs.dejavu_fonts ];
        description = ''
          List of packages with additional fonts.
        '';
      };

      enableCoreFonts = mkOption {
        default = false;
        description = ''
          Whether to include Microsoft's proprietary Core Fonts.  These fonts
          are redistributable, but only verbatim, among other restrictions.
          See <link xlink:href="http://corefonts.sourceforge.net/eula.htm"/>
          for details.
       '';
      };

    };

  };
in

###### implementation
let
  inherit (pkgs) builderDefs;
  inherit (pkgs.xorg) mkfontdir mkfontscale fontalias;

  fontDirs = config.fonts.fonts;


  localDefs = with builderDefs; builderDefs.passthru.function rec {
    src = "";/* put a fetchurl here */

    buildInputs = [mkfontdir mkfontscale];
    configureFlags = [];
    inherit fontDirs;
    installPhase = fullDepEntry ("
    list='';
    for i in ${toString fontDirs} ; do
      if [ -d \$i/ ]; then
        list=\"\$list \$i\";
      fi;
    done
    list=\$(find \$list -name fonts.dir -o -name '*.ttf' -o -name '*.otf');
    fontDirs='';
    for i in \$list ; do
      fontDirs=\"\$fontDirs \$(dirname \$i)\";
    done;
    mkdir -p \$out/share/X11-fonts/; 
    find \$fontDirs -type f -o -type l | while read i; do
      j=\"\${i##*/}\"
      if ! test -e \"\$out/share/X11-fonts/\${j}\"; then
        ln -s \"\$i\" \"\$out/share/X11-fonts/\${j}\";
      fi;
    done;
    cd \$out/share/X11-fonts/
    rm fonts.dir
    rm fonts.scale
    rm fonts.alias
    mkfontdir
    mkfontscale
    cat \$( find ${fontalias}/ -name fonts.alias) >fonts.alias
  ") ["minInit" "addInputs"];
  };

  x11Fonts = with localDefs; stdenv.mkDerivation rec {
    name = "X11-fonts";
    builder = writeScript (name + "-builder")
      (textClosure localDefs 
        [installPhase doForceShare doPropagate]);
    meta = {
      description = "
        Directory to contain all X11 fonts requested.
      ";
    };
  };

in

{
  require = [options];

  system.build.x11Fonts = x11Fonts;

  environment.etc = mkIf config.fonts.enableFontConfig
    [ { # Configuration file for fontconfig used to locate
        # (X11) client-rendered fonts.
        source = pkgs.makeFontsConf {
          fontDirectories = config.fonts.fonts;
        };
        target = "fonts/fonts.conf";
      }
    ];

  environment.shellInit =
    '' 
      export FONTCONFIG_FILE=/etc/fonts/fonts.conf
    '';

  environment.systemPackages =
    optional config.fonts.enableFontDir config.system.build.x11Fonts ++
    optional config.fonts.enableFontConfig pkgs.fontconfig;
}