aboutsummaryrefslogtreecommitdiff
path: root/nix/module.nix
blob: 5b59937996a3bae51a461171a67fa64e35e820df (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
self:

{ pkgs, lib, config, ... }: let
  cfg = config.services.thulani;
in {
  options.services.thulani = with lib; with lib.types; {
    enable = mkEnableOption "thulani";

    package = mkOption {
      description = "thulani derivation to use";
      type = package;
      default = pkgs.thulani;
    };

    environment = mkOption {
      description = "literal environment to include";
      type = attrs;
    };

    envFiles = mkOption {
      description = "environment files to include";
      type = listOf path;
      default = [];
    };

    user = mkOption {
      description = "user to run service as";
      type = str;
      default = "thulani";
    };

    group = mkOption {
      description = "group to run service as";
      type = str;
      default = "thulani";
    };

    userIdMappingFile = mkOption {
      description = "user id mapping file";
      type = nullOr path;
      default = null;
    };

    restrictFile = mkOption {
      description = "restrict file";
      type = nullOr path;
      default = null;
    };

    postgres = mkOption {
      description = "local postgres server with automatic setup";

      type = submodule {
        options = {
          enable = mkEnableOption "postgres";

          db = mkOption {
            description = "db name";
            type = str;
            default = "memes";
          };
        };
      };
    };
  };

  config = lib.mkIf cfg.enable {
    environment.systemPackages = [
      cfg.package
    ];

    services.thulani.environment = {
      RUST_BACKTRACE = lib.mkDefault "1";
      MAX_HIST = lib.mkDefault "30";
      DEFAULT_HIST = lib.mkDefault "5";
      MAX_SHEET_COLUMN = lib.mkDefault "ZZZ";
      YTDL = lib.mkDefault "${pkgs.yt-dlp}/bin/yt-dlp";
      FFMPEG = lib.mkDefault "${pkgs.ffmpeg_4}/bin/ffmpeg";

      RESTRICT = lib.mkIf (cfg.restrictFile != null) "${cfg.restrictFile}";
      USER_ID_MAPPING = lib.mkIf (cfg.userIdMappingFile != null) "${cfg.userIdMappingFile}";

      DATABASE_URL = lib.mkIf cfg.postgres.enable "postgres:///${cfg.postgres.db}?user=${cfg.user}&host=/var/run/postgresql";
    };

    systemd.services.thulani = {
      description = "thulani bot";

      wantedBy = [
        "multi-user.target"
      ];

      bindsTo = [
        "network-online.target"
      ] ++ lib.optional cfg.postgres.enable "postgresql.service";

      after = [
        "network-online.target"
      ] ++ lib.optional cfg.postgres.enable "postgresql.service";

      inherit (cfg) environment;

      unitConfig = {
        StartLimitBurst = 3;
        StartLimitIntervalSec = "1m";
      };

      serviceConfig = {
        Type = "exec";
        ExecStart = "${cfg.package}/bin/thulani";
        ExecStartPre = let
          inherit (self.packages.${pkgs.system}) dbInit;

        in lib.mkIf cfg.postgres.enable "+${pkgs.sudo}/bin/sudo -u ${config.services.postgresql.superUser} -- ${dbInit} ${config.services.postgresql.package}/bin/psql ${cfg.postgres.db} ${cfg.user}";

        EnvironmentFile = cfg.envFiles;

        DynamicUser = true;
        User = cfg.user;
        Group = cfg.group;

        Restart = "always";
        RestartSec = "10s";

        TimeoutStopSec = "10s";

        MemoryHigh = "200M";
        MemoryMax = "300M";

        ProtectSystem = "strict";
        ProtectProc = "noaccess";
        ProtectHome = true;
        ProtectHostname = true;
        ProtectClock = true;
        ProtectKernelTunables = true;
        ProtectKernelModules = true;
        ProtectKernelLogs = true;
        ProtectControlGroups = true;

        PrivateDevices = true;
        PrivateUsers = true;
        PrivateMounts = true;

        RestrictNamespaces = true;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;

        MemoryDenyWriteExecute = true;
        LockPersonality = true;
        NoNewPrivileges = true;
        KeyringMode = "private";

        SystemCallFilter = "@system-service";
        SystemCallErrorNumber = "EPERM";
      };
    };

    services.postgresql = lib.mkIf cfg.postgres.enable {
      enable = true;

      authentication = ''
        local ${cfg.postgres.db} ${cfg.user} ident
      '';
    };
  };
}