about summary refs log tree commit diff
path: root/nixos/modules/services/network-filesystems/eris-server.nix
blob: 104676a52c61fdba0d743f83983739275db26c8e (plain) (blame)
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
{ config, lib, pkgs, ... }:

let
  cfg = config.services.eris-server;
  stateDirectoryPath = "\${STATE_DIRECTORY}";
  nullOrStr = with lib.types; nullOr str;
in {

  options.services.eris-server = {

    enable = lib.mkEnableOption "an ERIS server";

    package = lib.mkOption {
      type = lib.types.package;
      default = pkgs.eris-go;
      defaultText = lib.literalExpression "pkgs.eris-go";
      description = "Package to use for the ERIS server.";
    };

    decode = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = ''
        Whether the HTTP service (when enabled) will decode ERIS content at /uri-res/N2R?urn:eris:.
        Enabling this is recommended only for private or local-only servers.
      '';
    };

    listenCoap = lib.mkOption {
      type = nullOrStr;
      default = ":5683";
      example = "[::1]:5683";
      description = ''
        Server CoAP listen address. Listen on all IP addresses at port 5683 by default.
        Please note that the server can service client requests for ERIS-blocks by
        querying other clients connected to the server. Whether or not blocks are
        relayed back to the server depends on client configuration but be aware this
        may leak sensitive metadata and trigger network activity.
      '';
    };

    listenHttp = lib.mkOption {
      type = nullOrStr;
      default = null;
      example = "[::1]:8080";
      description = "Server HTTP listen address. Do not listen by default.";
    };

    backends = lib.mkOption {
      type = with lib.types; listOf str;
      description = ''
        List of backend URLs.
        Add "get" and "put" as query elements to enable those operations.
      '';
      example = [
        "bolt+file:///srv/eris.bolt?get&put"
        "coap+tcp://eris.example.com:5683?get"
      ];
    };

    mountpoint = lib.mkOption {
      type = nullOrStr;
      default = null;
      example = "/eris";
      description = ''
        Mountpoint for FUSE namespace that exposes "urn:eris:…" files.
      '';
    };

  };

  config = lib.mkIf cfg.enable {
    assertions = [{
      assertion = lib.strings.versionAtLeast cfg.package.version "20231219";
      message =
        "Version of `config.services.eris-server.package` is incompatible with this module";
    }];

    systemd.services.eris-server = let
      cmd = "${cfg.package}/bin/eris-go server"
        + (lib.optionalString (cfg.listenCoap != null)
          " --coap '${cfg.listenCoap}'")
        + (lib.optionalString (cfg.listenHttp != null)
          " --http '${cfg.listenHttp}'")
        + (lib.optionalString cfg.decode " --decode")
        + (lib.optionalString (cfg.mountpoint != null)
          " --mountpoint '${cfg.mountpoint}'");
    in {
      description = "ERIS block server";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      environment.ERIS_STORE_URL = toString cfg.backends;
      script = lib.mkIf (cfg.mountpoint != null) ''
        export PATH=${config.security.wrapperDir}:$PATH
        ${cmd}
      '';
      serviceConfig = let
        umounter = lib.mkIf (cfg.mountpoint != null)
          "-${config.security.wrapperDir}/fusermount -uz ${cfg.mountpoint}";
      in if (cfg.mountpoint == null) then {
        ExecStart = cmd;
      } else
        {
          ExecStartPre = umounter;
          ExecStopPost = umounter;
        } // {
          Restart = "always";
          RestartSec = 20;
          AmbientCapabilities = "CAP_NET_BIND_SERVICE";
        };
    };
  };

  meta.maintainers = with lib.maintainers; [ ehmry ];
}