Skip to content

Instantly share code, notes, and snippets.

@ryantrinkle
Created January 22, 2022 14:08
Show Gist options
  • Save ryantrinkle/61b443fcdeb986fbf58aa8e71dcc5d9a to your computer and use it in GitHub Desktop.
Save ryantrinkle/61b443fcdeb986fbf58aa8e71dcc5d9a to your computer and use it in GitHub Desktop.
let # invert is O(n^2); it's not obvious to me that we can do much better without a lot of work
# invert :: Map a [b] -> Map b [a]
invert = m:
let allBs = builtins.concatLists (builtins.attrValues m);
exploded = builtins.concatMap (a: map (b: { ${b} = a; }) m.${a}) (builtins.attrNames m);
in builtins.listToAttrs (map (b: { name = b; value = builtins.catAttrs b exploded; }) allBs);
# Given a package that installs .desktop files in the usual location,
# return a mapping from mime types to lists of desktop file names.
# This is suitable for use in home-manager's
# xdg.mimeApps.defaultApplications.
getAssociations = p: invert (builtins.fromJSON (builtins.readFile (pkgs.runCommand "getAssociations" {
inherit p;
} ''
for x in $(ls "$p/share/applications/") ; do
cat "$p/share/applications/$x" | sed -n 's/MimeType=//p' | sed -e 's/;$//' -e 's/;/", "/g' -e 's/^\(.*\)$/"'"$x"'": ["\1"],/'
done | sed -e '1 i {' -e '$ s/,$//' -e '$ a }' >$out
'')));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment