From f15be4feb6e98fc2c52d4f8088400619381fd171 Mon Sep 17 00:00:00 2001 From: Aguirre Matteo <158215792+aguirre-matteo@users.noreply.github.com> Date: Fri, 2 May 2025 13:22:35 +0000 Subject: [PATCH] clipcat: add module (#6946) --- modules/modules.nix | 1 + modules/services/clipcat.nix | 164 ++++++++++++++++++ tests/default.nix | 1 + tests/modules/services/clipcat/cfg/ctl.toml | 7 + .../modules/services/clipcat/cfg/daemon.toml | 12 ++ tests/modules/services/clipcat/cfg/menu.toml | 12 ++ tests/modules/services/clipcat/default.nix | 1 + .../services/clipcat/example-config.nix | 65 +++++++ 8 files changed, 263 insertions(+) create mode 100644 modules/services/clipcat.nix create mode 100644 tests/modules/services/clipcat/cfg/ctl.toml create mode 100644 tests/modules/services/clipcat/cfg/daemon.toml create mode 100644 tests/modules/services/clipcat/cfg/menu.toml create mode 100644 tests/modules/services/clipcat/default.nix create mode 100644 tests/modules/services/clipcat/example-config.nix diff --git a/modules/modules.nix b/modules/modules.nix index 417e42d4..30e645e0 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -333,6 +333,7 @@ let ./services/cachix-agent.nix ./services/caffeine.nix ./services/cbatticon.nix + ./services/clipcat.nix ./services/cliphist.nix ./services/clipman.nix ./services/clipmenu.nix diff --git a/modules/services/clipcat.nix b/modules/services/clipcat.nix new file mode 100644 index 00000000..dd06a6e1 --- /dev/null +++ b/modules/services/clipcat.nix @@ -0,0 +1,164 @@ +{ + lib, + pkgs, + config, + ... +}: +let + inherit (lib) + types + mkIf + mkEnableOption + mkPackageOption + mkOption + ; + + cfg = config.services.clipcat; + + formatter = pkgs.formats.toml { }; +in +{ + meta.maintainers = with lib.hm.maintainers; [ aguirre-matteo ]; + + options.services.clipcat = { + enable = mkEnableOption "clipcat"; + package = mkPackageOption pkgs "clipcat" { }; + enableZshIntegration = lib.hm.shell.mkZshIntegrationOption { inherit config; }; + enableSystemdUnit = mkOption { + type = types.bool; + default = true; + example = false; + description = '' + Enable clipcat's Systemd Unit. + ''; + }; + daemonSettings = mkOption { + type = formatter.type; + default = { + daemonize = true; + }; + example = '' + { + daemonize = true; + max_history = 50; + history_file_path = "/home//.cache/clipcat/clipcatd-history"; + pid_file = "/run/user//clipcatd.pid"; + primary_threshold_ms = 5000; + log = { + file_path = "/path/to/log/file"; + emit_journald = true; + emit_stdout = false; + emit_stderr = false; + level = "INFO"; + }; + } + ''; + description = '' + Configuration settings for clipcatd. All available options can be found + here: . + ''; + }; + ctlSettings = mkOption { + type = formatter.type; + default = { }; + example = '' + { + server_endpoint = "/run/user//clipcat/grpc.sock"; + log = { + file_path = "/path/to/log/file"; + emit_journald = true; + emit_stdout = false; + emit_stderr = false; + level = "INFO"; + }; + } + ''; + description = '' + Configuration settings for clipcatctl. All available options can be found + here: . + ''; + }; + menuSettings = mkOption { + type = formatter.type; + default = { }; + example = '' + { + server_endpoint = "/run/user//clipcat/grpc.sock"; + finder = "rofi"; + rofi = { + line_length = 100; + menu_length = 30; + menu_prompt = "Clipcat"; + extra_arguments = [ + "-mesg" + "Please select a clip" + ]; + }; + dmenu = { + line_length = 100; + menu_length = 30; + menu_prompt = "Clipcat"; + }; + } + ''; + description = '' + Configuration settings for clipcat-menu. All available options can be found + here: . + ''; + }; + }; + + config = mkIf cfg.enable { + assertions = [ + (lib.hm.assertions.assertPlatform "services.clipcat" pkgs lib.platforms.linux) + ]; + + home.packages = [ cfg.package ]; + + programs.zsh.initContent = mkIf cfg.enableZshIntegration '' + if type clipcat-menu >/dev/null 2>&1; then + alias clipedit=' clipcat-menu --finder=builtin edit' + alias clipdel=' clipcat-menu --finder=builtin remove' + + bindkey -s '^\' "^Q clipcat-menu --finder=builtin insert ^J" + bindkey -s '^]' "^Q clipcat-menu --finder=builtin remove ^J" + fi + ''; + + systemd.user.services.clipcat = mkIf cfg.enableSystemdUnit { + Unit = { + Description = "Clipcat Daemon"; + PartOf = "graphical-session.target"; + }; + + Install.WantedBy = [ "graphical-session.target" ]; + + Service = { + ExecStartPre = "${pkgs.writeShellScript "clipcatd-exec-start-pre" '' + PATH=/run/current-system/sw/bin: + rm -f %t/clipcat/grpc.sock + ''}"; + + ExecStart = "${pkgs.writeShellScript "clipcatd-exec-start" '' + PATH=/run/current-system/sw/bin: + ${cfg.package}/bin/clipcatd --no-daemon --replace + ''}"; + + Restart = "on-failure"; + Type = "simple"; + }; + }; + + xdg.configFile = { + "clipcat/clipcatd.toml" = mkIf (cfg.daemonSettings != { }) { + source = formatter.generate "clipcatd.toml" cfg.daemonSettings; + }; + "clipcat/clipcatctl.toml" = mkIf (cfg.ctlSettings != { }) { + source = formatter.generate "clipcatctl.toml" cfg.ctlSettings; + }; + "clipcat/clipcat-menu.toml" = mkIf (cfg.menuSettings != { }) { + source = formatter.generate "clipcat-menu.toml" cfg.menuSettings; + }; + }; + }; +} diff --git a/tests/default.nix b/tests/default.nix index 67345ce1..77a3dac5 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -408,6 +408,7 @@ import nmtSrc { ./modules/services/blanket ./modules/services/borgmatic ./modules/services/cachix-agent + ./modules/services/clipcat ./modules/services/cliphist ./modules/services/clipman ./modules/services/clipse diff --git a/tests/modules/services/clipcat/cfg/ctl.toml b/tests/modules/services/clipcat/cfg/ctl.toml new file mode 100644 index 00000000..d1dd2396 --- /dev/null +++ b/tests/modules/services/clipcat/cfg/ctl.toml @@ -0,0 +1,7 @@ +server_endpoint = "/run/user//clipcat/grpc.sock" +[log] +emit_journald = true +emit_stderr = false +emit_stdout = false +file_path = "/path/to/log/file" +level = "INFO" diff --git a/tests/modules/services/clipcat/cfg/daemon.toml b/tests/modules/services/clipcat/cfg/daemon.toml new file mode 100644 index 00000000..7bdd9185 --- /dev/null +++ b/tests/modules/services/clipcat/cfg/daemon.toml @@ -0,0 +1,12 @@ +daemonize = true +history_file_path = "/home//.cache/clipcat/clipcatd-history" +max_history = 50 +pid_file = "/run/user//clipcatd.pid" +primary_threshold_ms = 5000 + +[log] +emit_journald = true +emit_stderr = false +emit_stdout = false +file_path = "/path/to/log/file" +level = "INFO" diff --git a/tests/modules/services/clipcat/cfg/menu.toml b/tests/modules/services/clipcat/cfg/menu.toml new file mode 100644 index 00000000..41011bdc --- /dev/null +++ b/tests/modules/services/clipcat/cfg/menu.toml @@ -0,0 +1,12 @@ +finder = "rofi" +server_endpoint = "/run/user//clipcat/grpc.sock" +[dmenu] +line_length = 100 +menu_length = 30 +menu_prompt = "Clipcat" + +[rofi] +extra_arguments = ["-mesg", "Please select a clip"] +line_length = 100 +menu_length = 30 +menu_prompt = "Clipcat" diff --git a/tests/modules/services/clipcat/default.nix b/tests/modules/services/clipcat/default.nix new file mode 100644 index 00000000..74202d0d --- /dev/null +++ b/tests/modules/services/clipcat/default.nix @@ -0,0 +1 @@ +{ clipcat-example-config = ./example-config.nix; } diff --git a/tests/modules/services/clipcat/example-config.nix b/tests/modules/services/clipcat/example-config.nix new file mode 100644 index 00000000..0be61c67 --- /dev/null +++ b/tests/modules/services/clipcat/example-config.nix @@ -0,0 +1,65 @@ +{ + services.clipcat = { + enable = true; + daemonSettings = { + daemonize = true; + max_history = 50; + history_file_path = "/home//.cache/clipcat/clipcatd-history"; + pid_file = "/run/user//clipcatd.pid"; + primary_threshold_ms = 5000; + log = { + file_path = "/path/to/log/file"; + emit_journald = true; + emit_stdout = false; + emit_stderr = false; + level = "INFO"; + }; + }; + + ctlSettings = { + server_endpoint = "/run/user//clipcat/grpc.sock"; + log = { + file_path = "/path/to/log/file"; + emit_journald = true; + emit_stdout = false; + emit_stderr = false; + level = "INFO"; + }; + }; + + menuSettings = { + server_endpoint = "/run/user//clipcat/grpc.sock"; + finder = "rofi"; + rofi = { + line_length = 100; + menu_length = 30; + menu_prompt = "Clipcat"; + extra_arguments = [ + "-mesg" + "Please select a clip" + ]; + }; + dmenu = { + line_length = 100; + menu_length = 30; + menu_prompt = "Clipcat"; + }; + }; + }; + + nmt.script = '' + assertFileExists home-files/.config/clipcat/clipcatd.toml + assertFileExists home-files/.config/clipcat/clipcatctl.toml + assertFileExists home-files/.config/clipcat/clipcat-menu.toml + + assertFileContent home-files/.config/clipcat/clipcatd.toml \ + ${./cfg/daemon.toml} + + assertFileContent home-files/.config/clipcat/clipcatctl.toml \ + ${./cfg/ctl.toml} + + assertFileContent home-files/.config/clipcat/clipcat-menu.toml \ + ${./cfg/menu.toml} + + ''; +}