Part of my NixOS migration series

This article continues from Setting up NixOS with Niri, Home Manager and a declarative desktop, where I set up NixOS from scratch.

If you’re using Niri compositor on Wayland with NixOS, you might have noticed that when you copy something from a terminal and close it, the clipboard content is lost. This is not a bug, it’s how Wayland handles clipboard ownership by design.

Unlike X11, where the clipboard is managed by the X server, Wayland applications own their clipboard content. When the application closes, so does the clipboard data. This can be particularly annoying when taking screenshots or copying text between terminals.

The solution

The fix involves two tools working together:

  • wl-clip-persist: Keeps clipboard content alive even after the source application closes
  • cliphist: Maintains a searchable history of everything you’ve copied

Installation

Add the required packages to your Niri system configuration:

modules/system/niri/default.nix
{ pkgs, ... }:
{
  environment.systemPackages = with pkgs; [
    niri
    # ... your other packages
    wl-clipboard
    cliphist
    wl-clip-persist
  ];
}

Configuration

Configure the clipboard managers to start automatically with Niri:

modules/user/niri/default.nix
programs.niri = {
  enable = true;
  settings = {
    spawn-at-startup = [
      # Your existing startup commands
      { command = ["wl-clip-persist" "--clipboard" "both"]; }
      { command = ["sh" "-c" "wl-paste --type text --watch cliphist store"]; }
      { command = ["sh" "-c" "wl-paste --type image --watch cliphist store"]; }
    ];
  };
};

Note

The --clipboard both flag ensures both the regular clipboard and primary selection are monitored.

Applying changes

Rebuild your NixOS configuration:

sudo nixos-rebuild switch

Then restart Niri (log out and log back in, or restart your system).

Verification

Test the clipboard persistence:

  1. Open a terminal and copy some text
  2. Close the terminal
  3. Open a new terminal and paste

The text should persist. You can also check the clipboard history:

cliphist list

This will show all your clipboard history, including both text and images.

How it works

  • wl-clip-persist monitors the clipboard and automatically takes ownership of copied content when the original application closes
  • cliphist watches clipboard changes and stores them in a database for later retrieval
  • Both tools work seamlessly together without interfering with normal clipboard operations

Now you can take screenshots with Niri’s native screenshot tool, close applications without losing clipboard data, and access your clipboard history when needed.