summaryrefslogtreecommitdiff
path: root/modules/nixos/duksebot/default.nix
blob: c1dc5cf9e35c2df46012da0bd79d1e1c7c75c404 (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
# This module defines an on-demand minecraft server service which turns off the
# server when it's not being used.

{ config, lib, pkgs, modulesPath, ... }:

let
  inherit (lib) mkIf mkOption mkEnableOption types;

  cfg = config.my.services.duksebot;
in
{
  options.my.services.duksebot = {
    enable = mkEnableOption "duksebot daily reminder";

    package = mkOption {
      description = "What package to use";
      default = pkgs.duksebot;
      type = types.package;
    };
  };

  config = mkIf cfg.enable {
    # Create a user to run the server under.
    users.users.duksebot = {
      description = "Runs daily dukse reminder";
      group = "duksebot";
      isSystemUser = true;
      home = "/srv/duksebot";
      createHome = true;
    };
    users.groups.duksebot = { };

    age.secrets.duksebot-env = {
      file = ../../../secrets/duksebot.env.age;
      owner = config.users.users.duksebot.name;
      group = config.users.users.duksebot.group;
      mode = "0440";
    };

    # Create a service which simply runs script. This will be invoked by our timer.
    systemd.services.duksebot = {
      serviceConfig = {
        # We only want to run this once every time the timer triggers it.
        Type = "oneshot";
        # Run as the user we created above.
        User = "duksebot";
        Group = "duksebot";
        WorkingDirectory = config.users.users.duksebot.home;
      };
      script = ''
        # Load the secret environment variables.
        export $(grep -v '^#' ${config.age.secrets.duksebot-env.path} | xargs)
        # Kick off.
        exec "${cfg.package}"/bin/duksebot
      '';
    };

    # Create a timer to activate our oneshot service.
    systemd.timers.duksebot = {
      wantedBy = [ "timers.target" ];
      partOf = [ "duksebot.service" ];
      after = [ "network-online.target" ];
      wants = [ "network-online.target" ]; # FIXME: redundant?
      timerConfig = {
        # OnCalendar = "*-*-* 7:00:00";
        OnCalendar = "*:0/1";
        Unit = "duksebot.service";
      };
    };
  };
}