2019-09-12 18:47:15 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.ttyd;
|
|
|
|
|
|
|
|
# Command line arguments for the ttyd daemon
|
|
|
|
args = [ "--port" (toString cfg.port) ]
|
|
|
|
++ optionals (cfg.socket != null) [ "--interface" cfg.socket ]
|
|
|
|
++ optionals (cfg.interface != null) [ "--interface" cfg.interface ]
|
|
|
|
++ [ "--signal" (toString cfg.signal) ]
|
|
|
|
++ (concatLists (mapAttrsToList (_k: _v: [ "--client-option" "${_k}=${_v}" ]) cfg.clientOptions))
|
|
|
|
++ [ "--terminal-type" cfg.terminalType ]
|
|
|
|
++ optionals cfg.checkOrigin [ "--check-origin" ]
|
|
|
|
++ [ "--max-clients" (toString cfg.maxClients) ]
|
|
|
|
++ optionals (cfg.indexFile != null) [ "--index" cfg.indexFile ]
|
|
|
|
++ optionals cfg.enableIPv6 [ "--ipv6" ]
|
|
|
|
++ optionals cfg.enableSSL [ "--ssl-cert" cfg.certFile
|
|
|
|
"--ssl-key" cfg.keyFile
|
|
|
|
"--ssl-ca" cfg.caFile ]
|
|
|
|
++ [ "--debug" (toString cfg.logLevel) ];
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
services.ttyd = {
|
|
|
|
enable = mkEnableOption "ttyd daemon";
|
|
|
|
|
|
|
|
port = mkOption {
|
2021-06-18 16:28:17 +01:00
|
|
|
type = types.port;
|
2019-09-12 18:47:15 +01:00
|
|
|
default = 7681;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Port to listen on (use 0 for random port)";
|
2019-09-12 18:47:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
socket = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
example = "/var/run/ttyd.sock";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "UNIX domain socket path to bind.";
|
2019-09-12 18:47:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
interface = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "eth0";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Network interface to bind.";
|
2019-09-12 18:47:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
username = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Username for basic authentication.";
|
2019-09-12 18:47:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
apply = value: if value == null then null else toString value;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-09-12 18:47:15 +01:00
|
|
|
File containing the password to use for basic authentication.
|
|
|
|
For insecurely putting the password in the globally readable store use
|
2022-07-28 22:19:15 +01:00
|
|
|
`pkgs.writeText "ttydpw" "MyPassword"`.
|
2019-09-12 18:47:15 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
signal = mkOption {
|
|
|
|
type = types.ints.u8;
|
|
|
|
default = 1;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Signal to send to the command on session close.";
|
2019-09-12 18:47:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
clientOptions = mkOption {
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
default = {};
|
2021-10-03 17:06:03 +01:00
|
|
|
example = literalExpression ''{
|
2019-09-12 18:47:15 +01:00
|
|
|
fontSize = "16";
|
|
|
|
fontFamily = "Fira Code";
|
|
|
|
|
|
|
|
}'';
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-09-12 18:47:15 +01:00
|
|
|
Attribute set of client options for xtermjs.
|
2022-07-28 22:19:15 +01:00
|
|
|
<https://xtermjs.org/docs/api/terminal/interfaces/iterminaloptions/>
|
2019-09-12 18:47:15 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
terminalType = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "xterm-256color";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Terminal type to report.";
|
2019-09-12 18:47:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
checkOrigin = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Whether to allow a websocket connection from a different origin.";
|
2019-09-12 18:47:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
maxClients = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 0;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Maximum clients to support (0, no limit)";
|
2019-09-12 18:47:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
indexFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Custom index.html path";
|
2019-09-12 18:47:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enableIPv6 = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Whether or not to enable IPv6 support.";
|
2019-09-12 18:47:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enableSSL = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Whether or not to enable SSL (https) support.";
|
2019-09-12 18:47:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
certFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "SSL certificate file path.";
|
2019-09-12 18:47:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
keyFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
apply = value: if value == null then null else toString value;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-09-12 18:47:15 +01:00
|
|
|
SSL key file path.
|
|
|
|
For insecurely putting the keyFile in the globally readable store use
|
2022-07-28 22:19:15 +01:00
|
|
|
`pkgs.writeText "ttydKeyFile" "SSLKEY"`.
|
2019-09-12 18:47:15 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
caFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "SSL CA file path for client certificate verification.";
|
2019-09-12 18:47:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
logLevel = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 7;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Set log level.";
|
2019-09-12 18:47:15 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
assertions =
|
|
|
|
[ { assertion = cfg.enableSSL
|
|
|
|
-> cfg.certFile != null && cfg.keyFile != null && cfg.caFile != null;
|
|
|
|
message = "SSL is enabled for ttyd, but no certFile, keyFile or caFile has been specefied."; }
|
|
|
|
{ assertion = ! (cfg.interface != null && cfg.socket != null);
|
|
|
|
message = "Cannot set both interface and socket for ttyd."; }
|
|
|
|
{ assertion = (cfg.username != null) == (cfg.passwordFile != null);
|
|
|
|
message = "Need to set both username and passwordFile for ttyd"; }
|
|
|
|
];
|
|
|
|
|
|
|
|
systemd.services.ttyd = {
|
|
|
|
description = "ttyd Web Server Daemon";
|
|
|
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
# Runs login which needs to be run as root
|
|
|
|
# login: Cannot possibly work without effective root
|
|
|
|
User = "root";
|
|
|
|
};
|
|
|
|
|
|
|
|
script = if cfg.passwordFile != null then ''
|
|
|
|
PASSWORD=$(cat ${escapeShellArg cfg.passwordFile})
|
|
|
|
${pkgs.ttyd}/bin/ttyd ${lib.escapeShellArgs args} \
|
|
|
|
--credential ${escapeShellArg cfg.username}:"$PASSWORD" \
|
|
|
|
${pkgs.shadow}/bin/login
|
|
|
|
''
|
|
|
|
else ''
|
|
|
|
${pkgs.ttyd}/bin/ttyd ${lib.escapeShellArgs args} \
|
|
|
|
${pkgs.shadow}/bin/login
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|