From 5b5eb10c5985d21f5ae3a1d84ff3e3ecfce10ea5 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sat, 30 Aug 2025 16:38:19 -0500 Subject: allow setting default application for generic categories of mime-types instead of the mimewiz like method --- home-manager/xdg-mime.nix | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'home-manager/xdg-mime.nix') diff --git a/home-manager/xdg-mime.nix b/home-manager/xdg-mime.nix index 043b214..420510f 100644 --- a/home-manager/xdg-mime.nix +++ b/home-manager/xdg-mime.nix @@ -1,22 +1,41 @@ { config, lib, pkgs, ... }: let - cfg = config.xdg.mimeApps._setDefaultApplications; - mimeDir = ./resources/xdg-mime; + cfg = config.xdg.mimeApps._defaultCategoryApplications; - getApplicationMimeTypes = applicationName: lib.filter (s: s != "") (lib.splitString "\n" (builtins.readFile "${mimeDir}/${applicationName}")); + # Mime-type category files are stored here + mimeResources = ./resources/xdg-mime; - getMimeTypeApplicationList = applicationName: lib.mkMerge (lib.map (mimetype: - { "${mimetype}" = [ "${applicationName}.desktop" ]; } - ) (getApplicationMimeTypes applicationName)); + # 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" ]; - defaultApplications = lib.mkMerge (lib.mapAttrsToList (appPath: pathType: lib.mkIf (pathType == "regular") (getMimeTypeApplicationList appPath)) (builtins.readDir mimeDir)); + # 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._setDefaultApplications = { + 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 = true; + enable = lib.mkDefault true; inherit defaultApplications; }; }; -- cgit v1.2.3