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