diff options
Diffstat (limited to 'home-manager/xdg-mime.nix')
-rw-r--r-- | home-manager/xdg-mime.nix | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/home-manager/xdg-mime.nix b/home-manager/xdg-mime.nix new file mode 100644 index 0000000..420510f --- /dev/null +++ b/home-manager/xdg-mime.nix @@ -0,0 +1,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; + }; + }; +} |