From 004753ae6b04c4b18aa07192c1106800aaacf6c3 Mon Sep 17 00:00:00 2001 From: saygo-png Date: Wed, 24 Sep 2025 22:11:51 +0200 Subject: [PATCH] home-manager: add backup overwrite option When using the backupFileExtension option, if the backup file exists, the activation process fails. This adds an option to instead overwrite the old backup instead of failing. --- modules/files.nix | 3 +++ modules/files/check-link-targets.sh | 4 +++- nix-darwin/default.nix | 1 + nixos/common.nix | 4 ++++ nixos/default.nix | 17 ++++++++++------- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/modules/files.nix b/modules/files.nix index 3c4a84b53..ba45665b9 100644 --- a/modules/files.nix +++ b/modules/files.nix @@ -149,6 +149,9 @@ in if [[ -e "$targetPath" && ! -L "$targetPath" && -n "$HOME_MANAGER_BACKUP_EXT" ]] ; then # The target exists, back it up backup="$targetPath.$HOME_MANAGER_BACKUP_EXT" + if [[ -e "$backup" && -n "$HOME_MANAGER_BACKUP_OVERWRITE" ]]; then + run rm $VERBOSE_ARG "$backup" + fi run mv $VERBOSE_ARG "$targetPath" "$backup" || errorEcho "Moving '$targetPath' failed!" fi diff --git a/modules/files/check-link-targets.sh b/modules/files/check-link-targets.sh index ae4f091ff..ceb335c65 100644 --- a/modules/files/check-link-targets.sh +++ b/modules/files/check-link-targets.sh @@ -33,8 +33,10 @@ for sourcePath in "$@" ; do elif [[ ! -L "$targetPath" && -n "$HOME_MANAGER_BACKUP_EXT" ]] ; then # Next, try to move the file to a backup location if configured and possible backup="$targetPath.$HOME_MANAGER_BACKUP_EXT" - if [[ -e "$backup" ]]; then + if [[ -e "$backup" && -z "$HOME_MANAGER_BACKUP_OVERWRITE" ]] ; then collisionErrors+=("Existing file '$backup' would be clobbered by backing up '$targetPath'") + elif [[ -e "$backup" && -n "$HOME_MANAGER_BACKUP_OVERWRITE" ]] ; then + warnEcho "Existing file '$targetPath' is in the way of '$sourcePath' and '$backup' exists. Backup will be clobbered due to HOME_MANAGER_BACKUP_OVERWRITE=1" else warnEcho "Existing file '$targetPath' is in the way of '$sourcePath', will be moved to '$backup'" fi diff --git a/nix-darwin/default.nix b/nix-darwin/default.nix index 3a9f8d977..971c2769a 100644 --- a/nix-darwin/default.nix +++ b/nix-darwin/default.nix @@ -28,6 +28,7 @@ in ${lib.optionalString ( cfg.backupFileExtension != null ) "export HOME_MANAGER_BACKUP_EXT=${lib.escapeShellArg cfg.backupFileExtension}"} + ${lib.optionalString cfg.overwriteBackup "export HOME_MANAGER_BACKUP_OVERWRITE=1"} ${lib.optionalString cfg.verbose "export VERBOSE=1"} exec ${usercfg.home.activationPackage}/activate --driver-version ${driverVersion} >&2 ''} diff --git a/nixos/common.nix b/nixos/common.nix index d557959a1..f2dec70b5 100644 --- a/nixos/common.nix +++ b/nixos/common.nix @@ -91,6 +91,10 @@ in ''; }; + overwriteBackup = mkEnableOption '' + forced overwriting of existing backup files when using `backupFileExtension` + ''; + extraSpecialArgs = mkOption { type = types.attrs; default = { }; diff --git a/nixos/default.nix b/nixos/default.nix index 793b02fa0..91378683b 100644 --- a/nixos/default.nix +++ b/nixos/default.nix @@ -7,15 +7,18 @@ }: let - + inherit (lib) mkIf; cfg = config.home-manager; - serviceEnvironment = - lib.optionalAttrs (cfg.backupFileExtension != null) { - HOME_MANAGER_BACKUP_EXT = cfg.backupFileExtension; - } - // lib.optionalAttrs cfg.verbose { VERBOSE = "1"; }; + serviceEnvironment = lib.mkMerge [ + (mkIf cfg.verbose { VERBOSE = "1"; }) + (mkIf (cfg.backupFileExtension != null) { + HOME_MANAGER_BACKUP_EXT = cfg.backupFileExtension; + }) + + (mkIf cfg.overwriteBackup { HOME_MANAGER_BACKUP_OVERWRITE = "1"; }) + ]; in { imports = [ ./common.nix ]; @@ -41,7 +44,7 @@ in ]; }; } - (lib.mkIf (cfg.users != { }) { + (mkIf (cfg.users != { }) { systemd.services = lib.mapAttrs' ( _: usercfg: let