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