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 ''
84 fontFamily = "Fira Code";
87 description = lib.mdDoc ''
88 Attribute set of client options for xtermjs.
89 <https://xtermjs.org/docs/api/terminal/interfaces/iterminaloptions/>
93 terminalType = mkOption {
95 default = "xterm-256color";
96 description = lib.mdDoc "Terminal type to report.";
99 checkOrigin = mkOption {
102 description = lib.mdDoc "Whether to allow a websocket connection from a different origin.";
105 maxClients = mkOption {
108 description = lib.mdDoc "Maximum clients to support (0, no limit)";
111 indexFile = mkOption {
112 type = types.nullOr types.path;
114 description = lib.mdDoc "Custom index.html path";
117 enableIPv6 = mkOption {
120 description = lib.mdDoc "Whether or not to enable IPv6 support.";
123 enableSSL = mkOption {
126 description = lib.mdDoc "Whether or not to enable SSL (https) support.";
129 certFile = mkOption {
130 type = types.nullOr types.path;
132 description = lib.mdDoc "SSL certificate file path.";
136 type = types.nullOr types.path;
138 apply = value: if value == null then null else toString value;
139 description = lib.mdDoc ''
141 For insecurely putting the keyFile in the globally readable store use
142 `pkgs.writeText "ttydKeyFile" "SSLKEY"`.
147 type = types.nullOr types.path;
149 description = lib.mdDoc "SSL CA file path for client certificate verification.";
152 logLevel = mkOption {
155 description = lib.mdDoc "Set log level.";
160 ###### implementation
162 config = mkIf cfg.enable {
165 [ { assertion = cfg.enableSSL
166 -> cfg.certFile != null && cfg.keyFile != null && cfg.caFile != null;
167 message = "SSL is enabled for ttyd, but no certFile, keyFile or caFile has been specified."; }
168 { assertion = ! (cfg.interface != null && cfg.socket != null);
169 message = "Cannot set both interface and socket for ttyd."; }
170 { assertion = (cfg.username != null) == (cfg.passwordFile != null);
171 message = "Need to set both username and passwordFile for ttyd"; }
174 systemd.services.ttyd = {
175 description = "ttyd Web Server Daemon";
177 wantedBy = [ "multi-user.target" ];
180 # Runs login which needs to be run as root
181 # login: Cannot possibly work without effective root
185 script = if cfg.passwordFile != null then ''
186 PASSWORD=$(cat ${escapeShellArg cfg.passwordFile})
187 ${pkgs.ttyd}/bin/ttyd ${lib.escapeShellArgs args} \
188 --credential ${escapeShellArg cfg.username}:"$PASSWORD" \
189 ${pkgs.shadow}/bin/login
192 ${pkgs.ttyd}/bin/ttyd ${lib.escapeShellArgs args} \
193 ${pkgs.shadow}/bin/login