summary refs log tree commit diff
path: root/nixos/modules/services/networking/shout.nix
blob: cca03a8f88a1baa07e3125436d60279620c79625 (plain) (blame)
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
{ pkgs, lib, config, ... }:

with lib;

let
  cfg = config.services.shout;
  shoutHome = "/var/lib/shout";

  defaultConfig = pkgs.runCommand "config.js" { preferLocalBuild = true; } ''
    EDITOR=true ${pkgs.shout}/bin/shout config --home $PWD
    mv config.js $out
  '';

  finalConfigFile = if (cfg.configFile != null) then cfg.configFile else ''
    var _ = require('${pkgs.shout}/lib/node_modules/shout/node_modules/lodash')

    module.exports = _.merge(
      {},
      require('${defaultConfig}'),
      ${builtins.toJSON cfg.config}
    )
  '';

in {
  options.services.shout = {
    enable = mkEnableOption "Shout web IRC client";

    private = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Make your shout instance private. You will need to configure user
        accounts by adding entries in <filename>${shoutHome}/users</filename>.
      '';
    };

    listenAddress = mkOption {
      type = types.str;
      default = "0.0.0.0";
      description = "IP interface to listen on for http connections.";
    };

    port = mkOption {
      type = types.port;
      default = 9000;
      description = "TCP port to listen on for http connections.";
    };

    configFile = mkOption {
      type = types.nullOr types.lines;
      default = null;
      description = ''
        Contents of Shout's <filename>config.js</filename> file.

        Used for backward compatibility, recommended way is now to use
        the <literal>config</literal> option.

        Documentation: http://shout-irc.com/docs/server/configuration.html
      '';
    };

    config = mkOption {
      default = {};
      type = types.attrs;
      example = {
        displayNetwork = false;
        defaults = {
          name = "Your Network";
          host = "localhost";
          port = 6697;
        };
      };
      description = ''
        Shout <filename>config.js</filename> contents as attribute set (will be
        converted to JSON to generate the configuration file).

        The options defined here will be merged to the default configuration file.

        Documentation: http://shout-irc.com/docs/server/configuration.html
      '';
    };
  };

  config = mkIf cfg.enable {
    users.users.shout = {
      isSystemUser = true;
      group = "shout";
      description = "Shout daemon user";
      home = shoutHome;
      createHome = true;
    };
    users.groups.shout = {};

    systemd.services.shout = {
      description = "Shout web IRC client";
      wantedBy = [ "multi-user.target" ];
      wants = [ "network-online.target" ];
      after = [ "network-online.target" ];
      preStart = "ln -sf ${pkgs.writeText "config.js" finalConfigFile} ${shoutHome}/config.js";
      script = concatStringsSep " " [
        "${pkgs.shout}/bin/shout"
        (if cfg.private then "--private" else "--public")
        "--port" (toString cfg.port)
        "--host" (toString cfg.listenAddress)
        "--home" shoutHome
      ];
      serviceConfig = {
        User = "shout";
        ProtectHome = "true";
        ProtectSystem = "full";
        PrivateTmp = "true";
      };
    };
  };
}