blob: 31de9b6a42fe437ebcee6fc6648ab899747ba793 (
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
|
{
config,
lib,
pkgs,
utils,
...
}:
let
cfg = config.services.artalk;
settingsFormat = pkgs.formats.json { };
in
{
meta = {
maintainers = with lib.maintainers; [ moraxyc ];
};
options = {
services.artalk = {
enable = lib.mkEnableOption "artalk, a comment system";
configFile = lib.mkOption {
type = lib.types.str;
default = "/etc/artalk/config.yml";
description = "Artalk config file path. If it is not exist, Artalk will generate one.";
};
allowModify = lib.mkOption {
type = lib.types.bool;
default = true;
description = "allow Artalk store the settings to config file persistently";
};
workdir = lib.mkOption {
type = lib.types.str;
default = "/var/lib/artalk";
description = "Artalk working directory";
};
user = lib.mkOption {
type = lib.types.str;
default = "artalk";
description = "Artalk user name.";
};
group = lib.mkOption {
type = lib.types.str;
default = "artalk";
description = "Artalk group name.";
};
package = lib.mkPackageOption pkgs "artalk" { };
settings = lib.mkOption {
type = lib.types.submodule {
freeformType = settingsFormat.type;
options = {
host = lib.mkOption {
type = lib.types.str;
default = "0.0.0.0";
description = ''
Artalk server listen host
'';
};
port = lib.mkOption {
type = lib.types.port;
default = 23366;
description = ''
Artalk server listen port
'';
};
};
};
default = { };
description = ''
The artalk configuration.
If you set allowModify to true, Artalk will be able to store the settings in the config file persistently. This section's content will update in the config file after the service restarts.
Options containing secret data should be set to an attribute set
containing the attribute `_secret` - a string pointing to a file
containing the value the option should be set to.
'';
};
};
};
config = lib.mkIf cfg.enable {
users.users.artalk = lib.optionalAttrs (cfg.user == "artalk") {
description = "artalk user";
isSystemUser = true;
group = cfg.group;
};
users.groups.artalk = lib.optionalAttrs (cfg.group == "artalk") { };
environment.systemPackages = [ cfg.package ];
systemd.services.artalk = {
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
preStart = ''
umask 0077
${utils.genJqSecretsReplacementSnippet cfg.settings "/run/artalk/new"}
''
+ (
if cfg.allowModify then
''
[ -e "${cfg.configFile}" ] || ${lib.getExe cfg.package} gen config "${cfg.configFile}"
cat "${cfg.configFile}" | ${lib.getExe pkgs.yj} > "/run/artalk/old"
${lib.getExe pkgs.jq} -s '.[0] * .[1]' "/run/artalk/old" "/run/artalk/new" > "/run/artalk/result"
cat "/run/artalk/result" | ${lib.getExe pkgs.yj} -r > "${cfg.configFile}"
rm /run/artalk/{old,new,result}
''
else
''
cat /run/artalk/new | ${lib.getExe pkgs.yj} -r > "${cfg.configFile}"
rm /run/artalk/new
''
);
serviceConfig = {
User = cfg.user;
Group = cfg.group;
Type = "simple";
ExecStart = "${lib.getExe cfg.package} server --config ${cfg.configFile} --workdir ${cfg.workdir} --host ${cfg.settings.host} --port ${toString cfg.settings.port}";
Restart = "on-failure";
RestartSec = "5s";
ConfigurationDirectory = [ "artalk" ];
StateDirectory = [ "artalk" ];
RuntimeDirectory = [ "artalk" ];
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
ProtectHome = "yes";
};
};
};
}
|