fork/home-manager

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

Christian Albrechtmodules: types.string throws error now (#4324)7b8d43fb

master
7.0 KiB215 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6  desktopEntry = {
7    imports = [
8      (mkRemovedOptionModule [ "extraConfig" ]
9        "The `extraConfig` option of `xdg.desktopEntries` has been removed following a change in Nixpkgs.")
10      (mkRemovedOptionModule [ "fileValidation" ]
11        "Validation of the desktop file is always enabled.")
12    ];
13    options = {
14      # Since this module uses the nixpkgs/pkgs/build-support/make-desktopitem function,
15      # our options and defaults follow its parameters, with the following exceptions:
16
17      # `desktopName` on makeDesktopItem is controlled by `name`.
18      # This is what we'd commonly consider the name of the application.
19      # `name` on makeDesktopItem is controlled by this module's key in the attrset.
20      # This is the file's filename excluding ".desktop".
21
22      # `extraConfig` on makeDesktopItem is controlled by `settings`,
23      # to match what's commonly used by other home manager modules.
24
25      # Descriptions are taken from the desktop entry spec:
26      # https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#recognized-keys
27
28      type = mkOption {
29        description = "The type of the desktop entry.";
30        default = "Application";
31        type = types.enum [ "Application" "Link" "Directory" ];
32      };
33
34      exec = mkOption {
35        description = "Program to execute, possibly with arguments.";
36        type = types.nullOr types.str;
37        default = null;
38      };
39
40      icon = mkOption {
41        description = "Icon to display in file manager, menus, etc.";
42        type = with types; nullOr (either str path);
43        default = null;
44      };
45
46      comment = mkOption {
47        description = "Tooltip for the entry.";
48        type = types.nullOr types.str;
49        default = null;
50      };
51
52      terminal = mkOption {
53        description = "Whether the program runs in a terminal window.";
54        type = types.bool;
55        default = false;
56      };
57
58      name = mkOption {
59        description = "Specific name of the application.";
60        type = types.str;
61      };
62
63      genericName = mkOption {
64        description = "Generic name of the application.";
65        type = types.nullOr types.str;
66        default = null;
67      };
68
69      mimeType = mkOption {
70        description = "The MIME type(s) supported by this application.";
71        type = types.nullOr (types.listOf types.str);
72        default = null;
73      };
74
75      categories = mkOption {
76        description =
77          "Categories in which the entry should be shown in a menu.";
78        type = types.nullOr (types.listOf types.str);
79        default = null;
80      };
81
82      startupNotify = mkOption {
83        description = ''
84          If true, it is KNOWN that the application will send a "remove"
85          message when started with the `DESKTOP_STARTUP_ID`
86          environment variable set. If false, it is KNOWN that the application
87          does not work with startup notification at all.'';
88        type = types.nullOr types.bool;
89        default = null;
90      };
91
92      noDisplay = mkOption {
93        description = ''
94          Means "this application exists, but don't display it in the menus".
95          This can be useful to e.g. associate this application with MIME types.
96        '';
97        type = types.nullOr types.bool;
98        default = null;
99      };
100
101      prefersNonDefaultGPU = mkOption {
102        description = ''
103          If true, the application prefers to be run on a more powerful discrete GPU if available.
104        '';
105        type = types.nullOr types.bool;
106        default = null;
107      };
108
109      settings = mkOption {
110        type = types.attrsOf types.str;
111        description = ''
112          Extra key-value pairs to add to the `[Desktop Entry]` section.
113          This may override other values.
114        '';
115        default = { };
116        example = literalExpression ''
117          {
118            Keywords = "calc;math";
119            DBusActivatable = "false";
120          }
121        '';
122      };
123
124      actions = mkOption {
125        type = types.attrsOf (types.submodule ({ name, ... }: {
126          options.name = mkOption {
127            type = types.str;
128            default = name;
129            defaultText = literalExpression "<name>";
130            description = "Name of the action.";
131          };
132          options.exec = mkOption {
133            type = types.nullOr types.str;
134            description = "Program to execute, possibly with arguments.";
135            default = null;
136          };
137          options.icon = mkOption {
138            type = with types; nullOr (either str path);
139            default = null;
140            description = "Icon to display in file manager, menus, etc.";
141          };
142        }));
143        default = { };
144        defaultText = literalExpression "{ }";
145        example = literalExpression ''
146          {
147            "New Window" = {
148              exec = "''${pkgs.firefox}/bin/firefox --new-window %u";
149            };
150          }
151        '';
152        description =
153          "The set of actions made available to application launchers.";
154      };
155
156      # Required for the assertions
157      # TODO: Remove me once https://github.com/NixOS/nixpkgs/issues/96006 is fixed
158      assertions = mkOption {
159        type = types.listOf types.unspecified;
160        default = [ ];
161        visible = false;
162        internal = true;
163      };
164    };
165  };
166
167  #passes config options to makeDesktopItem in expected format
168  makeFile = name: config:
169    pkgs.makeDesktopItem {
170      inherit name;
171      inherit (config)
172        type exec icon comment terminal genericName startupNotify noDisplay
173        prefersNonDefaultGPU actions;
174      desktopName = config.name;
175      mimeTypes = optionals (config.mimeType != null) config.mimeType;
176      categories = optionals (config.categories != null) config.categories;
177      extraConfig = config.settings;
178    };
179in {
180  meta.maintainers = [ hm.maintainers.cwyc ];
181
182  options.xdg.desktopEntries = mkOption {
183    description = ''
184      Desktop Entries allow applications to be shown in your desktop environment's app launcher.
185
186      You can define entries for programs without entries or override existing entries.
187
188      See <https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#recognized-keys> for more information on options.
189    '';
190    default = { };
191    type = types.attrsOf (types.submodule desktopEntry);
192    example = literalExpression ''
193      {
194        firefox = {
195          name = "Firefox";
196          genericName = "Web Browser";
197          exec = "firefox %U";
198          terminal = false;
199          categories = [ "Application" "Network" "WebBrowser" ];
200          mimeType = [ "text/html" "text/xml" ];
201        };
202      }
203    '';
204  };
205
206  config = mkIf (config.xdg.desktopEntries != { }) {
207    assertions = [
208      (hm.assertions.assertPlatform "xdg.desktopEntries" pkgs platforms.linux)
209    ] ++ flatten (catAttrs "assertions" (attrValues config.xdg.desktopEntries));
210
211    home.packages = (map hiPrio # we need hiPrio to override existing entries
212      (attrsets.mapAttrsToList makeFile config.xdg.desktopEntries));
213  };
214
215}