2024-02-11 23:31:43 +00:00
|
|
|
# TODO: move into modules/users.nix
|
|
|
|
{ config, lib, pkgs, ...}:
|
2023-01-28 09:22:11 +00:00
|
|
|
|
2023-07-15 10:04:22 +00:00
|
|
|
let
|
2023-12-11 03:03:22 +00:00
|
|
|
# [ ProgramConfig ]
|
|
|
|
enabledPrograms = builtins.filter
|
|
|
|
(p: p.enabled)
|
|
|
|
(builtins.attrValues config.sane.programs);
|
|
|
|
|
2024-02-11 23:31:43 +00:00
|
|
|
# [ ProgramConfig ]
|
|
|
|
enabledProgramsWithPackage = builtins.filter (p: p.package != null) enabledPrograms;
|
|
|
|
|
2023-12-11 03:03:22 +00:00
|
|
|
# [ { "<mime-type>" = { prority, desktop } ]
|
|
|
|
enabledWeightedMimes = builtins.map weightedMimes enabledPrograms;
|
|
|
|
|
2023-07-15 10:04:22 +00:00
|
|
|
# ProgramConfig -> { "<mime-type>" = { priority, desktop }; }
|
2023-12-11 03:03:22 +00:00
|
|
|
weightedMimes = prog: builtins.mapAttrs
|
|
|
|
(_key: desktop: {
|
|
|
|
priority = prog.mime.priority; desktop = desktop;
|
|
|
|
})
|
|
|
|
prog.mime.associations;
|
|
|
|
|
2023-07-15 10:04:22 +00:00
|
|
|
# [ { "<mime-type>" = { priority, desktop } ]; } ] -> { "<mime-type>" = [ { priority, desktop } ... ]; }
|
|
|
|
mergeMimes = mimes: lib.foldAttrs (item: acc: [item] ++ acc) [] mimes;
|
2023-12-11 03:03:22 +00:00
|
|
|
|
2023-07-15 10:04:22 +00:00
|
|
|
# [ { priority, desktop } ... ] -> Self
|
2023-12-11 03:03:22 +00:00
|
|
|
sortOneMimeType = associations: builtins.sort
|
|
|
|
(l: r: assert l.priority != r.priority; l.priority < r.priority)
|
|
|
|
associations;
|
2023-07-15 10:04:22 +00:00
|
|
|
sortMimes = mimes: builtins.mapAttrs (_k: sortOneMimeType) mimes;
|
2024-02-11 23:31:43 +00:00
|
|
|
# { "<mime-type>"} = [ { priority, desktop } ... ]; } -> { "<mime-type>" = [ "<desktop>" ... ]; }
|
2023-12-11 03:03:22 +00:00
|
|
|
removePriorities = mimes: builtins.mapAttrs
|
|
|
|
(_k: associations: builtins.map (a: a.desktop) associations)
|
|
|
|
mimes;
|
2024-02-11 23:31:43 +00:00
|
|
|
# { "<mime-type>" = [ "<desktop>" ... ]; } -> { "<mime-type>" = "<desktop1>;<desktop2>;..."; }
|
|
|
|
formatDesktopLists = mimes: builtins.mapAttrs
|
|
|
|
(_k: desktops: lib.concatStringsSep ";" desktops)
|
|
|
|
mimes;
|
|
|
|
|
|
|
|
mimeappsListPkg = pkgs.writeTextDir "share/applications/mimeapps.list" (
|
|
|
|
lib.generators.toINI { } {
|
|
|
|
"Default Applications" = formatDesktopLists (removePriorities (sortMimes (mergeMimes enabledWeightedMimes)));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
localShareApplicationsPkg = (pkgs.symlinkJoin {
|
|
|
|
name = "user-local-share-applications";
|
|
|
|
paths = builtins.map
|
|
|
|
(p: "${p.package}")
|
|
|
|
(enabledProgramsWithPackage ++ [ { package=mimeappsListPkg; } ]);
|
|
|
|
}).overrideAttrs (orig: {
|
|
|
|
# like normal symlinkJoin, but don't error if the path doesn't exist
|
|
|
|
buildCommand = ''
|
|
|
|
mkdir -p $out/share/applications
|
|
|
|
for i in $(cat $pathsPath); do
|
|
|
|
if [ -e "$i/share/applications" ]; then
|
2024-02-11 23:44:55 +00:00
|
|
|
${pkgs.buildPackages.xorg.lndir}/bin/lndir -silent $i/share/applications $out/share/applications
|
2024-02-11 23:31:43 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
runHook postBuild
|
|
|
|
'';
|
|
|
|
postBuild = ''
|
|
|
|
# rebuild `mimeinfo.cache`, used by file openers to show the list of *all* apps, not just the user's defaults.
|
|
|
|
${pkgs.buildPackages.desktop-file-utils}/bin/update-desktop-database $out/share/applications
|
|
|
|
'';
|
|
|
|
});
|
|
|
|
|
2023-07-15 10:04:22 +00:00
|
|
|
in
|
2023-01-28 09:22:11 +00:00
|
|
|
{
|
|
|
|
# the xdg mime type for a file can be found with:
|
|
|
|
# - `xdg-mime query filetype path/to/thing.ext`
|
2023-07-16 12:17:54 +00:00
|
|
|
# the default handler for a mime type can be found with:
|
|
|
|
# - `xdg-mime query default <mimetype>` (e.g. x-scheme-handler/http)
|
2024-01-24 05:00:35 +00:00
|
|
|
# the nix-configured handler can be found `nix-repl > :lf . > hostConfigs.desko.xdg.mime.defaultApplications`
|
2023-07-16 12:17:54 +00:00
|
|
|
#
|
2024-02-11 23:31:43 +00:00
|
|
|
# glib/gio is queried via glib.bin output:
|
|
|
|
# - `gio mime x-scheme-handler/https`
|
|
|
|
# - `gio open <path_or_url>`
|
|
|
|
#
|
2023-01-28 09:26:29 +00:00
|
|
|
# we can have single associations or a list of associations.
|
|
|
|
# there's also options to *remove* [non-default] associations from specific apps
|
2024-02-11 23:31:43 +00:00
|
|
|
# N.B.: don't use nixos' `xdg.mime` option becaue that caues `/share/applications` to be linked into the whole system,
|
|
|
|
# which limits what i can do around sandboxing. getting the default associations to live in ~/ makes it easier to expose
|
|
|
|
# the associations to apps selectively.
|
|
|
|
# xdg.mime.enable = true;
|
|
|
|
# xdg.mime.defaultApplications = removePriorities (sortMimes (mergeMimes enabledWeightedMimes));
|
|
|
|
|
|
|
|
sane.user.fs.".local/share/applications".symlink.target = "${localShareApplicationsPkg}/share/applications";
|
2023-01-28 09:22:11 +00:00
|
|
|
}
|