summaryrefslogtreecommitdiff
path: root/home-manager/xdg-mime.nix
blob: 420510f0f865621d3d25cbed5eb305658f041c16 (plain)
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
{ config, lib, pkgs, ... }: let
	cfg = config.xdg.mimeApps._defaultCategoryApplications;

	# Mime-type category files are stored here
	mimeResources = ./resources/xdg-mime;

	# Files present in mimeResources with newline separated mime-types
	# Would be more fun to load these dynamically with builtins.readDir, but I will be disciplined
	categories = [ "audio" "email" "image" "pdf" "text" "video" ];

	# Dynamically generate categories
	categoryApplications = (lib.genAttrs categories (category: lib.mkOption {
		type = lib.types.listOf lib.types.str;
		default = [ ];
		description = "set the default application used for ${category} files. omit '.desktop'";
	}));

	# Create list of mime-types from the category file
	getCategoryMimeTypes = category: lib.filter (s: s != "") (lib.splitString "\n" (builtins.readFile "${mimeResources}/${category}"));

	# Structure default applications for a specified category how home-manager expects
	assignMimeTypes = applications: category: lib.map (mimetype:
		{ "${mimetype}" = lib.mkIf (applications != []) (lib.map (app: "${app}.desktop") applications); }
	) (getCategoryMimeTypes category);

	# Combine all category applications
	defaultApplications = lib.mkMerge (lib.flatten (
		lib.map (category: assignMimeTypes cfg.categoryApplications.${category} category) categories
	));
in {
	options.xdg.mimeApps._defaultCategoryApplications = {
		enable = lib.mkEnableOption "set default applications as defined in the resources dir";
		inherit categoryApplications;
	};

	config = lib.mkIf cfg.enable {
		xdg.mimeApps = {
			enable = lib.mkDefault true;
			inherit defaultApplications;
		};
	};
}