From c8678105c621bbd6f24eab98380c5593bd294249 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Mon, 12 Aug 2024 17:36:15 +0200 Subject: [PATCH] home/neovim: add half-finished swapfile remover --- home/home.nix | 2 +- home/programs/neovim/auto-clean-swapfiles.nix | 56 +++++++++++++++++++ .../{neovim.nix => neovim/default.nix} | 4 ++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 home/programs/neovim/auto-clean-swapfiles.nix rename home/programs/{neovim.nix => neovim/default.nix} (98%) diff --git a/home/home.nix b/home/home.nix index bc68c18..eb70b3c 100644 --- a/home/home.nix +++ b/home/home.nix @@ -17,7 +17,7 @@ in { ./programs/git ./programs/gpg ./programs/less.nix - ./programs/neovim.nix + ./programs/neovim ./programs/nix-index ./programs/tealdeer ./programs/tmux.nix diff --git a/home/programs/neovim/auto-clean-swapfiles.nix b/home/programs/neovim/auto-clean-swapfiles.nix new file mode 100644 index 0000000..57ca005 --- /dev/null +++ b/home/programs/neovim/auto-clean-swapfiles.nix @@ -0,0 +1,56 @@ +{ config, pkgs, lib, ... }: +let + daysBeforeDeletion = 2; +in +{ + config = { + systemd.user.services.clean-neovim-swap-files = { + Unit = { + Description = "Clean old swap files for neovim"; + }; + + Service = { + Type = "oneshot"; + CPUSchedulingPolicy = "idle"; + IOSchedulingClass = "idle"; + ExecStart = lib.getExe (pkgs.writeShellApplication { + name = "clean-neovim-swap-files"; + runtimeInputs = with pkgs; [ findutils ]; + text = '' + echo "Cleaning old swap files for neovim" + + OLD_SWAPFILES=$(find "${config.xdg.stateHome}/nvim/swap" -type f -name '*.swp' -mtime +${toString daysBeforeDeletion}) + + if [ -z "$OLD_SWAPFILES" ]; then + echo "No old swap files found" + exit 0 + fi + + for swapfile in $OLD_SWAPFILES; do + echo "Removing $swapfile" + rm -- "$swapfile" + done + + echo "Done" + ''; + }); + }; + }; + + systemd.user.timers.clean-neovim-swap-files = { + Unit = { + Description = "Clean old swap files for neovim"; + }; + + Timer = { + Unit = "clean-neovim-swap-files.service"; + OnCalendar = "daily"; + Persistent = true; + }; + + Install = { + WantedBy = [ "timers.target" ]; + }; + }; + }; +} diff --git a/home/programs/neovim.nix b/home/programs/neovim/default.nix similarity index 98% rename from home/programs/neovim.nix rename to home/programs/neovim/default.nix index 2f20a39..a923d83 100644 --- a/home/programs/neovim.nix +++ b/home/programs/neovim/default.nix @@ -1,5 +1,9 @@ { pkgs, home, ... }: { + imports = [ + ./auto-clean-swapfiles.nix + ]; + programs.neovim = { enable = true;