使用Nix搭建RStudio环境后启动报错的技术求助
原Flake配置
用户使用的flake.nix如下:
{ description = "virtual environment with Rstudio, R, Shiny"; inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; inputs.flake-utils.url = "github:numtide/flake-utils"; outputs = { self, nixpkgs, flake-utils }: flake-utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; rWrapper=pkgs.rWrapper; rstudioWrapper=pkgs.rstudioWrapper; rstudioServerWrapper=pkgs.rstudioServerWrapper; Rpackages=with pkgs.rPackages;[shiny]; RwithPackages= rWrapper.override{ packages = Rpackages;}; RStudio-with-my-packages = rstudioWrapper.override{ packages = Rpackages; }; myDevTools = [ #qtbase #patch RwithPackages RStudio-with-my-packages ]; in { devShells.default = pkgs.mkShell { buildInputs = myDevTools; }; }); }
启动错误情况
执行nix develop进入虚拟环境后,运行rstudio出现第一个错误:
TTY detected. Printing informational message about logging
configuration. Logging configuration loaded from
'/etc/rstudio/logging.conf'. Logging to ''. qt.qpa.xcb: could not
connect to display qt.qpa.plugin: Could not load the Qt platform
plugin "xcb" in "" even though it was found. This application failed
to start because no Qt platform plugin could be initialized.
Reinstalling the application may fix this problem.Available platform plugins are: wayland-egl, wayland,
wayland-xcomposite-egl, wayland-xcomposite-glx, eglfs, linuxfb,
minimal, minimalegl, offscreen, vnc, xcb.Aborted
尝试设置export QT_QPA_PLATFORM=offscreen后,运行rstudio仍失败:
TTY detected. Printing informational message about logging
configuration. Logging configuration loaded from
'/etc/rstudio/logging.conf'. Logging to ''. QStandardPaths:
XDG_RUNTIME_DIR not set, defaulting to
'/tmp/nix-shell.aeqLGO/runtime-pierre' Fontconfig error: Cannot load
default config file: No such file: (null) Fontconfig error: No
writable cache directories Fontconfig error: No writable cache
directories Fontconfig error: No writable cache directories Fontconfig
error: No writable cache directories WebEngineContext used before
QtWebEngine::initialize() or OpenGL context creation failed. This
plugin does not support propagateSizeHints() This plugin does not
support raise() Failed to create OpenGL context for format
QSurfaceFormat(version 2.0, options
QFlagsQSurfaceFormat::FormatOption(), depthBufferSize 24,
redBufferSize -1, greenBufferSize -1, blueBufferSize -1,
alphaBufferSize -1, stencilBufferSize 8, samples 0, swapBehavior
QSurfaceFormat::DefaultSwapBehavior, swapInterval 1, colorSpace
QSurfaceFormat::DefaultColorSpace, profile QSurfaceFormat::NoProfile)Aborted
解决方案
1. 修改Flake配置,添加必要依赖与环境变量
更新flake.nix,补充缺失的依赖并配置正确的环境变量,无需手动设置:
{ description = "virtual environment with Rstudio, R, Shiny"; inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; inputs.flake-utils.url = "github:numtide/flake-utils"; outputs = { self, nixpkgs, flake-utils }: flake-utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; rWrapper = pkgs.rWrapper; rstudioWrapper = pkgs.rstudioWrapper; Rpackages = with pkgs.rPackages; [ shiny ]; RwithPackages = rWrapper.override { packages = Rpackages; }; RStudio-with-my-packages = rstudioWrapper.override { packages = Rpackages; }; myDevTools = [ pkgs.qtbase pkgs.fontconfig pkgs.xorg.libX11 pkgs.xorg.libXext pkgs.xorg.libXrender RwithPackages RStudio-with-my-packages ]; in { devShells.default = pkgs.mkShell { buildInputs = myDevTools; # 配置必要的环境变量 environmentVariables = { QT_QPA_PLATFORM = "xcb"; # RStudio为图形界面应用,不能使用offscreen模式 XDG_RUNTIME_DIR = "/tmp/rstudio-runtime-${builtins.getEnv "USER"}"; FONTCONFIG_FILE = "${pkgs.fontconfig.out}/etc/fonts/fonts.conf"; }; # 自动创建并设置运行目录权限 shellHook = '' mkdir -p $XDG_RUNTIME_DIR chmod 700 $XDG_RUNTIME_DIR ''; }; }); }
2. 关键修改说明
- 补充依赖:添加
qtbase修复Qt平台插件加载问题,fontconfig解决字体配置错误,Xorg相关库确保图形界面正常连接。 - 环境变量配置:
QT_QPA_PLATFORM设为xcb:RStudio是图形界面应用,offscreen模式会导致OpenGL初始化失败,不适用。XDG_RUNTIME_DIR指定专属可写目录:避免默认临时目录的权限问题。FONTCONFIG_FILE指定fontconfig配置路径:解决"无法加载默认配置文件"的错误。
- shellHook:自动创建运行目录并设置权限,确保目录可写。
3. 重新启动环境
执行以下命令更新环境并启动RStudio:
nix develop rstudio
内容的提问来源于stack exchange,提问作者Pierre-olivier Gendraud

