1 { config, lib, pkgs, ... }:
7 cfg = config.services.ttyd;
9 # Command line arguments for the ttyd daemon
10 args = [ "--port" (toString cfg.port) ]
11 ++ optionals (cfg.socket != null) [ "--interface" cfg.socket ]
12 ++ optionals (cfg.interface != null) [ "--interface" cfg.interface ]
13 ++ [ "--signal" (toString cfg.signal) ]
14 ++ (concatLists (mapAttrsToList (_k: _v: [ "--client-option" "${_k}=${_v}" ]) cfg.clientOptions))
15 ++ [ "--terminal-type" cfg.terminalType ]
16 ++ optionals cfg.checkOrigin [ "--check-origin" ]
17 ++ [ "--max-clients" (toString cfg.maxClients) ]
18 ++ optionals (cfg.indexFile != null) [ "--index" cfg.indexFile ]
19 ++ optionals cfg.enableIPv6 [ "--ipv6" ]
20 ++ optionals cfg.enableSSL [ "--ssl-cert" cfg.certFile
21 "--ssl-key" cfg.keyFile
22 "--ssl-ca" cfg.caFile ]
23 ++ [ "--debug" (toString cfg.logLevel) ];
33 enable = mkEnableOption (lib.mdDoc "ttyd daemon");
38 description = lib.mdDoc "Port to listen on (use 0 for random port)";
42 type = types.nullOr types.path;
44 example = "/var/run/ttyd.sock";
45 description = lib.mdDoc "UNIX domain socket path to bind.";
48 interface = mkOption {
49 type = types.nullOr types.str;
52 description = lib.mdDoc "Network interface to bind.";
56 type = types.nullOr types.str;
58 description = lib.mdDoc "Username for basic authentication.";
61 passwordFile = mkOption {
62 type = types.nullOr types.path;
64 apply = value: if value == null then null else toString value;
65 description = lib.mdDoc ''
66 File containing the password to use for basic authentication.
67 For insecurely putting the password in the globally readable store use
68 `pkgs.writeText "ttydpw" "MyPassword"`.
75 description = lib.mdDoc "Signal to send to the command on session close.";
78 clientOptions = mkOption {
79 type = types.attrsOf types.str;
81 example = literalExpression ''{
83 fontFamily = "Fira Code";
86 description = lib.mdDoc ''
87 Attribute set of client options for xtermjs.
88 <https://xtermjs.org/docs/api/terminal/interfaces/iterminaloptions/>
92 terminalType = mkOption {
94 default = "xterm-256color";
95 description = lib.mdDoc "Terminal type to report.";
98 checkOrigin = mkOption {
101 description = lib.mdDoc "Whether to allow a websocket connection from a different origin.";
104 maxClients = mkOption {
107 description = lib.mdDoc "Maximum clients to support (0, no limit)";
110 indexFile = mkOption {
111 type = types.nullOr types.path;
113 description = lib.mdDoc "Custom index.html path";
116 enableIPv6 = mkOption {
119 description = lib.mdDoc "Whether or not to enable IPv6 support.";
122 enableSSL = mkOption {
125 description = lib.mdDoc "Whether or not to enable SSL (https) support.";
128 certFile = mkOption {
129 type = types.nullOr types.path;
131 description = lib.mdDoc "SSL certificate file path.";
135 type = types.nullOr types.path;
137 apply = value: if value == null then null else toString value;
138 description = lib.mdDoc ''
140 For insecurely putting the keyFile in the globally readable store use
141 `pkgs.writeText "ttydKeyFile" "SSLKEY"`.
146 type = types.nullOr types.path;
148 description = lib.mdDoc "SSL CA file path for client certificate verification.";
151 logLevel = mkOption {
154 description = lib.mdDoc "Set log level.";
159 ###### implementation
161 config = mkIf cfg.enable {
164 [ { assertion = cfg.enableSSL
165 -> cfg.certFile != null && cfg.keyFile != null && cfg.caFile != null;
166 message = "SSL is enabled for ttyd, but no certFile, keyFile or caFile has been specefied."; }
167 { assertion = ! (cfg.interface != null && cfg.socket != null);
168 message = "Cannot set both interface and socket for ttyd."; }
169 { assertion = (cfg.username != null) == (cfg.passwordFile != null);
170 message = "Need to set both username and passwordFile for ttyd"; }
173 systemd.services.ttyd = {
174 description = "ttyd Web Server Daemon";
176 wantedBy = [ "multi-user.target" ];
179 # Runs login which needs to be run as root
180 # login: Cannot possibly work without effective root
184 script = if cfg.passwordFile != null then ''
185 PASSWORD=$(cat ${escapeShellArg cfg.passwordFile})
186 ${pkgs.ttyd}/bin/ttyd ${lib.escapeShellArgs args} \
187 --credential ${escapeShellArg cfg.username}:"$PASSWORD" \
188 ${pkgs.shadow}/bin/login
191 ${pkgs.ttyd}/bin/ttyd ${lib.escapeShellArgs args} \
192 ${pkgs.shadow}/bin/login