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.

I took a screenshot with Niri, closed the window, opened Slack to paste it and nothing. Copied some text from a terminal, closed it, tried to paste in the browser amd gone again. After the third time I went looking for an explanation.

In Wayland, applications own their clipboard content. Unlike X11 where the X server manages it, when a Wayland application closes, the clipboard data goes with it. This is by design, and it’s annoying.

The solution

The fix involves three tools working together:

  • wl-clipboard: Provides wl-copy and wl-paste, the command-line clipboard utilities for Wayland
  • wl-clip-persist: Monitors the clipboard and automatically takes ownership of copied content when the original application closes, keeping it alive
  • cliphist: Watches clipboard changes and stores them in a database, giving you 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