From 47c74e4fd0bec8f412b912bdef054d514ad7ac59 Mon Sep 17 00:00:00 2001 From: Austin Horstman Date: Thu, 16 Oct 2025 19:17:14 -0500 Subject: [PATCH] wl-clip-persist: add module Signed-off-by: Austin Horstman --- modules/services/wl-clip-persist.nix | 99 +++++++++++++++++++ .../services/wl-clip-persist/default.nix | 7 ++ .../wl-clip-persist-advanced.nix | 24 +++++ .../wl-clip-persist/wl-clip-persist-basic.nix | 13 +++ .../wl-clip-persist-clipboard-both.nix | 13 +++ 5 files changed, 156 insertions(+) create mode 100644 modules/services/wl-clip-persist.nix create mode 100644 tests/modules/services/wl-clip-persist/default.nix create mode 100644 tests/modules/services/wl-clip-persist/wl-clip-persist-advanced.nix create mode 100644 tests/modules/services/wl-clip-persist/wl-clip-persist-basic.nix create mode 100644 tests/modules/services/wl-clip-persist/wl-clip-persist-clipboard-both.nix diff --git a/modules/services/wl-clip-persist.nix b/modules/services/wl-clip-persist.nix new file mode 100644 index 000000000..44ab48647 --- /dev/null +++ b/modules/services/wl-clip-persist.nix @@ -0,0 +1,99 @@ +{ + config, + lib, + pkgs, + ... +}: +let + cfg = config.services.wl-clip-persist; +in +{ + meta.maintainers = [ lib.maintainers.khaneliman ]; + + options.services.wl-clip-persist = { + enable = lib.mkEnableOption "wl-clip-persist, a Wayland clipboard persistence tool"; + + package = lib.mkPackageOption pkgs "wl-clip-persist" { }; + + clipboardType = lib.mkOption { + type = lib.types.enum [ + "regular" + "primary" + "both" + ]; + default = "regular"; + description = '' + The clipboard type to persist. + + - `regular`: Persist the regular clipboard only (recommended). + - `primary`: Persist the primary selection only. + - `both`: Persist both regular and primary clipboards. + + Note: Operating on the primary clipboard may have unintended side effects + for some applications. + ''; + }; + + extraOptions = lib.mkOption { + type = with lib.types; listOf str; + default = [ ]; + example = [ + "--write-timeout" + "1000" + "--ignore-event-on-error" + "--all-mime-type-regex" + "'(?i)^(?!image/).+'" + "--selection-size-limit" + "1048576" + ]; + description = '' + Extra command-line arguments to pass to wl-clip-persist. + + Available options include: + - `--write-timeout `: Timeout for writing clipboard data (default: 3000). + - `--ignore-event-on-error`: Only handle events without errors. + - `--all-mime-type-regex `: Filter events by MIME type regex. + - `--selection-size-limit `: Limit clipboard data size. + - `--reconnect-tries `: Number of reconnection attempts. + - `--reconnect-delay `: Delay between reconnect attempts (default: 100). + - `--disable-timestamps`: Disable log timestamps. + ''; + }; + + systemdTargets = lib.mkOption { + type = with lib.types; either (listOf str) str; + default = [ config.wayland.systemd.target ]; + defaultText = lib.literalExpression "[ config.wayland.systemd.target ]"; + example = "sway-session.target"; + description = '' + The systemd targets that will automatically start the wl-clip-persist service. + ''; + }; + }; + + config = lib.mkIf cfg.enable { + assertions = [ + (lib.hm.assertions.assertPlatform "services.wl-clip-persist" pkgs lib.platforms.linux) + ]; + + home.packages = [ cfg.package ]; + + systemd.user.services.wl-clip-persist = { + Unit = { + Description = "Wayland clipboard persistence daemon"; + PartOf = lib.toList cfg.systemdTargets; + After = lib.toList cfg.systemdTargets; + }; + + Service = { + Type = "simple"; + ExecStart = "${lib.getExe cfg.package} --clipboard ${cfg.clipboardType} ${lib.escapeShellArgs cfg.extraOptions}"; + Restart = "on-failure"; + }; + + Install = { + WantedBy = lib.toList cfg.systemdTargets; + }; + }; + }; +} diff --git a/tests/modules/services/wl-clip-persist/default.nix b/tests/modules/services/wl-clip-persist/default.nix new file mode 100644 index 000000000..eac41528f --- /dev/null +++ b/tests/modules/services/wl-clip-persist/default.nix @@ -0,0 +1,7 @@ +{ lib, pkgs, ... }: + +lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux { + wl-clip-persist-basic = ./wl-clip-persist-basic.nix; + wl-clip-persist-advanced = ./wl-clip-persist-advanced.nix; + wl-clip-persist-clipboard-both = ./wl-clip-persist-clipboard-both.nix; +} diff --git a/tests/modules/services/wl-clip-persist/wl-clip-persist-advanced.nix b/tests/modules/services/wl-clip-persist/wl-clip-persist-advanced.nix new file mode 100644 index 000000000..ebe1ef040 --- /dev/null +++ b/tests/modules/services/wl-clip-persist/wl-clip-persist-advanced.nix @@ -0,0 +1,24 @@ +{ + services.wl-clip-persist = { + enable = true; + clipboardType = "regular"; + extraOptions = [ + "--write-timeout" + "1000" + "--ignore-event-on-error" + "--selection-size-limit" + "1048576" + ]; + }; + + nmt.script = '' + servicePath=home-files/.config/systemd/user + + assertFileExists $servicePath/wl-clip-persist.service + assertFileRegex $servicePath/wl-clip-persist.service "Description=Wayland clipboard persistence daemon" + assertFileRegex $servicePath/wl-clip-persist.service " --clipboard regular " + assertFileRegex $servicePath/wl-clip-persist.service " --write-timeout 1000" + assertFileRegex $servicePath/wl-clip-persist.service " --ignore-event-on-error" + assertFileRegex $servicePath/wl-clip-persist.service " --selection-size-limit 1048576" + ''; +} diff --git a/tests/modules/services/wl-clip-persist/wl-clip-persist-basic.nix b/tests/modules/services/wl-clip-persist/wl-clip-persist-basic.nix new file mode 100644 index 000000000..695b3d47c --- /dev/null +++ b/tests/modules/services/wl-clip-persist/wl-clip-persist-basic.nix @@ -0,0 +1,13 @@ +{ + services.wl-clip-persist = { + enable = true; + }; + + nmt.script = '' + servicePath=home-files/.config/systemd/user + + assertFileExists $servicePath/wl-clip-persist.service + assertFileRegex $servicePath/wl-clip-persist.service "Description=Wayland clipboard persistence daemon" + assertFileRegex $servicePath/wl-clip-persist.service "ExecStart=.* --clipboard regular" + ''; +} diff --git a/tests/modules/services/wl-clip-persist/wl-clip-persist-clipboard-both.nix b/tests/modules/services/wl-clip-persist/wl-clip-persist-clipboard-both.nix new file mode 100644 index 000000000..e43ba389a --- /dev/null +++ b/tests/modules/services/wl-clip-persist/wl-clip-persist-clipboard-both.nix @@ -0,0 +1,13 @@ +{ + services.wl-clip-persist = { + enable = true; + clipboardType = "both"; + }; + + nmt.script = '' + servicePath=home-files/.config/systemd/user + + assertFileExists $servicePath/wl-clip-persist.service + assertFileRegex $servicePath/wl-clip-persist.service " --clipboard both " + ''; +}