summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/networking/zenohd.nix
blob: 8e86635154d3f7ab7c70306d420ad0cc12683a9b (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
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
{
  lib,
  config,
  pkgs,
  ...
}:

let
  inherit (lib)
    types
    mkDefault
    mkOption
    mkPackageOption
    ;

  json = pkgs.formats.json { };

  cfg = config.services.zenohd;

in
{
  options = {
    services.zenohd = {
      enable = lib.mkEnableOption "Zenoh daemon.";

      package = mkPackageOption pkgs "zenoh" { };

      settings = mkOption {
        description = ''
          Config options for `zenoh.json5` configuration file.

          See <https://github.com/eclipse-zenoh/zenoh/blob/main/DEFAULT_CONFIG.json5>
          for more information.
        '';
        default = { };
        type = types.submodule {
          freeformType = json.type;
        };
      };

      plugins = mkOption {
        description = "Plugin packages to add to zenohd search paths.";
        type = with types; listOf package;
        default = [ ];
        example = lib.literalExpression ''
          [ pkgs.zenoh-plugin-mqtt ]
        '';
      };

      backends = mkOption {
        description = "Storage backend packages to add to zenohd search paths.";
        type = with types; listOf package;
        default = [ ];
        example = lib.literalExpression ''
          [ pkgs.zenoh-backend-rocksdb ]
        '';
      };

      home = mkOption {
        description = "Base directory for zenohd related files defined via ZENOH_HOME.";
        type = types.str;
        default = "/var/lib/zenoh";
      };

      env = mkOption {
        description = ''
          Set environment variables consumed by zenohd and its plugins.
        '';
        type = with types; attrsOf str;
        default = { };
      };

      extraOptions = mkOption {
        description = "Extra command line options for zenohd.";
        type = with types; listOf str;
        default = [ ];
      };
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.services.zenohd =
      let
        cfgFile = json.generate "zenohd.json" cfg.settings;

      in
      {
        wantedBy = [ "multi-user.target" ];
        wants = [ "network-online.target" ];
        after = [ "network-online.target" ];

        environment = cfg.env;

        serviceConfig = {
          Type = "simple";
          User = "zenohd";
          Group = "zenohd";
          ExecStart =
            "${lib.getExe cfg.package} -c ${cfgFile} " + (lib.concatStringsSep " " cfg.extraOptions);
        };
      };

    users = {
      users.zenohd = {
        description = "Zenoh daemon user";
        group = "zenohd";
        isSystemUser = true;
      };

      groups.zenohd = { };
    };

    services.zenohd = {
      env.ZENOH_HOME = cfg.home;

      settings = {
        plugins_loading = {
          enabled = mkDefault true;
          search_dirs = mkDefault (
            (map (x: "${lib.getLib x}/lib") cfg.plugins) ++ [ "${lib.getLib cfg.package}/lib" ]
          ); # needed for internal plugins
        };

        plugins.storage_manager.backend_search_dirs = mkDefault (
          map (x: "${lib.getLib x}/lib") cfg.backends
        );
      };
    };

    systemd.tmpfiles.rules = [ "d ${cfg.home} 750 zenohd zenohd -" ];
  };
}