1 { config, lib, pkgs, ... }:
4 gtk.iconCache.enable = lib.mkOption {
6 default = config.services.xserver.enable;
7 defaultText = lib.literalExpression "config.services.xserver.enable";
9 Whether to build icon theme caches for GTK applications.
14 config = lib.mkIf config.gtk.iconCache.enable {
16 # (Re)build icon theme caches
17 # ---------------------------
18 # Each icon theme has its own cache. The difficult is that many
19 # packages may contribute with icons to the same theme by installing
22 # For instance, on my current NixOS system, the following packages
23 # (among many others) have icons installed into the hicolor icon
24 # theme: hicolor-icon-theme, psensor, wpa_gui, caja, etc.
26 # As another example, the mate icon theme has icons installed by the
27 # packages mate-icon-theme, mate-settings-daemon, and libmateweather.
29 # The HighContrast icon theme also has icons from different packages,
30 # like gnome-theme-extras and meld.
32 # When the cache is built all of its icons has to be known. How to
35 # I think that most themes have all icons installed by only one
36 # package. On my system there are 71 themes installed. Only 3 of them
37 # have icons installed from more than one package.
39 # If the main package of the theme provides a cache, presumably most
40 # of its icons will be available to applications without running this
41 # module. But additional icons offered by other packages will not be
42 # available. Therefore I think that it is good that the main theme
43 # package installs a cache (although it does not completely fixes the
44 # situation for packages installed with nix-env).
46 # The module solution presented here keeps the cache when there is
47 # only one package contributing with icons to the theme. Otherwise it
48 # rebuilds the cache taking into account the icons provided all
51 environment.extraSetup = ''
52 # For each icon theme directory ...
53 find $out/share/icons -exec test -d {} ';' -mindepth 1 -maxdepth 1 -print0 | while read -d $'\0' themedir
55 # In order to build the cache, the theme dir should be
56 # writable. When the theme dir is a symbolic link to somewhere
57 # in the nix store it is not writable and it means that only
58 # one package is contributing to the theme. If it already has
59 # a cache, no rebuild is needed. Otherwise a cache has to be
60 # built, and to be able to do that we first remove the
61 # symbolic link and make a directory, and then make symbolic
62 # links from the original directory into the new one.
64 if [ ! -w "$themedir" -a -L "$themedir" -a ! -r "$themedir"/icon-theme.cache ]; then
65 name=$(basename "$themedir")
66 path=$(readlink -f "$themedir")
69 ln -s "$path"/* "$themedir"/
72 # (Re)build the cache if the theme dir is writable, replacing any
73 # existing cache for the theme
75 if [ -w "$themedir" ]; then
76 rm -f "$themedir"/icon-theme.cache
77 ${pkgs.buildPackages.gtk3.out}/bin/gtk-update-icon-cache --ignore-theme-index "$themedir"