Skip to content

home-manager: add support for custom backup command (#6424) #7153

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
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cdo256
Copy link

@cdo256 cdo256 commented May 28, 2025

Description

This PR adds support for a custom backup command in Home Manager.
A new backupCommand option is introduced, allowing users to specify a shell command to run on existing files that would otherwise block activation (e.g., moving files to trash, archiving, or custom backup logic). If set, this command is used in preference to the existing backupFileExtension mechanism.

  • Adds backupCommand option to NixOS and nix-darwin modules.
  • Exports HOME_MANAGER_BACKUP_COMMAND environment variable when set.
  • Updates activation logic to invoke the custom backup command if provided.
  • Updates collision checking and user-facing instructions to document the new option.

This enables more flexible and user-defined backup workflows during Home Manager activations.

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


Checklist

  • Change is backwards compatible.

    • Behaviour is only changed if the new option backupCommand, or the new environment variable HOME_MANAGER_BACKUP_COMMAND is used.
  • Code formatted.

  • Code tested.

  • Test cases updated/added. See example.

    • Requires additional integration test for backing up files generally.
  • Commit messages formatted.

  • If this PR adds a new module...
    . N/A

Maintainer CC

N/A

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.
@aguirre-matteo
Copy link
Contributor

That looks very good! I think this should have been done a long time ago.

run mv $VERBOSE_ARG "$targetPath" "$backup" || errorEcho "Moving '$targetPath' failed!"
if [[ -e "$targetPath" && ! -L "$targetPath" ]] ; then
if [[ -n "$HOME_MANAGER_BACKUP_COMMAND" ]] ; then
# TODO: Bail out early?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious what you mean by bail out early? We should continue on to the link creation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about the case in which the backup command failed, for whatever reason, but still removed the original file. However, upon second reading, it appears that's not necessary. Suppose the file is deleted without being backed up. In that case, it is lost anyway, and I suppose it is the user's responsibility to ensure that they select a reliable backup command that will fail gracefully. So I think this comment can be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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