1{
2 config,
3 lib,
4 pkgs,
5 ...
6}: let
7 cfg = config.myHome.profiles.defaultApps;
8 mimeTypes = import ./mimeTypes.nix;
9in {
10 options.myHome.profiles.defaultApps = {
11 enable = lib.mkEnableOption "enforce default applications";
12 forceMimeAssociations = lib.mkEnableOption "force mime associations for defaultApps";
13
14 archiveViewer = {
15 package = lib.mkOption {
16 type = lib.types.package;
17 default = pkgs.nemo;
18 description = "The default archive viewer package.";
19 };
20
21 exec = lib.mkOption {
22 type = lib.types.str;
23 default = lib.getExe cfg.archiveViewer.package;
24 description = "The executable path for the default archive viewer.";
25 };
26
27 icon = lib.mkOption {
28 type = lib.types.str;
29 default = "";
30 description = "The icon name for the default archive viewer.";
31 };
32 };
33
34 audioPlayer = {
35 package = lib.mkOption {
36 type = lib.types.package;
37 default = pkgs.celluloid;
38 description = "The default audio player package.";
39 };
40
41 exec = lib.mkOption {
42 type = lib.types.str;
43 default = lib.getExe cfg.audioPlayer.package;
44 description = "The executable path for the default audio player.";
45 };
46
47 terminal = lib.mkOption {
48 type = lib.types.bool;
49 default = false;
50 description = "Whether the editor is a terminal-based application.";
51 };
52
53 icon = lib.mkOption {
54 type = lib.types.str;
55 default = "";
56 description = "The icon name for the default audio player.";
57 };
58 };
59
60 editor = {
61 package = lib.mkOption {
62 type = lib.types.package;
63 default = pkgs.gnome-text-editor;
64 description = "The default text editor package.";
65 };
66
67 exec = lib.mkOption {
68 type = lib.types.str;
69 default = lib.getExe cfg.editor.package;
70 description = "The executable path for the default text editor.";
71 };
72
73 terminal = lib.mkOption {
74 type = lib.types.bool;
75 default = false;
76 description = "Whether the editor is a terminal-based application.";
77 };
78
79 icon = lib.mkOption {
80 type = lib.types.str;
81 default = "";
82 description = "The icon name for the default text editor.";
83 };
84 };
85
86 fileManager = {
87 package = lib.mkOption {
88 type = lib.types.package;
89 default = pkgs.nemo;
90 description = "The default file manager package.";
91 };
92
93 exec = lib.mkOption {
94 type = lib.types.str;
95 default = lib.getExe cfg.fileManager.package;
96 description = "The executable path for the default file manager.";
97 };
98
99 terminal = lib.mkOption {
100 type = lib.types.bool;
101 default = false;
102 description = "Whether the editor is a terminal-based application.";
103 };
104
105 icon = lib.mkOption {
106 type = lib.types.str;
107 default = "";
108 description = "The icon name for the default file manager.";
109 };
110 };
111
112 imageViewer = {
113 package = lib.mkOption {
114 type = lib.types.package;
115 default = pkgs.eog;
116 description = "The default image viewer package.";
117 };
118
119 exec = lib.mkOption {
120 type = lib.types.str;
121 default = lib.getExe cfg.imageViewer.package;
122 description = "The executable path for the default image viewer.";
123 };
124
125 icon = lib.mkOption {
126 type = lib.types.str;
127 default = "";
128 description = "The icon name for the default image viewer.";
129 };
130 };
131
132 pdfViewer = {
133 package = lib.mkOption {
134 type = lib.types.package;
135 default = pkgs.papers;
136 description = "The default PDF viewer package.";
137 };
138
139 exec = lib.mkOption {
140 type = lib.types.str;
141 default = lib.getExe cfg.pdfViewer.package;
142 description = "The executable path for the default PDF viewer.";
143 };
144
145 terminal = lib.mkOption {
146 type = lib.types.bool;
147 default = false;
148 description = "Whether the editor is a terminal-based application.";
149 };
150
151 icon = lib.mkOption {
152 type = lib.types.str;
153 default = "";
154 description = "The icon name for the default PDF viewer.";
155 };
156 };
157
158 terminal = {
159 package = lib.mkOption {
160 type = lib.types.package;
161 default = pkgs.ghostty;
162 description = "The default terminal emulator package.";
163 };
164
165 exec = lib.mkOption {
166 type = lib.types.str;
167 default = lib.getExe cfg.terminal.package;
168 description = "The executable path for the default terminal emulator.";
169 };
170
171 icon = lib.mkOption {
172 type = lib.types.str;
173 default = "";
174 description = "The icon name for the default terminal emulator.";
175 };
176 };
177
178 terminalEditor = {
179 package = lib.mkOption {
180 type = lib.types.package;
181 default = pkgs.neovim;
182 description = "The default terminal text editor package.";
183 };
184
185 exec = lib.mkOption {
186 type = lib.types.str;
187 default = lib.getExe cfg.terminalEditor.package;
188 description = "The executable path for the default terminal text editor.";
189 };
190 };
191
192 videoPlayer = {
193 package = lib.mkOption {
194 type = lib.types.package;
195 default = pkgs.celluloid;
196 description = "The default video player package.";
197 };
198
199 exec = lib.mkOption {
200 type = lib.types.str;
201 default = lib.getExe cfg.videoPlayer.package;
202 description = "The executable path for the default video player.";
203 };
204
205 icon = lib.mkOption {
206 type = lib.types.str;
207 default = "";
208 description = "The icon name for the default video player.";
209 };
210 };
211
212 webBrowser = {
213 package = lib.mkOption {
214 type = lib.types.package;
215 default = config.programs.firefox.finalPackage;
216 description = "The default web browser package.";
217 };
218
219 exec = lib.mkOption {
220 type = lib.types.str;
221 default = lib.getExe cfg.webBrowser.package;
222 description = "The executable path for the default web browser.";
223 };
224
225 icon = lib.mkOption {
226 type = lib.types.str;
227 default = lib.getIcon cfg.webBrowser.package;
228 description = "The icon name for the default web browser.";
229 };
230 };
231 };
232
233 config = lib.mkIf cfg.enable {
234 dconf = {
235 enable = true;
236
237 settings = {
238 "org/cinnamon/desktop/applications/terminal".exec = "${cfg.terminal.exec}";
239 "org/cinnamon/desktop/default-applications/terminal".exec = "${cfg.terminal.exec}";
240 };
241 };
242
243 home = {
244 packages = with cfg; [
245 audioPlayer.package
246 editor.package
247 fileManager.package
248 imageViewer.package
249 pdfViewer.package
250 terminal.package
251 terminalEditor.package
252 videoPlayer.package
253 webBrowser.package
254 ];
255
256 sessionVariables = {
257 BROWSER = "${builtins.baseNameOf cfg.webBrowser.exec}";
258 EDITOR = "${builtins.baseNameOf cfg.terminalEditor.exec}";
259 TERMINAL = "${builtins.baseNameOf cfg.terminal.exec}";
260 };
261 };
262
263 xdg = {
264 configFile = {
265 "xfce4/helpers.rc".text = ''
266 FileManager=${builtins.baseNameOf cfg.fileManager.exec}
267 TerminalEmulator=${builtins.baseNameOf cfg.terminal.exec}
268 WebBrowser=${builtins.baseNameOf cfg.webBrowser.exec}
269 '';
270 "mimeapps.list" = lib.mkIf cfg.forceMimeAssociations {
271 force = true;
272 };
273 };
274
275 mimeApps = lib.mkIf cfg.forceMimeAssociations {
276 enable = true;
277
278 defaultApplications = let
279 mkDefaults = files: desktopFile: lib.genAttrs files (_: [desktopFile]);
280 audioTypes = mkDefaults mimeTypes.audioFiles "defaultAudioPlayer.desktop";
281
282 browserTypes = mkDefaults mimeTypes.browserFiles "defaultWebBrowser.desktop";
283
284 documentTypes = mkDefaults mimeTypes.documentFiles "defaultPdfViewer.desktop";
285
286 editorTypes = mkDefaults mimeTypes.editorFiles "defaultEditor.desktop";
287
288 folderTypes = {
289 "inode/directory" = "defaultFileManager.desktop";
290 };
291
292 imageTypes = mkDefaults mimeTypes.imageFiles "defaultImageViewer.desktop";
293
294 videoTypes = mkDefaults mimeTypes.videoFiles "defaultVideoPlayer.desktop";
295
296 archiveTypes = mkDefaults mimeTypes.archiveFiles "defaultArchiveViewer.desktop";
297 in
298 audioTypes
299 // browserTypes
300 // documentTypes
301 // editorTypes
302 // folderTypes
303 // imageTypes
304 // videoTypes
305 // archiveTypes;
306 };
307
308 desktopEntries = let
309 mkDefaultEntry = name: exec: terminal: icon: {
310 exec = "${exec} %U";
311 icon =
312 if icon != ""
313 then icon
314 else "${builtins.baseNameOf exec}";
315 name = "Default ${name}";
316 inherit terminal;
317
318 settings = {
319 NoDisplay = "true";
320 };
321 };
322 in
323 lib.mkIf cfg.forceMimeAssociations {
324 defaultAudioPlayer =
325 mkDefaultEntry "Audio Player" cfg.audioPlayer.exec cfg.audioPlayer.terminal cfg.audioPlayer.icon;
326 defaultEditor =
327 mkDefaultEntry "Editor" cfg.editor.exec cfg.editor.terminal cfg.editor.icon;
328 defaultFileManager =
329 mkDefaultEntry "File Manager" cfg.fileManager.exec cfg.fileManager.terminal cfg.fileManager.icon;
330 defaultImageViewer =
331 mkDefaultEntry "Image Viewer" cfg.imageViewer.exec false cfg.imageViewer.icon;
332 defaultPdfViewer =
333 mkDefaultEntry "PDF Viewer" cfg.pdfViewer.exec cfg.pdfViewer.terminal cfg.pdfViewer.icon;
334 defaultVideoPlayer =
335 mkDefaultEntry "Video Player" cfg.videoPlayer.exec false cfg.videoPlayer.icon;
336 defaultWebBrowser =
337 mkDefaultEntry "Web Browser" cfg.webBrowser.exec false cfg.webBrowser.icon;
338 defaultArchiveViewer =
339 mkDefaultEntry "Archive Viewer" cfg.archiveViewer.exec false cfg.archiveViewer.icon;
340 };
341 };
342 };
343}