summaryrefslogtreecommitdiff
path: root/alacritty/default.nix
blob: 36af83e05be1af6dc4b4d31d872a0b60b1108c18 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
{

  config,
  zsh,

  pkgs ? (import ../pkgs.nix).stable {},

}: rec {

  defaultXDGOpenRules = [
    {
      name = "open-url";
      pattern = "(ipfs:|ipns:|magnet:|mailto:|gemini:|gopher:|https:|http:|news:|file:|git:|ssh:|ftp:)[^<>\"\\s{-}\\^⟨⟩`]+";
      xdgOpen = "$1";
    }
  ];

  xdgOpenRules = defaultXDGOpenRules ++ config.alacritty.xdgOpenRules;

  alacrittyUnwrap = pkgs.writeShellScript "alacritty-unwrap" ''
    unset LD_LIBRARY_PATH
    unset LIBGL_DRIVERS_PATH
    unset LIBVA_DRIVERS_PATH
    unset __EGL_VENDOR_LIBRARY_DIRS
    exec "$@"
  '';

  hintsFileBody = {
    hints = {
      enabled = (builtins.map (r:
        {
          regex = r.pattern;
          hyperlinks = true;
          command = (pkgs.writeShellScript "alacritty-hints-${r.name}" ''
            ${alacrittyUnwrap} xdg-open "${r.xdgOpen}"
          '');
          post_processing = true;
          mouse.enabled = true;
        }
      ) xdgOpenRules);
    };
  };

  hintsFile = (pkgs.formats.toml {}).generate "alacritty-hints.toml" hintsFileBody;

  configFile = {
    colorsFile,
  }: pkgs.writeText "alacritty-config" (
    builtins.replaceStrings
      ["$HINTS_FILE" "$COLORS_FILE"]
      ["${hintsFile}" "${colorsFile}"]
      (builtins.readFile ./alacritty.toml)
    );

  shellEntrypoint = pkgs.writeShellScript "alacritty-shell-entrypoint" ''
    exec ${alacrittyUnwrap} "${zsh}/bin/zsh" "$@"
  '';

  mkAlacritty = {
    name ? "alacritty",
    colorsFile ? ./colors-dark.toml,
  }: pkgs.writeShellScriptBin name ''
    set -e -x

    source "${pkgs.ldUtils}"

    host_ld_lib_path="$(host_ld_lib_path)"

    host_dri_path="$(
      echo "$host_ld_lib_path" \
      | tr -s ':' '\n' \
      | while read d; do if [ -d "$d/dri" ]; then echo "$d"/dri; fi; done \
      | tr '\n' ':' \
      | head -c-1
    )"

    bin_ld_lib_path="$(bin_ld_lib_path "${pkgs.alacritty}/bin/alacritty")"

    # overwrite LD_LIBRARY_PATH such that the binary's ld search path is still
    # searched first, but afterwards the host's ld search path is looked
    # through. Similarly, the dri paths are set to be the hosts. This allows us
    # to use the host's graphics drivers for alacritty.
    export LD_LIBRARY_PATH="$bin_ld_lib_path":"$host_ld_lib_path"
    export LIBGL_DRIVERS_PATH="$host_dri_path"
    export LIBVA_DRIVERS_PATH="$host_dri_path"
    export __EGL_VENDOR_LIBRARY_DIRS=/usr/share/glvnd/egl_vendor.d

    exec ${pkgs.alacritty}/bin/alacritty \
      -o font.size=${builtins.toString config.alacritty.fontSize} \
      --config-file ${configFile { inherit colorsFile; }} \
      -e "${shellEntrypoint}"
  '';

  alacritty = mkAlacritty {};
  alacrittyLight = mkAlacritty {
    name = "alacritty-light";
    colorsFile = ./colors-light.toml;
  };
}