-
Notifications
You must be signed in to change notification settings - Fork 1
/
nixos-module.nix
79 lines (74 loc) · 2.13 KB
/
nixos-module.nix
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
{ lib, pkgs, config, ... }:
let
inherit (lib) mkEnableOption mkIf mkOption types;
cfg = config.services.telegram-sendmail;
socket = "/run/telegram-sendmail/socket.sock";
in
{
options = {
services.telegram-sendmail = {
enable = mkEnableOption "telegram-sendmail";
credentialFile = mkOption {
description = "Dotenv file used in the service. Should not be a nix-store path.";
type = types.path;
};
extraArgs = mkOption {
description = "Extra arguments to pass to the script";
type = types.listOf types.str;
};
};
};
config = mkIf cfg.enable {
users.users.telegram_sendmail = {
isSystemUser = true;
group = "telegram_sendmail";
};
users.groups.telegram_sendmail = {};
systemd.services.telegram-sendmail = {
wantedBy = [ "multi-user.target" ];
unitConfig = {
StartLimitIntervalSec = 0;
};
serviceConfig = {
RuntimeDirectory = "telegram-sendmail";
StateDirectory = "telegram-sendmail";
Restart = "always";
RestartSec = 1;
EnvironmentFile = [ cfg.credentialFile ];
User = "telegram_sendmail";
};
script = let
telegram_mail = pkgs.stdenvNoCC.mkDerivation {
name = "telegram_mail";
dontUnpack = true;
preferLocalBuild = true;
allowSubstitutes = false;
buildInputs = with pkgs; [ python3 ];
installPhase = ''
install -m 555 ${./service} $out
patchShebangs $out
'';
};
in ''
${telegram_mail} ${lib.escapeShellArgs ([
"-b" "${socket}"
"-n" "${config.networking.hostName}"
] ++ cfg.extraArgs)}
'';
};
services.mail.sendmailSetuidWrapper = {
program = "sendmail";
source = pkgs.writeShellScript "sendmail" ''
while [ ! -S "${socket}" ]; do
echo Waiting for the sendmail socket to be available... >&2
sleep 1
done
${pkgs.netcat}/bin/nc -N -U "${socket}"
'';
setuid = false;
setgid = false;
owner = "root";
group = "root";
};
};
}