1 { config, lib, pkgs, ... }:
6 cfg = config.services.xrdp;
8 confDir = pkgs.runCommand "xrdp.conf" { preferLocalBuild = true; } ''
11 cp -r ${cfg.package}/etc/xrdp/* $out
14 cat > $out/startwm.sh <<EOF
17 ${lib.optionalString cfg.audio.enable "${cfg.audio.package}/libexec/pulsaudio-xrdp-module/pulseaudio_xrdp_init"}
18 ${cfg.defaultWindowManager}
20 chmod +x $out/startwm.sh
22 substituteInPlace $out/xrdp.ini \
23 --replace "#rsakeys_ini=" "rsakeys_ini=/run/xrdp/rsakeys.ini" \
24 --replace "certificate=" "certificate=${cfg.sslCert}" \
25 --replace "key_file=" "key_file=${cfg.sslKey}" \
26 --replace LogFile=xrdp.log LogFile=/dev/null \
27 --replace EnableSyslog=true EnableSyslog=false
29 substituteInPlace $out/sesman.ini \
30 --replace LogFile=xrdp-sesman.log LogFile=/dev/null \
31 --replace EnableSyslog=1 EnableSyslog=0 \
32 --replace startwm.sh $out/startwm.sh \
33 --replace reconnectwm.sh $out/reconnectwm.sh \
35 # Ensure that clipboard works for non-ASCII characters
36 sed -i -e '/.*SessionVariables.*/ a\
37 LANG=${config.i18n.defaultLocale}\
38 LOCALE_ARCHIVE=${config.i18n.glibcLocales}/lib/locale/locale-archive
41 ${cfg.extraConfDirCommands}
52 enable = mkEnableOption "xrdp, the Remote Desktop Protocol server";
54 package = mkPackageOption pkgs "xrdp" { };
57 enable = mkEnableOption "audio support for xrdp sessions. So far it only works with PulseAudio sessions on the server side. No PipeWire support yet";
58 package = mkPackageOption pkgs "pulseaudio-module-xrdp" {};
65 Specifies on which port the xrdp daemon listens.
69 openFirewall = mkOption {
72 description = "Whether to open the firewall for the specified RDP port.";
77 default = "/etc/xrdp/key.pem";
78 example = "/path/to/your/key.pem";
81 A self-signed certificate will be generated if file not exists.
87 default = "/etc/xrdp/cert.pem";
88 example = "/path/to/your/cert.pem";
91 A self-signed certificate will be generated if file not exists.
95 defaultWindowManager = mkOption {
98 example = "xfce4-session";
100 The script to run when user log in, usually a window manager, e.g. "icewm", "xfce4-session"
101 This is per-user overridable, if file ~/startwm.sh exists it will be used instead.
110 Configuration directory of xrdp and sesman.
112 Changes to this must be made through extraConfDirCommands.
117 extraConfDirCommands = mkOption {
121 Extra commands to run on the default confDir derivation.
124 substituteInPlace $out/sesman.ini \
125 --replace LogLevel=INFO LogLevel=DEBUG \
126 --replace LogFile=/dev/null LogFile=/var/log/xrdp.log
132 ###### implementation
134 config = lib.mkMerge [
135 (mkIf cfg.audio.enable {
136 environment.systemPackages = [ cfg.audio.package ]; # needed for autostart
138 hardware.pulseaudio.extraModules = [ cfg.audio.package ];
143 networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];
145 # xrdp can run X11 program even if "services.xserver.enable = false"
147 autostart.enable = true;
153 fonts.enableDefaultPackages = mkDefault true;
155 environment.etc."xrdp".source = "${confDir}/*";
159 wantedBy = [ "multi-user.target" ];
160 after = [ "network.target" ];
161 description = "xrdp daemon";
162 requires = [ "xrdp-sesman.service" ];
164 # prepare directory for unix sockets (the sockets will be owned by loggedinuser:xrdp)
165 mkdir -p /tmp/.xrdp || true
166 chown xrdp:xrdp /tmp/.xrdp
167 chmod 3777 /tmp/.xrdp
169 # generate a self-signed certificate
170 if [ ! -s ${cfg.sslCert} -o ! -s ${cfg.sslKey} ]; then
171 mkdir -p $(dirname ${cfg.sslCert}) || true
172 mkdir -p $(dirname ${cfg.sslKey}) || true
173 ${lib.getExe pkgs.openssl} req -x509 -newkey rsa:2048 -sha256 -nodes -days 365 \
174 -subj /C=US/ST=CA/L=Sunnyvale/O=xrdp/CN=www.xrdp.org \
175 -config ${cfg.package}/share/xrdp/openssl.conf \
176 -keyout ${cfg.sslKey} -out ${cfg.sslCert}
177 chown root:xrdp ${cfg.sslKey} ${cfg.sslCert}
178 chmod 440 ${cfg.sslKey} ${cfg.sslCert}
180 if [ ! -s /run/xrdp/rsakeys.ini ]; then
182 ${pkgs.xrdp}/bin/xrdp-keygen xrdp /run/xrdp/rsakeys.ini
188 PermissionsStartOnly = true;
189 ExecStart = "${pkgs.xrdp}/bin/xrdp --nodaemon --port ${toString cfg.port} --config ${confDir}/xrdp.ini";
193 services.xrdp-sesman = {
194 wantedBy = [ "multi-user.target" ];
195 after = [ "network.target" ];
196 description = "xrdp session manager";
197 restartIfChanged = false; # do not restart on "nixos-rebuild switch". like "display-manager", it can have many interactive programs as children
199 ExecStart = "${pkgs.xrdp}/bin/xrdp-sesman --nodaemon --config ${confDir}/sesman.ini";
200 ExecStop = "${pkgs.coreutils}/bin/kill -INT $MAINPID";
207 description = "xrdp daemon user";
211 users.groups.xrdp = {};
213 security.pam.services.xrdp-sesman = {
214 allowNullPassword = true;