1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7
8let
9 cfg = config.services.weechat;
10in
11{
12 options.services.weechat = {
13 enable = lib.mkEnableOption "weechat";
14
15 package = lib.mkPackageOption pkgs "weechat" { };
16
17 root = lib.mkOption {
18 description = "Weechat state directory.";
19 type = lib.types.path;
20 default = "/var/lib/weechat";
21 };
22
23 sessionName = lib.mkOption {
24 description = "Name of the `screen` session for weechat.";
25 default = "weechat-screen";
26 type = lib.types.str;
27 };
28
29 binary = lib.mkOption {
30 type = lib.types.path;
31 description = "Binary to execute.";
32 default =
33 if (!cfg.headless) then "${cfg.package}/bin/weechat" else "${cfg.package}/bin/weechat-headless";
34 defaultText = lib.literalExpression ''"''${cfg.package}/bin/weechat"'';
35 example = lib.literalExpression ''"''${cfg.package}/bin/weechat-headless"'';
36 };
37
38 headless = lib.mkOption {
39 type = lib.types.bool;
40 default = false;
41 description = ''
42 Allows specifying if weechat should run in TUI or headless mode.
43 '';
44 };
45 };
46
47 config = lib.mkIf cfg.enable {
48 users = {
49 groups.weechat = { };
50 users.weechat = {
51 group = "weechat";
52 isSystemUser = true;
53 };
54 };
55
56 systemd.tmpfiles.settings."weechat" = {
57 "${cfg.root}" = lib.mkIf (cfg.root != "/var/lib/weechat") {
58 d = {
59 user = "weechat";
60 group = "weechat";
61 mode = "750";
62 };
63 };
64 };
65
66 systemd.services.weechat = {
67 serviceConfig = {
68 Type = "simple";
69 User = "weechat";
70 Group = "weechat";
71 ExecStart = lib.mkIf (cfg.headless) "${cfg.binary} --dir ${cfg.root} --stdout";
72 StateDirectory = lib.mkIf (cfg.root == "/var/lib/weechat") "weechat";
73 StateDirectoryMode = 750;
74 RemainAfterExit = "yes";
75 };
76 script =
77 lib.mkIf (!cfg.headless)
78 "exec ${config.security.wrapperDir}/screen -Dm -S ${cfg.sessionName} ${cfg.binary} --dir ${cfg.root}";
79 wantedBy = [ "multi-user.target" ];
80 wants = [ "network.target" ];
81 };
82
83 security.wrappers.screen = lib.mkIf (!cfg.headless) {
84 setuid = true;
85 owner = "root";
86 group = "root";
87 source = "${pkgs.screen}/bin/screen";
88 };
89 };
90
91 meta.doc = ./weechat.md;
92}