{ 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; }; }; }