1{
2 lib,
3 config,
4 pkgs,
5 ...
6}:
7
8let
9 inherit (lib)
10 types
11 mkDefault
12 mkOption
13 ;
14
15 json = pkgs.formats.json { };
16
17 cfg = config.services.zenohd;
18
19in
20{
21 options = {
22 services.zenohd = {
23 enable = lib.mkEnableOption "Zenoh daemon.";
24
25 package = mkOption {
26 description = "The zenoh package to use.";
27 type = types.package;
28 default = pkgs.zenoh;
29 defaultText = "pkgs.zenoh";
30 };
31
32 settings = mkOption {
33 description = ''
34 Config options for `zenoh.json5` configuration file.
35
36 See <https://github.com/eclipse-zenoh/zenoh/blob/main/DEFAULT_CONFIG.json5>
37 for more information.
38 '';
39 default = { };
40 type = types.submodule {
41 freeformType = json.type;
42 };
43 };
44
45 plugins = mkOption {
46 description = "Plugin packages to add to zenohd search paths.";
47 type = with types; listOf package;
48 default = [ ];
49 example = lib.literalExpression ''
50 [ pkgs.zenoh-plugin-mqtt ]
51 '';
52 };
53
54 backends = mkOption {
55 description = "Storage backend packages to add to zenohd search paths.";
56 type = with types; listOf package;
57 default = [ ];
58 example = lib.literalExpression ''
59 [ pkgs.zenoh-backend-rocksdb ]
60 '';
61 };
62
63 home = mkOption {
64 description = "Base directory for zenohd related files defined via ZENOH_HOME.";
65 type = types.str;
66 default = "/var/lib/zenoh";
67 };
68
69 env = mkOption {
70 description = ''
71 Set environment variables consumed by zenohd and its plugins.
72 '';
73 type = with types; attrsOf str;
74 default = { };
75 };
76
77 extraOptions = mkOption {
78 description = "Extra command line options for zenohd.";
79 type = with types; listOf str;
80 default = [ ];
81 };
82 };
83 };
84
85 config = lib.mkIf cfg.enable {
86 systemd.services.zenohd =
87 let
88 cfgFile = json.generate "zenohd.json" cfg.settings;
89
90 in
91 {
92 wantedBy = [ "multi-user.target" ];
93 wants = [ "network-online.target" ];
94 after = [ "network-online.target" ];
95
96 environment = cfg.env;
97
98 serviceConfig = {
99 Type = "simple";
100 User = "zenohd";
101 Group = "zenohd";
102 ExecStart =
103 "${lib.getExe cfg.package} -c ${cfgFile} " + (lib.concatStringsSep " " cfg.extraOptions);
104 };
105 };
106
107 users = {
108 users.zenohd = {
109 description = "Zenoh daemon user";
110 group = "zenohd";
111 isSystemUser = true;
112 };
113
114 groups.zenohd = { };
115 };
116
117 services.zenohd = {
118 env.ZENOH_HOME = cfg.home;
119
120 settings = {
121 plugins_loading = {
122 enabled = mkDefault true;
123 search_dirs = mkDefault (
124 (map (x: "${lib.getLib x}/lib") cfg.plugins) ++ [ "${lib.getLib cfg.package}/lib" ]
125 ); # needed for internal plugins
126 };
127
128 plugins.storage_manager.backend_search_dirs = mkDefault (
129 map (x: "${lib.getLib x}/lib") cfg.backends
130 );
131 };
132 };
133
134 systemd.tmpfiles.rules = [ "d ${cfg.home} 750 zenohd zenohd -" ];
135 };
136}