2017-01-05 23:14:35 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.xrdp;
|
2018-11-08 10:59:03 +00:00
|
|
|
confDir = pkgs.runCommand "xrdp.conf" { preferLocalBuild = true; } ''
|
2017-01-05 23:14:35 +00:00
|
|
|
mkdir $out
|
|
|
|
|
|
|
|
cp ${cfg.package}/etc/xrdp/{km-*,xrdp,sesman,xrdp_keyboard}.ini $out
|
|
|
|
|
|
|
|
cat > $out/startwm.sh <<EOF
|
|
|
|
#!/bin/sh
|
|
|
|
. /etc/profile
|
|
|
|
${cfg.defaultWindowManager}
|
|
|
|
EOF
|
|
|
|
chmod +x $out/startwm.sh
|
2017-04-29 17:23:21 +00:00
|
|
|
|
2017-01-05 23:14:35 +00:00
|
|
|
substituteInPlace $out/xrdp.ini \
|
2018-12-19 21:41:17 +00:00
|
|
|
--replace "#rsakeys_ini=" "rsakeys_ini=/run/xrdp/rsakeys.ini" \
|
2017-01-05 23:14:35 +00:00
|
|
|
--replace "certificate=" "certificate=${cfg.sslCert}" \
|
|
|
|
--replace "key_file=" "key_file=${cfg.sslKey}" \
|
|
|
|
--replace LogFile=xrdp.log LogFile=/dev/null \
|
|
|
|
--replace EnableSyslog=true EnableSyslog=false
|
|
|
|
|
|
|
|
substituteInPlace $out/sesman.ini \
|
|
|
|
--replace LogFile=xrdp-sesman.log LogFile=/dev/null \
|
|
|
|
--replace EnableSyslog=1 EnableSyslog=0
|
2019-01-16 22:07:30 +00:00
|
|
|
|
|
|
|
# Ensure that clipboard works for non-ASCII characters
|
|
|
|
sed -i -e '/.*SessionVariables.*/ a\
|
|
|
|
LANG=${config.i18n.defaultLocale}\
|
|
|
|
LOCALE_ARCHIVE=${config.i18n.glibcLocales}/lib/locale/locale-archive
|
|
|
|
' $out/sesman.ini
|
2017-01-05 23:14:35 +00:00
|
|
|
'';
|
|
|
|
in
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.xrdp = {
|
|
|
|
|
2018-10-05 06:14:45 +00:00
|
|
|
enable = mkEnableOption "xrdp, the Remote Desktop Protocol server";
|
2017-01-05 23:14:35 +00:00
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.xrdp;
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = literalExpression "pkgs.xrdp";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2017-01-05 23:14:35 +00:00
|
|
|
The package to use for the xrdp daemon's binary.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 3389;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2017-01-05 23:14:35 +00:00
|
|
|
Specifies on which port the xrdp daemon listens.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-06-10 04:50:29 +00:00
|
|
|
openFirewall = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Whether to open the firewall for the specified RDP port.";
|
2021-06-10 04:50:29 +00:00
|
|
|
};
|
|
|
|
|
2017-01-05 23:14:35 +00:00
|
|
|
sslKey = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "/etc/xrdp/key.pem";
|
|
|
|
example = "/path/to/your/key.pem";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2017-01-05 23:14:35 +00:00
|
|
|
ssl private key path
|
|
|
|
A self-signed certificate will be generated if file not exists.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
sslCert = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "/etc/xrdp/cert.pem";
|
|
|
|
example = "/path/to/your/cert.pem";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2017-01-05 23:14:35 +00:00
|
|
|
ssl certificate path
|
|
|
|
A self-signed certificate will be generated if file not exists.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
defaultWindowManager = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "xterm";
|
|
|
|
example = "xfce4-session";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2017-01-05 23:14:35 +00:00
|
|
|
The script to run when user log in, usually a window manager, e.g. "icewm", "xfce4-session"
|
|
|
|
This is per-user overridable, if file ~/startwm.sh exists it will be used instead.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-12-09 13:42:46 +00:00
|
|
|
confDir = mkOption {
|
2021-12-09 22:08:45 +00:00
|
|
|
type = types.path;
|
2021-12-09 13:42:46 +00:00
|
|
|
default = confDir;
|
2021-12-18 08:28:23 +00:00
|
|
|
defaultText = literalDocBook "generated from configuration";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "The location of the config files for xrdp.";
|
2021-12-09 13:42:46 +00:00
|
|
|
};
|
2017-01-05 23:14:35 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2021-06-10 04:50:29 +00:00
|
|
|
networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];
|
|
|
|
|
2017-05-02 21:07:57 +00:00
|
|
|
# xrdp can run X11 program even if "services.xserver.enable = false"
|
2018-08-15 08:55:35 +00:00
|
|
|
xdg = {
|
|
|
|
autostart.enable = true;
|
|
|
|
menus.enable = true;
|
|
|
|
mime.enable = true;
|
|
|
|
icons.enable = true;
|
|
|
|
};
|
|
|
|
|
2018-06-17 11:23:30 +00:00
|
|
|
fonts.enableDefaultFonts = mkDefault true;
|
2017-05-02 21:07:57 +00:00
|
|
|
|
2017-01-05 23:14:35 +00:00
|
|
|
systemd = {
|
|
|
|
services.xrdp = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
description = "xrdp daemon";
|
|
|
|
requires = [ "xrdp-sesman.service" ];
|
|
|
|
preStart = ''
|
|
|
|
# prepare directory for unix sockets (the sockets will be owned by loggedinuser:xrdp)
|
|
|
|
mkdir -p /tmp/.xrdp || true
|
|
|
|
chown xrdp:xrdp /tmp/.xrdp
|
|
|
|
chmod 3777 /tmp/.xrdp
|
|
|
|
|
|
|
|
# generate a self-signed certificate
|
|
|
|
if [ ! -s ${cfg.sslCert} -o ! -s ${cfg.sslKey} ]; then
|
|
|
|
mkdir -p $(dirname ${cfg.sslCert}) || true
|
|
|
|
mkdir -p $(dirname ${cfg.sslKey}) || true
|
|
|
|
${pkgs.openssl.bin}/bin/openssl req -x509 -newkey rsa:2048 -sha256 -nodes -days 365 \
|
|
|
|
-subj /C=US/ST=CA/L=Sunnyvale/O=xrdp/CN=www.xrdp.org \
|
|
|
|
-config ${cfg.package}/share/xrdp/openssl.conf \
|
|
|
|
-keyout ${cfg.sslKey} -out ${cfg.sslCert}
|
|
|
|
chown root:xrdp ${cfg.sslKey} ${cfg.sslCert}
|
|
|
|
chmod 440 ${cfg.sslKey} ${cfg.sslCert}
|
|
|
|
fi
|
2018-12-19 21:41:17 +00:00
|
|
|
if [ ! -s /run/xrdp/rsakeys.ini ]; then
|
|
|
|
mkdir -p /run/xrdp
|
|
|
|
${cfg.package}/bin/xrdp-keygen xrdp /run/xrdp/rsakeys.ini
|
2017-04-29 17:23:21 +00:00
|
|
|
fi
|
2017-01-05 23:14:35 +00:00
|
|
|
'';
|
|
|
|
serviceConfig = {
|
|
|
|
User = "xrdp";
|
|
|
|
Group = "xrdp";
|
|
|
|
PermissionsStartOnly = true;
|
2021-12-09 13:42:46 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/xrdp --nodaemon --port ${toString cfg.port} --config ${cfg.confDir}/xrdp.ini";
|
2017-01-05 23:14:35 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
services.xrdp-sesman = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
description = "xrdp session manager";
|
2017-05-02 18:46:53 +00:00
|
|
|
restartIfChanged = false; # do not restart on "nixos-rebuild switch". like "display-manager", it can have many interactive programs as children
|
2017-01-05 23:14:35 +00:00
|
|
|
serviceConfig = {
|
2021-12-09 13:42:46 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/xrdp-sesman --nodaemon --config ${cfg.confDir}/sesman.ini";
|
2017-05-02 18:46:53 +00:00
|
|
|
ExecStop = "${pkgs.coreutils}/bin/kill -INT $MAINPID";
|
2017-01-05 23:14:35 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
users.users.xrdp = {
|
|
|
|
description = "xrdp daemon user";
|
|
|
|
isSystemUser = true;
|
|
|
|
group = "xrdp";
|
|
|
|
};
|
|
|
|
users.groups.xrdp = {};
|
|
|
|
|
|
|
|
security.pam.services.xrdp-sesman = { allowNullPassword = true; startSession = true; };
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|