nix-community / impermanence

Modules to help you handle persistent state on systems with ephemeral root storage [maintainer=@talyz]
MIT License
1.01k stars 76 forks source link

`nixos-rebuild` fails due to the symlink, Impermanence created #170

Open coritanic opened 4 months ago

coritanic commented 4 months ago

When a persisted file doesn't exist in the persistent directory, Impermanence creates a symlink to that nonexistent file. Example:

/home/user/.bash_history -> /nix/persist/home/user/.bash_history

However, during nixos-rebuild test/switch, persist-files fails due to that symlink, so nixos-rebuild fails:

# nixos-rebuild --flake /etc/nixos#vbox --target-host vbox test
building the system configuration...
copying 2 paths...
copying path '/nix/store/wwda11fzqzwchkbmfynhy3vhzg6al9hg-users-groups.json' to 'ssh://vbox'...
copying path '/nix/store/qwbmvab6zhs9ir3zqhb8b7f2ghijng38-nixos-system-nixos-23.11.20240220.526d051' to 'ssh://vbox'...
activating the configuration...
setting up /etc...
ln: failed to create symbolic link '/home/user/.bash_history': File exists
Error when executing ln -s "$targetFile" "$mountPoint" at line 42!
Activation script snippet 'persist-files' failed (1)
reloading user units for root...
setting up tmpfiles
warning: the following units failed: persist-\x27-nix-persist-home-user-.bash_history\x27.service

× persist-\x27-nix-persist-home-user-.bash_history\x27.service - Bind mount or link '/nix/persist/home/user/.bash_history' to '/home/user/.bash_history'
     Loaded: loaded (/etc/systemd/system/persist-\x27-nix-persist-home-user-.bash_history\x27.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Thu 2024-02-22 11:31:24 +03; 175ms ago
    Process: 1466 ExecStart=/nix/store/viy5r3a8slihv80lknjkxnhiiwa9rn2j-impermanence-mount-file /home/user/.bash_history /nix/persist/home/user/.bash_history  (code=exited, status=1/FAILURE)
   Main PID: 1466 (code=exited, status=1/FAILURE)
         IP: 0B in, 0B out
        CPU: 10ms

Feb 22 11:31:24 nixos systemd[1]: Starting Bind mount or link '/nix/persist/home/user/.bash_history' to '/home/user/.bash_history'...
Feb 22 11:31:24 nixos viy5r3a8slihv80lknjkxnhiiwa9rn2j-impermanence-mount-file[1473]: ln: failed to create symbolic link '/home/user/.bash_history': File exists
Feb 22 11:31:24 nixos viy5r3a8slihv80lknjkxnhiiwa9rn2j-impermanence-mount-file[1466]: Error when executing ln -s "$targetFile" "$mountPoint" at line 42!
Feb 22 11:31:24 nixos systemd[1]: persist-\x27-nix-persist-home-user-.bash_history\x27.service: Main process exited, code=exited, status=1/FAILURE
Feb 22 11:31:24 nixos systemd[1]: persist-\x27-nix-persist-home-user-.bash_history\x27.service: Failed with result 'exit-code'.
Feb 22 11:31:24 nixos systemd[1]: Failed to start Bind mount or link '/nix/persist/home/user/.bash_history' to '/home/user/.bash_history'.
warning: error(s) occurred while switching to the new configuration

After creating the file:

# ssh vbox touch /nix/persist/home/user/.bash_history
# nixos-rebuild --flake /etc/nixos#vbox --target-host vbox test
building the system configuration...
copying 0 paths...
activating the configuration...
setting up /etc...
A file already exists at /home/user/.bash_history!
Activation script snippet 'persist-files' failed (1)
reloading user units for root...
setting up tmpfiles
warning: the following units failed: persist-\x27-nix-persist-home-user-.bash_history\x27.service

× persist-\x27-nix-persist-home-user-.bash_history\x27.service - Bind mount or link '/nix/persist/home/user/.bash_history' to '/home/user/.bash_history'
     Loaded: loaded (/etc/systemd/system/persist-\x27-nix-persist-home-user-.bash_history\x27.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Thu 2024-02-22 11:51:27 +03; 150ms ago
    Process: 2349 ExecStart=/nix/store/viy5r3a8slihv80lknjkxnhiiwa9rn2j-impermanence-mount-file /home/user/.bash_history /nix/persist/home/user/.bash_history  (code=exited, status=1/FAILURE)
   Main PID: 2349 (code=exited, status=1/FAILURE)
         IP: 0B in, 0B out
        CPU: 10ms

Feb 22 11:51:27 nixos systemd[1]: Starting Bind mount or link '/nix/persist/home/user/.bash_history' to '/home/user/.bash_history'...
Feb 22 11:51:27 nixos viy5r3a8slihv80lknjkxnhiiwa9rn2j-impermanence-mount-file[2349]: A file already exists at /home/user/.bash_history!
Feb 22 11:51:27 nixos systemd[1]: persist-\x27-nix-persist-home-user-.bash_history\x27.service: Main process exited, code=exited, status=1/FAILURE
Feb 22 11:51:27 nixos systemd[1]: persist-\x27-nix-persist-home-user-.bash_history\x27.service: Failed with result 'exit-code'.
Feb 22 11:51:27 nixos systemd[1]: Failed to start Bind mount or link '/nix/persist/home/user/.bash_history' to '/home/user/.bash_history'.
warning: error(s) occurred while switching to the new configuration

Only after creating the file and rebooting the machine, the problem goes:

# nixos-rebuild --flake /etc/nixos#vbox --target-host vbox test
building the system configuration...
copying 0 paths...
activating the configuration...
setting up /etc...
reloading user units for root...
setting up tmpfiles

After removing the file from the persistent directory and rebooting, the problem comes again.

88 adds a force option for replacing files. This problem should be handled without any additional option.