Jump to content

TigerVNC: Difference between revisions

From Official NixOS Wiki
Johanno (talk | contribs)
Explain how to configure a systemd service in a configuration nix import
 
Nika03 (talk | contribs)
m The xorg package set has been deprecated, 'xorg.xhost' has been renamed to 'xhost' and 'xorg.xauth' has been renamed to 'xauth'
 
(One intermediate revision by one other user not shown)
Line 50: Line 50:


   config = lib.mkIf cfg.enable {
   config = lib.mkIf cfg.enable {
     environment.systemPackages = [ pkgs.tigervnc pkgs.xorg.xhost pkgs.xorg.xauth ];
     environment.systemPackages = [ pkgs.tigervnc pkgs.xhost pkgs.xauth ];


     systemd.user.services = lib.mapAttrs' (user: opts:
     systemd.user.services = lib.mapAttrs' (user: opts:
Line 62: Line 62:
           wantedBy = [ "graphical-session.target" ];
           wantedBy = [ "graphical-session.target" ];


           path = [ pkgs.tigervnc pkgs.xorg.xhost pkgs.xorg.xauth pkgs.coreutils ];
           path = [ pkgs.tigervnc pkgs.xhost pkgs.xauth pkgs.coreutils ];


           script = let
           script = let
Line 81: Line 81:
             if [ ! -r "$HOME/.vnc/passwd" ]; then
             if [ ! -r "$HOME/.vnc/passwd" ]; then
               echo "ERROR: Run: vncpasswd ~/.vnc/passwd" >&2
               echo "ERROR: Run: vncpasswd ~/.vnc/passwd" >&2
### CHANGE THE > in the above line TO A GREATER THAN SIGN! ###
               exit 1
               exit 1
             fi
             fi
Line 111: Line 112:
}
}


</nowiki>}}
</nowiki>|name=/etc/nixos/tigervnc.nix|lang=nix}}


For an initial setup you need to set your password with <code>vncpasswd</code>
For an initial setup you need to set your password with <code>vncpasswd</code>

Latest revision as of 16:51, 2 June 2026

In order to get TigerVNC to run on nixos without further hassle you can use the following config. (AI was heavily used to create it)

❄︎ /etc/nixos/configuration.nix
{
  imports = [
    ./tigervnc.nix
  ];

  ### tigervnc
  services.xserver.enable = true;  # Prerequisite

  services.tigervnc = {
    enable = true;
    users = {
      YOURUSERNAME = {
        localhost = false;  # true for SSH-only
      };
    };
  };
  ### tigervnc END
}

Here you need to replace all YOURUSERNAME with your user name.

Now the tigervnc file to import.

❄︎ /etc/nixos/tigervnc.nix
{ config, pkgs, lib, ... }:

let
  cfg = config.services.tigervnc;
in
{
  options.services.tigervnc = {
    enable = lib.mkEnableOption "TigerVNC x0vncserver (mirrors :0)";
    users = lib.mkOption {
      type = lib.types.attrsOf (lib.types.submodule ({ name, config, ... }: {
        options = {
          enableSharing = lib.mkOption { type = lib.types.bool; default = true; };
          port = lib.mkOption { type = lib.types.int; default = 5901; };
          localhost = lib.mkOption { type = lib.types.bool; default = false; };
          #geometry = lib.mkOption { type = lib.types.str; default = ""; };
        };
      }));
      default = { };
    };
  };

  config = lib.mkIf cfg.enable {
    environment.systemPackages = [ pkgs.tigervnc pkgs.xhost pkgs.xauth ];

    systemd.user.services = lib.mapAttrs' (user: opts:
      let svcName = "tigervnc-x0vncserver-${user}";
      in {
        name = svcName;
        value = lib.mkIf opts.enableSharing {
          description = "TigerVNC x0vncserver sharing :0 for ${user}";
          after = [ "graphical-session.target" ];
          partOf = [ "graphical-session.target" ];
          wantedBy = [ "graphical-session.target" ];

          path = [ pkgs.tigervnc pkgs.xhost pkgs.xauth pkgs.coreutils ];

          script = let
            # Build args array to avoid shell escaping hell
            vncArgs = [
              "-display :0"
              "-rfbport ${toString opts.port}"
              "-rfbauth $HOME/.vnc/passwd"
              "-AcceptSetDesktopSize=0"
              "-AlwaysShared"
            ] #++ lib.optionals (opts.geometry != "") [ "-Geometry ${opts.geometry}" ]
              ++ lib.optionals opts.localhost [ "-localhost" ];
          in ''
            set -euxo pipefail

            # Password check
            mkdir -p "$HOME/.vnc"
            if [ ! -r "$HOME/.vnc/passwd" ]; then
              echo "ERROR: Run: vncpasswd ~/.vnc/passwd" &gt;&2
### CHANGE THE &gt; in the above line TO A GREATER THAN SIGN! ###
              exit 1
            fi

            # X authority
            xhost +local:

            # Clean x0vncserver args
            ${pkgs.tigervnc}/bin/x0vncserver ${lib.concatStringsSep " " (map (arg: "${arg}") vncArgs)}
          '';

          preStop = ''
            xhost -local:
            pkill -f "x0vncserver.*${toString opts.port}"
          '';

          serviceConfig = {
            Type = "simple";
            Restart = "always";
            RestartSec = "5s";
            RuntimeDirectory = "vnc-${user}";
          };
        };
      }
    ) cfg.users;

    networking.firewall.allowedTCPPorts =
      lib.mapAttrsToList (_: opts: opts.port) cfg.users;
  };
}

For an initial setup you need to set your password with vncpasswd

I had issues due to an existing .vnc folder so I did: echo "YOUR_PW" | vncpasswd -f > ~/.vnc/passwd