From 77acbbfe51e8e3fca6566b52e173f6cccec20ac9 Mon Sep 17 00:00:00 2001 From: albert <albert@sysctl.io> Date: Sat, 18 Jan 2025 00:53:02 +0100 Subject: [PATCH] Add home-manager cleanup cronjob --- .../common/services/clean-home-manager.nix | 26 +++++++++++++++++++ home-manager/default.nix | 12 +++------ home-manager/small.nix | 14 +++------- 3 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 home-manager/common/services/clean-home-manager.nix diff --git a/home-manager/common/services/clean-home-manager.nix b/home-manager/common/services/clean-home-manager.nix new file mode 100644 index 00000000..305971c4 --- /dev/null +++ b/home-manager/common/services/clean-home-manager.nix @@ -0,0 +1,26 @@ +{ pkgs, ... }: { + systemd.user.services.cleanup-generations = { + Unit = { + Description = "Cleanup old home-manager generations"; + }; + Service = { + Type = "oneshot"; + ExecStart = "${pkgs.writeShellScript "cleanup-generations" '' + home-manager generations | tail -n +6 | awk '{print $5}' | xargs -I {} home-manager remove-generations {} + ''}"; + }; + }; + + systemd.user.timers.cleanup-generations = { + Unit = { + Description = "Timer for cleaning up old home-manager generations"; + }; + Timer = { + OnCalendar = "weekly"; + Persistent = true; + }; + Install = { + WantedBy = [ "timers.target" ]; + }; + }; +} diff --git a/home-manager/default.nix b/home-manager/default.nix index f38ff7e9..5987e019 100644 --- a/home-manager/default.nix +++ b/home-manager/default.nix @@ -3,6 +3,9 @@ # Common configs ./common/software/cli + # Services + ./common/services/clean-home-manager.nix + # User configs ./users/${username} @@ -25,15 +28,6 @@ homeDirectory = "/home/${username}"; sessionPath = [ "$HOME/.local/bin" ]; }; - - # Clean up all but the most recent 5 home-manager configs - services.cron = { - enable = true; - crontab = '' - # Cleanup all but last 5 home-manager generations weekly on Sunday at midnight - 0 0 * * 0 home-manager generations | tail -n +6 | awk '{print $5}' | xargs -I {} home-manager remove-generations {} - ''; - }; nixpkgs = { overlays = [ diff --git a/home-manager/small.nix b/home-manager/small.nix index 597dc892..501b1976 100644 --- a/home-manager/small.nix +++ b/home-manager/small.nix @@ -1,7 +1,6 @@ { lib, inputs, hostname, username, desktop, hmStateVersion, ... }: { imports = [ # Common configs - # ./common/software/cli/bash.nix ./common/software/cli/fish.nix ./common/software/cli/git.nix ./common/software/cli/nixvim/base.nix @@ -13,6 +12,9 @@ ./common/software/cli/gpg.nix ./common/software/cli/zoxide.nix + # Services + ./common/services/clean-home-manager.nix + # User configs ./users/${username} @@ -35,16 +37,6 @@ homeDirectory = "/home/${username}"; sessionPath = [ "$HOME/.local/bin" ]; }; - - # Clean up all but the most recent 2 home-manager configs - services.cron = { - enable = true; - crontab = '' - # Cleanup all but last 5 home-manager generations weekly on Sunday at midnight - 0 0 * * 0 home-manager generations | tail -n +3 | awk '{print $5}' | xargs -I {} home-manager remove-generations {} - ''; - }; - nixpkgs = { overlays = [