home-manager: add generation expiration command

This adds a new command to the home-manager shell script that allows
generations to be removed that are older than an given absolute or
relative date.

This allows users to manage the expiration of their home-manager
generations separately from their system or user profiles via
nix-collect-garbage. It is most important if the user desires to have
a convenient way to have different expiration times for Home Manager
generations than other system or user profiles.
This commit is contained in:
Alex Brandt 2018-10-01 21:42:46 -07:00 committed by Robert Helgesson
parent 0cfd21cc15
commit 3b9b897af3
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
2 changed files with 29 additions and 4 deletions

View file

@ -21,6 +21,8 @@ pkgs.stdenv.mkDerivation {
substituteInPlace $out/bin/home-manager \ substituteInPlace $out/bin/home-manager \
--subst-var-by bash "${pkgs.bash}" \ --subst-var-by bash "${pkgs.bash}" \
--subst-var-by coreutils "${pkgs.coreutils}" \ --subst-var-by coreutils "${pkgs.coreutils}" \
--subst-var-by findutils "${pkgs.findutils}" \
--subst-var-by gnused "${pkgs.gnused}" \
--subst-var-by less "${pkgs.less}" \ --subst-var-by less "${pkgs.less}" \
--subst-var-by HOME_MANAGER_PATH '${pathStr}' --subst-var-by HOME_MANAGER_PATH '${pathStr}'
''; '';

View file

@ -1,9 +1,7 @@
#!@bash@/bin/bash #!@bash@/bin/bash
# This code explicitly requires GNU Core Utilities and we therefore # Prepare to use tools from Nixpkgs.
# need to ensure they are prioritized over any other similarly named PATH=@coreutils@/bin:@findutils@/bin:@gnused@/bin:@less@/bin${PATH:+:}$PATH
# tools on the system.
PATH=@coreutils@/bin:@less@/bin${PATH:+:}$PATH
set -euo pipefail set -euo pipefail
@ -237,6 +235,23 @@ function doRmGenerations() {
popd > /dev/null popd > /dev/null
} }
function doExpireGenerations() {
local profileDir="/nix/var/nix/profiles/per-user/$USER"
local generations
generations="$( \
find "$profileDir" -name 'home-manager-*-link' -not -newermt "$1" \
| sed 's/^.*-\([0-9]*\)-link$/\1/' \
)"
if [[ -n $generations ]]; then
# shellcheck disable=2086
doRmGenerations $generations
elif [[ -v VERBOSE ]]; then
echo "No generations to expire"
fi
}
function doListPackages() { function doListPackages() {
local outPath local outPath
outPath="$(nix-env -q --out-path | grep -o '/.*home-manager-path$')" outPath="$(nix-env -q --out-path | grep -o '/.*home-manager-path$')"
@ -349,6 +364,11 @@ function doHelp() {
echo " Remove indicated generations. Use 'generations' command to" echo " Remove indicated generations. Use 'generations' command to"
echo " find suitable generation numbers." echo " find suitable generation numbers."
echo echo
echo " expire-generations TIMESTAMP"
echo " Remove generations older than TIMESTAMP where TIMESTAMP is"
echo " interpreted as in the -d argument of the date tool. For"
echo " example \"-30 days\" or \"2018-01-01\"."
echo
echo " packages List all packages installed in home-manager-path" echo " packages List all packages installed in home-manager-path"
echo echo
echo " news Show news entries in a pager" echo " news Show news entries in a pager"
@ -422,6 +442,9 @@ case "$cmd" in
remove-generations) remove-generations)
doRmGenerations "$@" doRmGenerations "$@"
;; ;;
expire-generations)
doExpireGenerations "$@"
;;
packages) packages)
doListPackages doListPackages
;; ;;