fork/home-manager

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

polykernelhome-cursor: remove IFD when linking icon directoriesa0ad9817

master
5.7 KiB182 linesraw
1{ config, options, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.home.pointerCursor;
8
9  pointerCursorModule = types.submodule {
10    options = {
11      package = mkOption {
12        type = types.package;
13        example = literalExpression "pkgs.vanilla-dmz";
14        description = "Package providing the cursor theme.";
15      };
16
17      name = mkOption {
18        type = types.str;
19        example = "Vanilla-DMZ";
20        description = "The cursor name within the package.";
21      };
22
23      size = mkOption {
24        type = types.int;
25        default = 32;
26        example = 64;
27        description = "The cursor size.";
28      };
29
30      x11 = {
31        enable = mkEnableOption ''
32          x11 config generation for {option}`home.pointerCursor`
33        '';
34
35        defaultCursor = mkOption {
36          type = types.str;
37          default = "left_ptr";
38          example = "X_cursor";
39          description = "The default cursor file to use within the package.";
40        };
41      };
42
43      gtk = {
44        enable = mkEnableOption ''
45          gtk config generation for {option}`home.pointerCursor`
46        '';
47      };
48    };
49  };
50
51  cursorPath = "${cfg.package}/share/icons/${escapeShellArg cfg.name}/cursors/${
52      escapeShellArg cfg.x11.defaultCursor
53    }";
54
55  defaultIndexThemePackage = pkgs.writeTextFile {
56    name = "index.theme";
57    destination = "/share/icons/default/index.theme";
58    # Set name in icons theme, for compatibility with AwesomeWM etc. See:
59    # https://github.com/nix-community/home-manager/issues/2081
60    # https://wiki.archlinux.org/title/Cursor_themes#XDG_specification
61    text = ''
62      [Icon Theme]
63      Name=Default
64      Comment=Default Cursor Theme
65      Inherits=${cfg.name}
66    '';
67  };
68
69in {
70  meta.maintainers = [ maintainers.polykernel maintainers.league ];
71
72  imports = [
73    (mkAliasOptionModule [ "xsession" "pointerCursor" "package" ] [
74      "home"
75      "pointerCursor"
76      "package"
77    ])
78    (mkAliasOptionModule [ "xsession" "pointerCursor" "name" ] [
79      "home"
80      "pointerCursor"
81      "name"
82    ])
83    (mkAliasOptionModule [ "xsession" "pointerCursor" "size" ] [
84      "home"
85      "pointerCursor"
86      "size"
87    ])
88    (mkAliasOptionModule [ "xsession" "pointerCursor" "defaultCursor" ] [
89      "home"
90      "pointerCursor"
91      "x11"
92      "defaultCursor"
93    ])
94
95    ({ ... }: {
96      warnings = optional (any (x:
97        getAttrFromPath
98        ([ "xsession" "pointerCursor" ] ++ [ x ] ++ [ "isDefined" ])
99        options) [ "package" "name" "size" "defaultCursor" ]) ''
100          The option `xsession.pointerCursor` has been merged into `home.pointerCursor` and will be removed
101          in the future. Please change to set `home.pointerCursor` directly and enable `home.pointerCursor.x11.enable`
102          to generate x11 specific cursor configurations. You can refer to the documentation for more details.
103        '';
104    })
105  ];
106
107  options = {
108    home.pointerCursor = mkOption {
109      type = types.nullOr pointerCursorModule;
110      default = null;
111      description = ''
112        Cursor configuration. Set to `null` to disable.
113
114        Top-level options declared under this submodule are backend independent
115        options. Options declared under namespaces such as `x11`
116        are backend specific options. By default, only backend independent cursor
117        configurations are generated. If you need configurations for specific
118        backends, you can toggle them via the enable option. For example,
119        [](#opt-home.pointerCursor.x11.enable)
120        will enable x11 cursor configurations.
121
122        Note that this will merely generate the cursor configurations.
123        To apply the configurations, the relevant subsytems must also be configured.
124        For example, [](#opt-home.pointerCursor.gtk.enable) will generate
125        the gtk cursor configuration, but [](#opt-gtk.enable) needs
126        to be set for it to be applied.
127      '';
128    };
129  };
130
131  config = mkIf (cfg != null) (mkMerge [
132    {
133      assertions = [
134        (hm.assertions.assertPlatform "home.pointerCursor" pkgs platforms.linux)
135      ];
136
137      home.packages = [ cfg.package defaultIndexThemePackage ];
138
139      # Set directory to look for cursors in, needed for some applications
140      # that are unable to find cursors otherwise. See:
141      # https://github.com/nix-community/home-manager/issues/2812
142      # https://wiki.archlinux.org/title/Cursor_themes#Environment_variable
143      home.sessionVariables = {
144        XCURSOR_PATH = mkDefault ("$XCURSOR_PATH\${XCURSOR_PATH:+:}"
145          + "${config.home.profileDirectory}/share/icons");
146        XCURSOR_SIZE = mkDefault cfg.size;
147        XCURSOR_THEME = mkDefault cfg.name;
148      };
149
150      # Add symlink of cursor icon directory to $HOME/.icons, needed for
151      # backwards compatibility with some applications. See:
152      # https://specifications.freedesktop.org/icon-theme-spec/latest/ar01s03.html
153      home.file.".icons/default/index.theme".source =
154        "${defaultIndexThemePackage}/share/icons/default/index.theme";
155      home.file.".icons/${cfg.name}".source =
156        "${cfg.package}/share/icons/${cfg.name}";
157
158      # Add cursor icon link to $XDG_DATA_HOME as well for redundancy.
159      xdg.dataFile.".icons/default/index.theme".source =
160        "${defaultIndexThemePackage}/share/icons/default/index.theme";
161      xdg.dataFile.".icons/${cfg.name}".source =
162        "${cfg.package}/share/icons/${cfg.name}";
163    }
164
165    (mkIf cfg.x11.enable {
166      xsession.initExtra = ''
167        ${pkgs.xorg.xsetroot}/bin/xsetroot -xcf ${cursorPath} ${
168          toString cfg.size
169        }
170      '';
171
172      xresources.properties = {
173        "Xcursor.theme" = cfg.name;
174        "Xcursor.size" = cfg.size;
175      };
176    })
177
178    (mkIf cfg.gtk.enable {
179      gtk.cursorTheme = mkDefault { inherit (cfg) package name size; };
180    })
181  ]);
182}