aboutsummaryrefslogtreecommitdiff
path: root/nix/nixos-module.nix
blob: 286d3bc8469228884874106829bfe014cf3dfe0f (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
{ lib, config, pkgs, ... }:

let
  cfg = config.services.catinator;
  toml = pkgs.formats.toml { };
  configFile = toml.generate "config.toml" cfg.settings;
in

with lib;
{
  options = {
    services.catinator = {
      package = mkOption {
        defaultText = lib.literalMD "`packages.default` from the catinator flake";
      };

      extraEnvironment = mkOption {
        type = types.attrsOf types.str;
        description = "Extra environment variables to pass to the Garage server.";
        default = { };
        example = { RUST_BACKTRACE = "yes"; };
      };

      environmentFile = mkOption {
        type = types.nullOr types.path;
        description = "File containing environment variables to be passed to the Garage server.";
        default = null;
      };

      logLevel = mkOption {
        type = types.enum ([ "error" "warn" "info" "debug" "trace" ]);
        default = "info";
        example = "debug";
      };

      settings = mkOption {
        type = types.submodule {
          freeformType = toml.type;
        };
      };
    };
  };

  config = {
    services.catinator.settings = {
      default = {
        user = {
          username = lib.mkDefault "catinator";
          realname = lib.mkDefault "moaw";
        };

        server = {
          hostname = lib.mkDefault "";
          port = lib.mkDefault 6697;
          tls = lib.mkDefault true;
          sasl = lib.mkDefault true;
        };

        settings = {
          prefix = lib.mkDefault ":";
        };
      };

      release = {
        user = {
          nickname = lib.mkDefault "\\__{^-_-^}";
        };

        server = {
         channels = lib.mkDefault "";
        };
      };
    };

    systemd.services.catinator = {
      description = "Catinator IRC Bot";
      after = [ "network.target" "network-online.target" ];
      wants = [ "network.target" "network-online.target" ];
      wantedBy = [ "multi-user.target" ];
      restartTriggers = [ configFile ] ++ (lib.optional (cfg.environmentFile != null) cfg.environmentFile);
      serviceConfig = {
        ExecStart = "${cfg.package}/bin/catinator";

        DynamicUser = lib.mkDefault true;
        ProtectHome = true;
        NoNewPrivileges = true;
        EnvironmentFile = lib.optional (cfg.environmentFile != null) cfg.environmentFile;
      };
      environment = {
        RUST_LOG = lib.mkDefault "catinator=${cfg.logLevel}";
        CATINATOR_CONFIG = configFile;
      } // cfg.extraEnvironment;
    };
  };
}