stockholm/lass/2configs/xdg-open.nix

68 lines
1.6 KiB
Nix
Raw Normal View History

2019-04-17 18:12:01 +00:00
{ config, pkgs, lib, ... }: with import <stockholm/lib>; let
xdg-open-wrapper = pkgs.writeDashBin "xdg-open" ''
exec ${xdg-open}/bin/xdg-open "$@" >> /tmp/xdg-debug.log 2>&1
2019-04-17 18:12:01 +00:00
'';
xdg-open = pkgs.writeBashBin "xdg-open" ''
set -xe
2019-04-17 18:12:01 +00:00
FILE="$1"
PATH=/run/current-system/sw/bin
2019-04-17 18:12:01 +00:00
mime=
case "$FILE" in
http://*|https://*)
mime=text/html
;;
mailto:*)
mime=special/mailaddress
;;
magnet:*)
mime=application/x-bittorrent
;;
irc:*)
mime=x-scheme-handler/irc
;;
*)
# its a file
# strip possible protocol
FILE=''${FILE#file://}
mime=''$(file -E --brief --mime-type "$FILE") \
|| (echo "$mime" 1>&2; exit 1)
# ^ echo the error message of file
;;
esac
case "$mime" in
special/mailaddress)
alacritty --execute vim "$FILE" ;;
2019-04-17 18:12:01 +00:00
text/html)
firefox "$FILE" ;;
2019-04-17 18:12:01 +00:00
text/xml)
firefox "$FILE" ;;
2019-04-17 18:12:01 +00:00
text/*)
alacritty --execute vim "$FILE" ;;
2019-04-17 18:12:01 +00:00
image/*)
sxiv "$FILE" ;;
application/x-bittorrent)
env DISPLAY=:0 transgui "$FILE" ;;
application/pdf)
zathura "$FILE" ;;
inode/directory)
alacritty --execute mc "$FILE" ;;
2019-04-17 18:12:01 +00:00
*)
# open dmenu and ask for program to open with
runner=$(print -rC1 -- ''${(ko)commands} | dmenu)
exec $runner "$FILE";;
2019-04-17 18:12:01 +00:00
esac
'';
in {
environment.systemPackages = [ xdg-open-wrapper ];
security.sudo.extraConfig = ''
cr ALL=(lass) NOPASSWD: ${xdg-open}/bin/xdg-open *
ff ALL=(lass) NOPASSWD: ${xdg-open}/bin/xdg-open *
2019-04-17 18:12:01 +00:00
'';
}