Skip to content

Commit 35e0890

Browse files
committed
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.
1 parent 6238bbc commit 35e0890

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

modules/files.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ in
149149
if [[ -e "$targetPath" && ! -L "$targetPath" && -n "$HOME_MANAGER_BACKUP_EXT" ]] ; then
150150
# The target exists, back it up
151151
backup="$targetPath.$HOME_MANAGER_BACKUP_EXT"
152+
if [[ -e "$backup" && -n "$HOME_MANAGER_BACKUP_OVERWRITE" ]]; then
153+
run rm $VERBOSE_ARG "$backup"
154+
fi
152155
run mv $VERBOSE_ARG "$targetPath" "$backup" || errorEcho "Moving '$targetPath' failed!"
153156
fi
154157

modules/files/check-link-targets.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ for sourcePath in "$@" ; do
3333
elif [[ ! -L "$targetPath" && -n "$HOME_MANAGER_BACKUP_EXT" ]] ; then
3434
# Next, try to move the file to a backup location if configured and possible
3535
backup="$targetPath.$HOME_MANAGER_BACKUP_EXT"
36-
if [[ -e "$backup" ]]; then
36+
if [[ -e "$backup" && -z "$HOME_MANAGER_BACKUP_OVERWRITE" ]] ; then
3737
collisionErrors+=("Existing file '$backup' would be clobbered by backing up '$targetPath'")
38+
elif [[ -e "$backup" && -n "$HOME_MANAGER_BACKUP_OVERWRITE" ]] ; then
39+
warnEcho "Existing file '$targetPath' is in the way of '$sourcePath' and '$backup' exists. Backup will be clobbered due to HOME_MANAGER_BACKUP_OVERWRITE=1"
3840
else
3941
warnEcho "Existing file '$targetPath' is in the way of '$sourcePath', will be moved to '$backup'"
4042
fi

nix-darwin/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ in
2828
${lib.optionalString (
2929
cfg.backupFileExtension != null
3030
) "export HOME_MANAGER_BACKUP_EXT=${lib.escapeShellArg cfg.backupFileExtension}"}
31+
${lib.optionalString cfg.overwriteBackup "export HOME_MANAGER_BACKUP_OVERWRITE=1"}
3132
${lib.optionalString cfg.verbose "export VERBOSE=1"}
3233
exec ${usercfg.home.activationPackage}/activate --driver-version ${driverVersion} >&2
3334
''}

nixos/common.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ in
9191
'';
9292
};
9393

94+
overwriteBackup = mkEnableOption ''
95+
forced overwriting of existing backup files when using `backupFileExtension`'';
96+
9497
extraSpecialArgs = mkOption {
9598
type = types.attrs;
9699
default = { };

nixos/default.nix

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
}:
88

99
let
10-
10+
inherit (lib) mkIf;
1111
cfg = config.home-manager;
1212

13-
serviceEnvironment =
14-
lib.optionalAttrs (cfg.backupFileExtension != null) {
13+
serviceEnvironment = lib.mkMerge [
14+
(mkIf cfg.verbose { VERBOSE = "1"; })
15+
16+
(mkIf (cfg.backupFileExtension != null) {
1517
HOME_MANAGER_BACKUP_EXT = cfg.backupFileExtension;
16-
}
17-
// lib.optionalAttrs cfg.verbose { VERBOSE = "1"; };
18+
})
1819

20+
(mkIf cfg.overwriteBackup { HOME_MANAGER_BACKUP_OVERWRITE = "1"; })
21+
];
1922
in
2023
{
2124
imports = [ ./common.nix ];
@@ -41,7 +44,7 @@ in
4144
];
4245
};
4346
}
44-
(lib.mkIf (cfg.users != { }) {
47+
(mkIf (cfg.users != { }) {
4548
systemd.services = lib.mapAttrs' (
4649
_: usercfg:
4750
let

0 commit comments

Comments
 (0)