fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
9.7 KiB343 linesraw
1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6  cfg = config.programs.hexchat;
7
8  channelOptions = with types;
9    submodule {
10      options = {
11        autoconnect = mkOption {
12          type = nullOr bool;
13          default = false;
14          description = "Autoconnect to network.";
15        };
16
17        connectToSelectedServerOnly = mkOption {
18          type = nullOr bool;
19          default = true;
20          description = "Connect to selected server only.";
21        };
22
23        bypassProxy = mkOption {
24          type = nullOr bool;
25          default = true;
26          description = "Bypass proxy.";
27        };
28
29        forceSSL = mkOption {
30          type = nullOr bool;
31          default = false;
32          description = "Use SSL for all servers.";
33        };
34
35        acceptInvalidSSLCertificates = mkOption {
36          type = nullOr bool;
37          default = false;
38          description = "Accept invalid SSL certificates.";
39        };
40
41        useGlobalUserInformation = mkOption {
42          type = nullOr bool;
43          default = false;
44          description = "Use global user information.";
45        };
46      };
47    };
48
49  modChannelOption = with types;
50    submodule {
51      options = {
52        autojoin = mkOption {
53          type = listOf str;
54          default = [ ];
55          example = [ "#home-manager" "#linux" "#nix" ];
56          description = "Channels list to autojoin on connecting to server.";
57        };
58
59        charset = mkOption {
60          type = nullOr str;
61          default = null;
62          example = "UTF-8 (Unicode)";
63          description = "Character set.";
64        };
65
66        commands = mkOption {
67          type = listOf str;
68          default = [ ];
69          example = literalExpression ''[ "ECHO Greetings fellow Nixer! ]'';
70          description = "Commands to be executed on connecting to server.";
71        };
72
73        loginMethod = mkOption {
74          type = nullOr (enum (attrNames loginMethodMap));
75          default = null;
76          description = ''
77            The login method. The allowed options are:
78
79            `null`
80            :  Default
81
82            `"nickServMsg"`
83            :  NickServ (`/MSG NickServ` + password)
84
85            `"nickServ"`
86            :  NickServ (`/NICKSERV` + password)
87
88            `"challengeAuth"`
89            :  Challenge Auth (username + password)
90
91            `"sasl"`
92            :  SASL (username + password)
93
94            `"serverPassword"`
95            :  Server password (`/PASS` password)
96
97            `"saslExternal"`
98            :  SASL EXTERNAL (cert)
99
100            `"customCommands"`
101            : Use "commands" field for auth. For example
102              ```nix
103              commands = [ "/msg NickServ IDENTIFY my_password" ]
104              ```
105          '';
106        };
107
108        nickname = mkOption {
109          type = nullOr str;
110          default = null;
111          description = "Primary nickname.";
112        };
113
114        nickname2 = mkOption {
115          type = nullOr str;
116          default = null;
117          description = "Secondary nickname.";
118        };
119
120        options = mkOption {
121          type = nullOr channelOptions;
122          default = null;
123          example = {
124            autoconnect = true;
125            useGlobalUserInformation = true;
126          };
127          description = "Channel options.";
128        };
129
130        password = mkOption {
131          type = nullOr str;
132          default = null;
133          description = ''
134            Password to use. Note this password will be readable by all user's
135            in the Nix store.
136          '';
137        };
138
139        realName = mkOption {
140          type = nullOr str;
141          default = null;
142          description = ''
143            Real name. Is used to populate the real name field that appears when
144            someone uses the `WHOIS` command on your nick.
145          '';
146        };
147
148        userName = mkOption {
149          type = nullOr str;
150          default = null;
151          description = ''
152            User name. Part of your `user@host` hostmask that
153            appears to other on IRC.
154          '';
155        };
156
157        servers = mkOption {
158          type = listOf str;
159          default = [ ];
160          example = [ "irc.oftc.net" ];
161          description = "IRC Server Address List.";
162        };
163      };
164    };
165
166  transformField = k: v: if (v != null) then "${k}=${v}" else null;
167
168  listChar = c: l:
169    if l != [ ] then concatMapStringsSep "\n" (transformField c) l else null;
170
171  computeFieldsValue = channel:
172    let
173      ifTrue = p: n: if p then n else 0;
174      result = with channel.options;
175        foldl' (a: b: a + b) 0 [
176          (ifTrue (!connectToSelectedServerOnly) 1)
177          (ifTrue useGlobalUserInformation 2)
178          (ifTrue forceSSL 4)
179          (ifTrue autoconnect 8)
180          (ifTrue (!bypassProxy) 16)
181          (ifTrue acceptInvalidSSLCertificates 32)
182        ];
183    in toString (if channel.options == null then 0 else result);
184
185  loginMethodMap = {
186    nickServMsg = 1;
187    nickServ = 2;
188    challengeAuth = 4;
189    sasl = 6;
190    serverPassword = 7;
191    customCommands = 9;
192    saslExternal = 10;
193  };
194
195  loginMethod = channel:
196    transformField "L" (optionalString (channel.loginMethod != null)
197      (toString loginMethodMap.${channel.loginMethod}));
198
199  # Note: Missing option `D=`.
200  transformChannel = channelName:
201    let channel = cfg.channels.${channelName};
202    in concatStringsSep "\n" (remove null [
203      "" # leave a space between one server and another
204      (transformField "N" channelName)
205      (loginMethod channel)
206      (transformField "E" channel.charset)
207      (transformField "F" (computeFieldsValue channel))
208      (transformField "I" channel.nickname)
209      (transformField "i" channel.nickname2)
210      (transformField "R" channel.realName)
211      (transformField "U" channel.userName)
212      (transformField "P" channel.password)
213      (listChar "S" channel.servers)
214      (listChar "J" channel.autojoin)
215      (listChar "C" channel.commands)
216    ]);
217
218in {
219  meta.maintainers = with maintainers; [ thiagokokada ];
220
221  options.programs.hexchat = with types; {
222    enable = mkEnableOption "HexChat, a graphical IRC client";
223
224    channels = mkOption {
225      type = attrsOf modChannelOption;
226      default = { };
227      example = literalExpression ''
228        {
229          oftc = {
230            autojoin = [
231              "#home-manager"
232              "#linux"
233            ];
234            charset = "UTF-8 (Unicode)";
235            commands = [
236              "ECHO Buzz Lightyear sent you a message: 'To Infinity... and Beyond!'"
237            ];
238            loginMethod = sasl;
239            nickname = "my_nickname";
240            nickname2 = "my_secondchoice";
241            options = {
242              acceptInvalidSSLCertificates = false;
243              autoconnect = true;
244              bypassProxy = true;
245              connectToSelectedServerOnly = true;
246              useGlobalUserInformation = false;
247              forceSSL = false;
248            };
249            password = "my_password";
250            realName = "my_realname";
251            servers = [
252              "irc.oftc.net"
253            ];
254            userName = "my_username";
255          };
256        }'';
257      description = ''
258        Configures {file}`$XDG_CONFIG_HOME/hexchat/servlist.conf`.
259      '';
260    };
261
262    settings = mkOption {
263      default = null;
264      type = nullOr (attrsOf str);
265      example = literalExpression ''
266        {
267          irc_nick1 = "mynick";
268          irc_username = "bob";
269          irc_realname = "Bart Simpson";
270          text_font = "Monospace 14";
271        };
272      '';
273      description = ''
274        Configuration for {file}`$XDG_CONFIG_HOME/hexchat/hexchat.conf`, see
275        <https://hexchat.readthedocs.io/en/latest/settings.html#list-of-settings>
276        for supported values.
277      '';
278    };
279
280    overwriteConfigFiles = mkOption {
281      type = nullOr bool;
282      default = false;
283      description = ''
284        Enables overwriting HexChat configuration files
285        ({file}`hexchat.conf`, {file}`servlist.conf`).
286        Any existing HexChat configuration will be lost. Make sure to back up
287        any previous configuration before enabling this.
288
289        Enabling this setting is recommended, because everytime HexChat
290        application is closed it overwrites Nix/Home Manager provided
291        configuration files, causing:
292
293        1. Nix/Home Manager provided configuration to be out of sync with
294           actual active HexChat configuration.
295        2. Nix/Home Manager updates to be blocked until configuration files are
296           manually removed.
297      '';
298    };
299
300    theme = mkOption {
301      type = nullOr package;
302      default = null;
303      example = literalExpression ''
304        source = pkgs.fetchzip {
305          url = "https://dl.hexchat.net/themes/Monokai.hct#Monokai.zip";
306          sha256 = "sha256-WCdgEr8PwKSZvBMs0fN7E2gOjNM0c2DscZGSKSmdID0=";
307          stripRoot = false;
308        };
309      '';
310      description = ''
311        Theme package for HexChat. Expects a derivation containing decompressed
312        theme files. Note, `.hct` files are actually ZIP files,
313        as seen in example.
314      '';
315    };
316  };
317
318  config = mkIf cfg.enable {
319    assertions = [
320      (hm.assertions.assertPlatform "programs.hexchat" pkgs platforms.linux)
321    ];
322
323    home.packages = [ pkgs.hexchat ];
324
325    xdg.configFile."hexchat" = mkIf (cfg.theme != null) {
326      source = cfg.theme;
327      recursive = true;
328    };
329
330    xdg.configFile."hexchat/hexchat.conf" = mkIf (cfg.settings != null) {
331      force = cfg.overwriteConfigFiles;
332      text = concatMapStringsSep "\n" (x: x + " = " + cfg.settings.${x})
333        (attrNames cfg.settings);
334    };
335
336    xdg.configFile."hexchat/servlist.conf" = mkIf (cfg.channels != { }) {
337      force = cfg.overwriteConfigFiles;
338      # Final line breaks is required to avoid cropping last field value.
339      text = concatMapStringsSep "\n" transformChannel (attrNames cfg.channels)
340        + "\n\n";
341    };
342  };
343}