Skip to content

Optional user defined script for error handling in case of home-manager backup file already existing #6424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Doc-Steve opened this issue Feb 9, 2025 · 2 comments · May be fixed by #7153
Assignees

Comments

@Doc-Steve
Copy link

Description

There have been several proposals for handling failures for backup of files stopping the home-manager activation, because there is a need in special cases which can not be solved with e.g. forcing overwrite for a single specified file. Unfortunately it seems that there is no consensus how to deal with that error case: setting a global force-overwrite flag was considered too risky because something important could be deleted (by intended or even unintended behaviour).

My proposal is to give that decision to a user defined optional error handling script with an option like home-manager.backupFileExistsHook. This bash script should be executed in case of "backup file already exists" and two environment variables (e.g. ORIGINALFILE, BACKUPFILE) should be set. Then the user can define any strategy wanted (from renaming the backups with dates in the filname to moving with overwriting). If the script returns no error and made space for writing a new file in the place of "ORIGINALFILE" the home-manager activation can continue. If no script is set at all, same backward compatible behaviour of immediate failure.

Copy link

stale bot commented May 10, 2025

Thank you for your contribution! I marked this issue as stale due to inactivity. Please be considerate of people watching this issue and receiving notifications before commenting 'I have this issue too'. We welcome additional information that will help resolve this issue. Please read the relevant sections below before commenting.

If you are the original author of the issue

  • If this is resolved, please consider closing it so that the maintainers know not to focus on this.
  • If this might still be an issue, but you are not interested in promoting its resolution, please consider closing it while encouraging others to take over and reopen an issue if they care enough.
  • If you know how to solve the issue, please consider submitting a Pull Request that addresses this issue.

If you are not the original author of the issue

  • If you are also experiencing this issue, please add details of your situation to help with the debugging process.
  • If you know how to solve the issue, please consider submitting a Pull Request that addresses this issue.

Memorandum on closing issues

Don't be afraid to manually close an issue, even if it holds valuable information. Closed issues stay in the system for people to search, read, cross-reference, or even reopen – nothing is lost! Closing obsolete issues is an important way to help maintainers focus their time and effort.

@stale stale bot added the status: stale label May 10, 2025
cdo256 added a commit to cdo256/home-manager that referenced this issue May 28, 2025
This adds a new backupCommand option to allow users to specify a
custom command to run on existing files during activation, as an
alternative to the existing backupFileExtension mechanism.

Adds backupCommand option to NixOS and nix-darwin modules.

Exports HOME_MANAGER_BACKUP_COMMAND environment variable when set.

Updates file activation logic to use the custom backup command if
provided, falling back to the existing backup extension logic.

Updates collision checking and user-facing instructions to
mention the new option.

This enables advanced backup workflows, such as moving files to
trash or archiving with custom tools, before managing them with
Home Manager.
cdo256 added a commit to cdo256/home-manager that referenced this issue May 28, 2025
This adds a new backupCommand option to allow users to specify a
custom command to run on existing files during activation, as an
alternative to the existing backupFileExtension mechanism.

Adds backupCommand option to NixOS and nix-darwin modules.

Exports HOME_MANAGER_BACKUP_COMMAND environment variable when set.

Updates file activation logic to use the custom backup command if
provided, falling back to the existing backup extension logic.

Updates collision checking and user-facing instructions to
mention the new option.

This enables advanced backup workflows, such as moving files to
trash or archiving with custom tools, before managing them with
Home Manager.
@cdo256 cdo256 linked a pull request May 28, 2025 that will close this issue
5 tasks
@cdo256
Copy link

cdo256 commented May 28, 2025

I have created a pull request for this. I've been using this branch for 3 weeks setting backupCommand to ${pkgs.trash-cli}/bin/trash with good success.

@stale stale bot removed the status: stale label May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants