NixOS rebuild fails on backup up config file by home-manager

Hi!

I’m using NixOS (24.05, unstable) with Home Manager to configure my Firefox. It often happened that nixos-rebuild switch failed because Home Manager wanted to override an existing config file (/home/rob/.mozilla/firefox/rob/search.json.mozlz4). So I activated the Home Manager option home-manager.backupFileExtension = "backup", which would backup the file with an extension instead of just overwriting it. But now the NixOS rebuild fails again, but with the backed up file:

Existing file '/home/rob/.mozilla/firefox/rob/search.json.mozlz4.backup' would be clobbered by backing up '/home/rob/.mozilla/firefox/rob/search.json.mozlz4'

What am I missing? Is there an option to tell Home Manager to just overwrite the backed up file?

Thanks for your help!

Rob

1 Like

Hi,

I also have some home activation issues while I’m using backFileExtension, there is an issue open.

I recently tried to use a timestamp in backupFileExtension as a workaround, but the timestamp is not refreshed on home activation (got the timestamp trick here, but I failed to use builtins.currentTime):

# one try
home-manager.backupFileExtension = "backup-" + pkgs.lib.readFile "${pkgs.runCommand "timestamp" {} "echo -n `date '+%Y%m%d%H%M%S'` > $out"}";

# another one
home-manager.backupFileExtension = "backup-" + pkgs.lib.readFile "${pkgs.runCommand "timestamp" { env.when = self.sourceInfo.lastModified; } "echo -n `date '+%Y%m%d%H%M%S'` > $out"}";

Maybe someone has an idea on how to get a new timestamp on every activation, it should work then :slight_smile:

3 Likes

I use a hacky workaround for this with a custom script to build nixos which renames the {filename}.backup file to {filename}.bak (with home-manager.backupFileExtension = "backup".

export FIREFOX_CFG = "${HOME}/.mozilla/firefox/default";
ls $FIREFOX_CFG | rg "\.backup$" | sed 's/.backup//g' | xargs -I "{}" mv $FIREFOX_CFG/{}.backup $FIREFOX_CFG/{}.bak && sudo nixos-rebuild switch

I actually use this as a bash shell alias with FIREFOX_CFG set as a session variable.

nix-make = "ls \$FIREFOX_CFG | rg \"\\.backup\$\" | sed 's/.backup//g' | xargs -I \"{}\" mv \$FIREFOX_CFG/{}.backup \$FIREFOX_CFG/{}.bak && sudo nixos-rebuild switch";

This snippet uses rg (ripgrep) (personal preference), but can be used with grep as well.

Can also be used as a system.userActivationScripts.

Also there is another way to just overwrite the existing backup file referenced here: Add option to replace existing files · Issue #4199 · nix-community/home-manager · GitHub