1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 19:46:05 +01:00
home-manager/nix-darwin/default.nix
Christina O'Donnell ce76393bb7
home-manager: add support for custom backup command (#6424) (#7153)
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.
2025-10-30 12:26:13 -05:00

43 lines
1.4 KiB
Nix

{
config,
lib,
pkgs,
...
}:
let
cfg = config.home-manager;
in
{
imports = [ ../nixos/common.nix ];
config = lib.mkMerge [
{ home-manager.extraSpecialArgs.darwinConfig = config; }
(lib.mkIf (cfg.users != { }) {
system.activationScripts.postActivation.text = lib.concatStringsSep "\n" (
lib.mapAttrsToList (
username: usercfg:
let
driverVersion = if cfg.enableLegacyProfileManagement then "0" else "1";
in
''
echo Activating home-manager configuration for ${usercfg.home.username} >&2
launchctl asuser "$(id -u ${usercfg.home.username})" sudo -u ${usercfg.home.username} --set-home ${pkgs.writeShellScript "activation-${usercfg.home.username}" ''
${lib.optionalString (
cfg.backupFileExtension != null
) "export HOME_MANAGER_BACKUP_EXT=${lib.escapeShellArg cfg.backupFileExtension}"}
${lib.optionalString (
cfg.backupCommand != null
) "export HOME_MANAGER_BACKUP_COMMAND=${lib.escapeShellArg cfg.backupCommand}"}
${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
''}
''
) cfg.users
);
})
];
}