summaryrefslogtreecommitdiffstats
path: root/modules/continuum.nix
blob: 12360888b5bd00bc10d5ad7d0c4b625d3c75e6df (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
{ config, lib, pkgs, ... }: let
  cfg = config.hrm.continuum;
  format = pkgs.formats.toml {};
in {
  options.hrm.continuum.instances = lib.mkOption {
    default = {};
    description = ''
      continuum instances.  Each is a service continuum-<name> with the state
      directory /var/lib/continuum-<name>.
    '';
    type = lib.types.attrsOf (lib.types.submodule {
      options.settings = lib.mkOption {
        default = {};
        description = ''
          The instance's configuration: continuum's options by their long
          names, see docs/configuration.md and docs/example.toml in continuum.
          Text may hold placeholders like ''${env:STATE_DIRECTORY}, which
          continuum fills in when it starts.  In a Nix string, escape them:
          "\''${env:STATE_DIRECTORY}/node.key".  Unescaped, Nix takes
          env:STATE_DIRECTORY for a URL and drops the braces.
        '';
        type = lib.types.submodule {
          freeformType = format.type;
        };
      };
    });
  };

  config.systemd.services = lib.mapAttrs' (name: instance:
    lib.nameValuePair "continuum-${name}" {
      description = "continuum ${name}";
      wantedBy = [ "multi-user.target" ];
      wants = [ "network-online.target" ];
      after = [ "network-online.target" ];
      serviceConfig = {
        ExecStart = "${pkgs.continuum}/bin/continuum --config ${
          format.generate "continuum-${name}.toml" instance.settings
        }";
        # continuum exits with status 0 when a transport stops, too.
        Restart = "always";
        RestartSec = "5s";

        DynamicUser = true;
        StateDirectory = "continuum-${name}";
        StateDirectoryMode = "0700";

        # It only needs the network and its state directory.
        ProtectSystem = "strict";
        ProtectHome = true;
        PrivateTmp = true;
        PrivateDevices = true;
        NoNewPrivileges = true;
        RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" "AF_NETLINK" ];
        CapabilityBoundingSet = "";
        SystemCallFilter = [ "@system-service" ];
      };
    }
  ) cfg.instances;
}