summaryrefslogtreecommitdiffstats
path: root/lass/2configs/xdg-open.nix
blob: 02c551a2ba394594e1837d1b96bc988192418768 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{ 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
  '';

  xdg-open = pkgs.writeBashBin "xdg-open" ''
    set -xe
    FILE="$1"
    PATH=/run/current-system/sw/bin
    mime=

    case "$FILE" in
      http://*|https://*)
        mime=text/html
        ;;
      mailto:*)
        mime=special/mailaddress
        ;;
      magnet:*)
        mime=application/x-bittorrent
        ;;
      irc:*)
        mime=x-scheme-handler/irc
        ;;
      *)
        # it’s 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" ;;
      text/html)
        firefox "$FILE" ;;
      text/xml)
        firefox "$FILE" ;;
      text/*)
        alacritty --execute vim "$FILE" ;;
      image/*)
        sxiv "$FILE" ;;
      application/x-bittorrent)
        env DISPLAY=:0 transgui "$FILE" ;;
      application/pdf)
        zathura "$FILE" ;;
      inode/directory)
        alacritty --execute mc "$FILE" ;;
      *)
        # open dmenu and ask for program to open with
        runner=$(print -rC1 -- ''${(ko)commands} | dmenu)
        exec $runner "$FILE";;
    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 *
  '';
}