fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
5.6 KiB236 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.irssi;
8
9  quoteStr = s: escape [ ''"'' ] s;
10
11  # Comma followed by newline.
12  cnl = ''
13    ,
14  '';
15
16  assignFormat = set:
17    concatStringsSep "\n"
18    (mapAttrsToList (k: v: "  ${k} = \"${quoteStr v}\";") set);
19
20  chatnetString = concatStringsSep "\n" (flip mapAttrsToList cfg.networks
21    (k: v: ''
22      ${k} = {
23        type = "${v.type}";
24        nick = "${quoteStr v.nick}";
25        autosendcmd = "${concatMapStringsSep ";" quoteStr v.autoCommands}";
26        ${
27          lib.optionalString (v.saslExternal) ''
28            sasl_username = "${quoteStr v.nick}";
29              sasl_mechanism = "EXTERNAL";''
30        }
31      };
32    ''));
33
34  serversString = concatStringsSep cnl (flip mapAttrsToList cfg.networks
35    (k: v: ''
36      {
37        chatnet = "${k}";
38        address = "${v.server.address}";
39        port = "${toString v.server.port}";
40        use_ssl = "${lib.hm.booleans.yesNo v.server.ssl.enable}";
41        ssl_verify = "${lib.hm.booleans.yesNo v.server.ssl.verify}";
42        autoconnect = "${lib.hm.booleans.yesNo v.server.autoConnect}";
43        ${
44          optionalString (v.server.ssl.certificateFile != null) ''
45            ssl_cert = "${v.server.ssl.certificateFile}";
46          ''
47        }
48      }
49    ''));
50
51  channelString = concatStringsSep cnl (concatLists
52    (flip mapAttrsToList cfg.networks (k: v:
53      (flip mapAttrsToList v.channels (c: cv: ''
54        {
55          chatnet = "${k}";
56          name = "${c}";
57          autojoin = "${lib.hm.booleans.yesNo cv.autoJoin}";
58        }
59      '')))));
60
61  channelType = types.submodule {
62    options = {
63      name = mkOption {
64        type = types.nullOr types.str;
65        visible = false;
66        default = null;
67        description = "Name of the channel.";
68      };
69
70      autoJoin = mkOption {
71        type = types.bool;
72        default = false;
73        description = "Whether to join this channel on connect.";
74      };
75    };
76  };
77
78  networkType = types.submodule ({ name, ... }: {
79    options = {
80      name = mkOption {
81        visible = false;
82        default = name;
83        type = types.str;
84      };
85
86      nick = mkOption {
87        type = types.str;
88        description = "Nickname in that network.";
89      };
90
91      type = mkOption {
92        type = types.str;
93        description = "Type of the network.";
94        default = "IRC";
95      };
96
97      autoCommands = mkOption {
98        type = types.listOf types.str;
99        default = [ ];
100        description = "List of commands to execute on connect.";
101      };
102
103      server = {
104        address = mkOption {
105          type = types.str;
106          description = "Address of the chat server.";
107        };
108
109        port = mkOption {
110          type = types.port;
111          default = 6667;
112          description = "Port of the chat server.";
113        };
114
115        ssl = {
116          enable = mkOption {
117            type = types.bool;
118            default = true;
119            description = "Whether SSL should be used.";
120          };
121
122          verify = mkOption {
123            type = types.bool;
124            default = true;
125            description = "Whether the SSL certificate should be verified.";
126          };
127
128          certificateFile = mkOption {
129            type = types.nullOr types.path;
130            default = null;
131            description = ''
132              Path to a file containing the certificate used for
133              client authentication to the server.
134            '';
135          };
136        };
137
138        autoConnect = mkOption {
139          type = types.bool;
140          default = false;
141          description = "Whether Irssi connects to the server on launch.";
142        };
143      };
144
145      channels = mkOption {
146        description = "Channels for the given network.";
147        type = types.attrsOf channelType;
148        default = { };
149      };
150
151      saslExternal = mkOption {
152        type = types.bool;
153        default = false;
154        description = ''
155          Enable SASL external authentication. This requires setting a path in
156          [](#opt-programs.irssi.networks._name_.server.ssl.certificateFile).
157        '';
158      };
159    };
160  });
161
162in {
163
164  options = {
165    programs.irssi = {
166      enable = mkEnableOption "the Irssi chat client";
167
168      extraConfig = mkOption {
169        default = "";
170        description = "These lines are appended to the Irssi configuration.";
171        type = types.lines;
172      };
173
174      aliases = mkOption {
175        default = { };
176        example = {
177          J = "join";
178          BYE = "quit";
179        };
180        description = "An attribute set that maps aliases to commands.";
181        type = types.attrsOf types.str;
182      };
183
184      networks = mkOption {
185        default = { };
186        example = literalExpression ''
187          {
188            liberachat = {
189              nick = "hmuser";
190              server = {
191                address = "irc.libera.chat";
192                port = 6697;
193                autoConnect = true;
194              };
195              channels = {
196                nixos.autoJoin = true;
197              };
198            };
199          }
200        '';
201        description = "An attribute set of chat networks.";
202        type = types.attrsOf networkType;
203      };
204    };
205  };
206
207  config = mkIf cfg.enable {
208    home.packages = [ pkgs.irssi ];
209
210    home.file.".irssi/config".text = ''
211      settings = {
212        core = {
213          settings_autosave = "no";
214        };
215      };
216
217      aliases = {
218      ${assignFormat cfg.aliases}
219      };
220
221      chatnets = {
222      ${chatnetString}
223      };
224
225      servers = (
226      ${serversString}
227      );
228
229      channels = (
230      ${channelString}
231      );
232
233      ${cfg.extraConfig}
234    '';
235  };
236}