about summary refs log tree commit diff
path: root/nixos/modules/services/video/photonvision.nix
blob: fdbe9da3999dad3bf30796967644ad90758dfb8c (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
{ config, pkgs, lib, ... }:

let
  cfg = config.services.photonvision;
in
{
  options = {
    services.photonvision = {
      enable = lib.mkEnableOption (lib.mdDoc "Enable PhotonVision");

      package = lib.mkPackageOption pkgs "photonvision" {};

      openFirewall = lib.mkOption {
        description = lib.mdDoc ''
          Whether to open the required ports in the firewall.
        '';
        default = false;
        type = lib.types.bool;
      };
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.services.photonvision = {
      description = "PhotonVision, the free, fast, and easy-to-use computer vision solution for the FIRST Robotics Competition";

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

      serviceConfig = {
        ExecStart = lib.getExe cfg.package;

        # ephemeral root directory
        RuntimeDirectory = "photonvision";
        RootDirectory = "/run/photonvision";

        # setup persistent state and logs directories
        StateDirectory = "photonvision";
        LogsDirectory = "photonvision";

        BindReadOnlyPaths = [
          # mount the nix store read-only
          "/nix/store"

          # the JRE reads the user.home property from /etc/passwd
          "/etc/passwd"
        ];
        BindPaths = [
          # mount the configuration and logs directories to the host
          "/var/lib/photonvision:/photonvision_config"
          "/var/log/photonvision:/photonvision_config/logs"
        ];

        # for PhotonVision's dynamic libraries, which it writes to /tmp
        PrivateTmp = true;
      };
    };

    networking.firewall = lib.mkIf cfg.openFirewall {
      allowedTCPPorts = [ 5800 ];
      allowedTCPPortRanges = [{ from = 1180; to = 1190; }];
    };
  };
}