aboutsummaryrefslogtreecommitdiff
path: root/pkgs/weznix.nix
blob: 5339e32b0612af9c3107728e5490eb2ba147bc13 (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
100
101
102
103
104
105
106
107
108
109
110
{
  wezterm,
  makeWrapper,
  writeText,
  nixToLua,
  lib,
}:
{
  configDirectory ? "~/.config/wezterm",
  wrapConfig ? "WEZDEV",
  luaPath,
  mainFile ? "init",
  extraLuaInit ? "",
  extraWrapperArgs ? [ ],
  extraPATH ? [ ],
  pass ? { },
}:
let
  inherit (nixToLua) toLua;
  passables = {
    inherit configDirectory wrapConfig luaPath;
    wezterm = builtins.toString wezterm;
    mainFile =
      if (mainFile == "wezterm" || mainFile == "weznix") then
        throw "mainFile cannot be named 'wezterm' or 'weznix'"
      else
        mainFile;
  }
  // pass;

  wezinit =
    writeText "wezterm.lua" # lua
      ''
        package.preload["weznix"] = function()
          return ${toLua passables}
        end

        local weznix = require('weznix')

        local cfgDir = weznix.luaPath
        if (weznix.wrapConfig == false) or (type(weznix.wrapConfig) == "string" and os.getenv(weznix.wrapConfig) == "1") then
          cfgDir = weznix.configDirectory
        end

        local wezterm = require('wezterm')
        wezterm.config_dir = cfgDir

        package.path = cfgDir .. '/?.lua'
        package.cpath = package.cpath .. ';' .. cfgDir .. '/?.so'

        ${extraLuaInit}

        return require(weznix.mainFile)
      '';

  wrapperArgs = [
    "--set"
    "WEZTERM_CONFIG_FILE"
    (builtins.toString wezinit)
  ]
  ++ lib.optionals (extraPATH != [ ]) [
    "--prefix"
    "PATH"
    ":"
    (lib.makeBinPath extraPATH)
  ]
  ++ extraWrapperArgs;

in
wezterm.overrideAttrs (
  {
    nativeBuildInputs ? [ ],
    ...
  }:
  {
    nativeBuildInputs = nativeBuildInputs ++ [ makeWrapper ];

    separateDebugInfo = false;

    buildCommand = /* bash */ ''
      ${lib.concatMapStringsSep "\n" (outputName: ''
        echo "Copying output ${outputName}"
        set -x
        cp -rs --no-preserve=mode "${wezterm.${outputName}}" "''$${outputName}"
        set +x
      '') (wezterm.outputs or [ "out" ])}


      shopt -s nullglob
      for bin in ${wezterm.out}/bin/wezterm*; do
        local prog="$(basename "$bin")"
        rm "$out/bin/$prog"
        makeWrapper "$bin" "$out/bin/$prog" ${lib.escapeShellArgs wrapperArgs}
      done

      # If .desktop files refer to the old package, replace the references
      for dsk in "$out/share/applications"/*.desktop ; do
        if ! grep -q "${wezterm.out}" "$dsk"; then
          continue
        fi
        src="$(readlink "$dsk")"
        rm "$dsk"
        sed "s|${wezterm.out}|$out|g" "$src" > "$dsk"
      done

      shopt -u nullglob

    '';
  }
)