1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7
8let
9 cfg = config.services.eris-server;
10 stateDirectoryPath = "\${STATE_DIRECTORY}";
11 nullOrStr = with lib.types; nullOr str;
12in
13{
14
15 options.services.eris-server = {
16
17 enable = lib.mkEnableOption "an ERIS server";
18
19 package = lib.mkOption {
20 type = lib.types.package;
21 default = pkgs.eris-go;
22 defaultText = lib.literalExpression "pkgs.eris-go";
23 description = "Package to use for the ERIS server.";
24 };
25
26 decode = lib.mkOption {
27 type = lib.types.bool;
28 default = false;
29 description = ''
30 Whether the HTTP service (when enabled) will decode ERIS content at /uri-res/N2R?urn:eris:.
31 Enabling this is recommended only for private or local-only servers.
32 '';
33 };
34
35 listenCoap = lib.mkOption {
36 type = nullOrStr;
37 default = ":5683";
38 example = "[::1]:5683";
39 description = ''
40 Server CoAP listen address. Listen on all IP addresses at port 5683 by default.
41 Please note that the server can service client requests for ERIS-blocks by
42 querying other clients connected to the server. Whether or not blocks are
43 relayed back to the server depends on client configuration but be aware this
44 may leak sensitive metadata and trigger network activity.
45 '';
46 };
47
48 listenHttp = lib.mkOption {
49 type = nullOrStr;
50 default = null;
51 example = "[::1]:8080";
52 description = "Server HTTP listen address. Do not listen by default.";
53 };
54
55 backends = lib.mkOption {
56 type = with lib.types; listOf str;
57 description = ''
58 List of backend URLs.
59 Add "get" and "put" as query elements to enable those operations.
60 '';
61 example = [
62 "badger+file:///var/db/eris.badger?get&put"
63 "coap+tcp://eris.example.com:5683?get"
64 ];
65 };
66
67 mountpoint = lib.mkOption {
68 type = nullOrStr;
69 default = null;
70 example = "/eris";
71 description = ''
72 Mountpoint for FUSE namespace that exposes "urn:eris:…" files.
73 '';
74 };
75
76 };
77
78 config = lib.mkIf cfg.enable {
79 assertions = [
80 {
81 assertion = lib.strings.versionAtLeast cfg.package.version "20231219";
82 message = "Version of `config.services.eris-server.package` is incompatible with this module";
83 }
84 ];
85
86 systemd.services.eris-server =
87 let
88 cmd =
89 "${cfg.package}/bin/eris-go server"
90 + (lib.optionalString (cfg.listenCoap != null) " --coap '${cfg.listenCoap}'")
91 + (lib.optionalString (cfg.listenHttp != null) " --http '${cfg.listenHttp}'")
92 + (lib.optionalString cfg.decode " --decode")
93 + (lib.optionalString (cfg.mountpoint != null) " --mountpoint '${cfg.mountpoint}'");
94 in
95 {
96 description = "ERIS block server";
97 after = [ "network.target" ];
98 wantedBy = [ "multi-user.target" ];
99 environment.ERIS_STORE_URL = toString cfg.backends;
100 script = lib.mkIf (cfg.mountpoint != null) ''
101 export PATH=${config.security.wrapperDir}:$PATH
102 ${cmd}
103 '';
104 serviceConfig =
105 let
106 umounter = lib.mkIf (
107 cfg.mountpoint != null
108 ) "-${config.security.wrapperDir}/fusermount -uz ${cfg.mountpoint}";
109 in
110 if (cfg.mountpoint == null) then
111 {
112 ExecStart = cmd;
113 }
114 else
115 {
116 ExecStartPre = umounter;
117 ExecStopPost = umounter;
118 }
119 // {
120 Restart = "always";
121 RestartSec = 20;
122 AmbientCapabilities = "CAP_NET_BIND_SERVICE";
123 };
124 };
125 };
126
127 meta.maintainers = with lib.maintainers; [ ehmry ];
128}