aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/programs/irssi.nix
blob: fc8fa8e6132cab41184422185c438a5761a7f977 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.programs.irssi;

  boolStr = b: if b then "yes" else "no";
  quoteStr = s: escape ["\""] s;

  assignFormat = set:
    concatStringsSep "\n"
      (mapAttrsToList (k: v: "  ${k} = \"${quoteStr v}\";") set);

  chatnetString =
    concatStringsSep "\n"
      (flip mapAttrsToList cfg.networks
      (k: v: ''
        ${k} = {
          type = "${v.type}";
          nick = "${quoteStr v.nick}";
          autosendcmd = "${concatMapStringsSep ";" quoteStr v.autoCommands}";
        };
      ''));

  serversString =
    concatStringsSep ",\n"
      (flip mapAttrsToList cfg.networks
      (k: v: ''
        {
          chatnet = "${k}";
          address = "${v.server.address}";
          port = "${toString v.server.port}";
          use_ssl = "${boolStr v.server.ssl.enable}";
          ssl_verify = "${boolStr v.server.ssl.verify}";
          autoconnect = "${boolStr v.server.autoConnect}";
        }
      ''));

  channelString =
    concatStringsSep ",\n"
      (flip mapAttrsToList cfg.networks
      (k: v:
        concatStringsSep ",\n"
          (flip mapAttrsToList v.channels
          (c: cv: ''
            {
              chatnet = "${k}";
              name = "${c}";
              autojoin = "${boolStr cv.autoJoin}";
            }
          ''))));

  channelType = types.submodule {
    options = {
      name = mkOption {
        type = types.nullOr types.str;
        visible = false;
        default = null;
        description = "Name of the channel.";
      };

      autoJoin = mkOption {
        type = types.bool;
        default = false;
        description = "Whether to join this channel on connect.";
      };
    };
  };

  networkType = types.submodule ({ name, ...}: {
    options = {
      name = mkOption {
        visible = false;
        default = name;
        type = types.str;
      };

      nick = mkOption {
        type = types.str;
        description = "Nickname in that network.";
      };

      type = mkOption {
        type = types.str;
        description = "Type of the network.";
        default = "IRC";
      };

      autoCommands = mkOption {
        type = types.listOf types.str;
        default = [];
        description = "List of commands to execute on connect.";
      };

      server = {
        address = mkOption {
          type = types.str;
          description = "Address of the chat server.";
        };

        port = mkOption {
          type = types.port;
          default = 6667;
          description = "Port of the chat server.";
        };

        ssl = {
          enable = mkOption {
            type = types.bool;
            default = true;
            description = "Whether SSL should be used.";
          };

          verify = mkOption {
            type = types.bool;
            default = true;
            description = "Whether the SSL certificate should be verified.";
          };
        };

        autoConnect = mkOption {
          type = types.bool;
          default = false;
          description = "Whether Irssi connects to the server on launch.";
        };
      };

      channels = mkOption {
        description = "Channels for the given network.";
        type = types.attrsOf channelType;
        default = {};
      };
    };
  });

in

{

  options = {
    programs.irssi = {
      enable = mkEnableOption "the Irssi chat client";

      extraConfig = mkOption {
        default = "";
        description = "These lines are appended to the Irssi configuration.";
        type = types.str;
      };

      aliases = mkOption {
        default = {};
        example = { J = "join"; BYE = "quit";};
        description = "An attribute set that maps aliases to commands.";
        type = types.attrsOf types.str;
      };

      networks = mkOption {
        default = {};
        example = literalExample ''
          {
            freenode = {
              nick = "hmuser";
              server = {
                address = "chat.freenode.net";
                port = 6697;
                autoConnect = true;
              };
              channels = {
                nixos.autoJoin = true;
              };
            };
          }
        '';
        description = "An attribute set of chat networks.";
        type = types.attrsOf networkType;
      };
    };
  };

  config = mkIf cfg.enable {
    home.packages = [ pkgs.irssi ];

    home.file.".irssi/config".text = ''
      settings = {
        core = {
          settings_autosave = "no";
        };
      };

      aliases = {
      ${assignFormat cfg.aliases}
      };

      chatnets = {
      ${chatnetString}
      };

      servers = (
      ${serversString}
      );

      channels = (
      ${channelString}
      );

      ${cfg.extraConfig}
    '';
  };
}