about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/misc/rkvm.nix
blob: 582e8511ed96ee64a8b4804639d224cbefe16ed4 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
{ options, config, pkgs, lib, ... }:

with lib;
let
  opt = options.services.rkvm;
  cfg = config.services.rkvm;
  toml = pkgs.formats.toml { };
in
{
  meta.maintainers = with maintainers; [ ckie ];

  options.services.rkvm = {
    enable = mkOption {
      default = cfg.server.enable || cfg.client.enable;
      defaultText = literalExpression "config.${opt.server.enable} || config.${opt.client.enable}";
      type = types.bool;
      description = mdDoc ''
        Whether to enable rkvm, a Virtual KVM switch for Linux machines.
      '';
    };

    package = mkPackageOption pkgs "rkvm" { };

    server = {
      enable = mkEnableOption "the rkvm server daemon (input transmitter)";

      settings = mkOption {
        type = types.submodule
          {
            freeformType = toml.type;
            options = {
              listen = mkOption {
                type = types.str;
                default = "0.0.0.0:5258";
                description = mdDoc ''
                  An internet socket address to listen on, either IPv4 or IPv6.
                '';
              };

              switch-keys = mkOption {
                type = types.listOf types.str;
                default = [ "left-alt" "left-ctrl" ];
                description = mdDoc ''
                  A key list specifying a host switch combination.

                  _A list of key names is available in <https://github.com/htrefil/rkvm/blob/master/switch-keys.md>._
                '';
              };

              certificate = mkOption {
                type = types.path;
                default = "/etc/rkvm/certificate.pem";
                description = mdDoc ''
                  TLS certificate path.

                  ::: {.note}
                  This should be generated with {command}`rkvm-certificate-gen`.
                  :::
                '';
              };

              key = mkOption {
                type = types.path;
                default = "/etc/rkvm/key.pem";
                description = mdDoc ''
                  TLS key path.

                  ::: {.note}
                  This should be generated with {command}`rkvm-certificate-gen`.
                  :::
                '';
              };

              password = mkOption {
                type = types.str;
                description = mdDoc ''
                  Shared secret token to authenticate the client.
                  Make sure this matches your client's config.
                '';
              };
            };
          };

        default = { };
        description = mdDoc "Structured server daemon configuration";
      };
    };

    client = {
      enable = mkEnableOption "the rkvm client daemon (input receiver)";

      settings = mkOption {
        type = types.submodule
          {
            freeformType = toml.type;
            options = {
              server = mkOption {
                type = types.str;
                example = "192.168.0.123:5258";
                description = mdDoc ''
                  An RKVM server's internet socket address, either IPv4 or IPv6.
                '';
              };

              certificate = mkOption {
                type = types.path;
                default = "/etc/rkvm/certificate.pem";
                description = mdDoc ''
                  TLS ceritficate path.

                  ::: {.note}
                  This should be generated with {command}`rkvm-certificate-gen`.
                  :::
                '';
              };

              password = mkOption {
                type = types.str;
                description = mdDoc ''
                  Shared secret token to authenticate the client.
                  Make sure this matches your server's config.
                '';
              };
            };
          };

        default = {};
        description = mdDoc "Structured client daemon configuration";
      };
    };

  };

  config = mkIf cfg.enable {
    environment.systemPackages = [ cfg.package ];

    systemd.services =
      let
        mkBase = component: {
          description = "RKVM ${component}";
          wantedBy = [ "multi-user.target" ];
          after = {
            server = [ "network.target" ];
            client = [ "network-online.target" ];
          }.${component};
          wants = {
            server = [ ];
            client = [ "network-online.target" ];
          }.${component};
          serviceConfig = {
            ExecStart = "${cfg.package}/bin/rkvm-${component} ${toml.generate "rkvm-${component}.toml" cfg.${component}.settings}";
            Restart = "always";
            RestartSec = 5;
            Type = "simple";
          };
        };
      in
      {
        rkvm-server = mkIf cfg.server.enable (mkBase "server");
        rkvm-client = mkIf cfg.client.enable (mkBase "client");
      };
  };

}