NixOS: Gelöschte configuration.nix aus früheren Generationen retten

NixOS: Recovering a Deleted configuration.nix from Earlier Generations

In this guide, you will learn how to recover an accidentally deleted configuration.nix without having to restore a full system backup. This tutorial is intended for users with NixOS experience.

Prerequisites

  • A functional NixOS system.
  • Root privileges via the sudo command.
  • An open terminal.

Step 1: Search for Configuration Files in the Store

By searching for corresponding directories, you can look for remaining configuration files:

ls -d /nix/store/*-source/configuration.nix

If successful, the output will display a list of paths:

/nix/store/a1b2c3d4e5f6...-source/configuration.nix
/nix/store/f7g8h9i0j1k2...-source/configuration.nix

If at least one line appears, the configuration is still present on the drive.

Step 2: Check Flake Metadata (Alternative)

Alternatively, if you use flakes, you can run the metadata command to locate configuration paths:

nix flake metadata

Step 3: Copy the Configuration Back to /etc/nixos

Now, copy the discovered file back to /etc/nixos/configuration.nix.

Warnung: Achte darauf, den Zielpfad exakt anzugeben, um keine anderen Systemdateien in /etc zu überschreiben.
sudo cp /nix/store/QUELL_PFAD-source/configuration.nix /etc/nixos/configuration.nix

Replace /nix/store/QUELL_PFAD-source/configuration.nix with the path you identified earlier.

Step 4: Set Write Permissions for the Restored File

After copying, permissions must be adjusted so you can edit the configuration.

sudo chmod 644 /etc/nixos/configuration.nix

Step 5: Best Practices for the Future

For simple setups, the system.copySystemConfiguration option keeps a copy of the configuration on hand. Using Git for version control is also strongly recommended.

Verifying the Setup

Run the following command to check whether the file exists and contains text:

head -n 5 /etc/nixos/configuration.nix

Troubleshooting

ls: cannot access ‘/nix/store/*-source/configuration.nix’: No such file or directory
Cause: No matching store paths are available.
Solution: If you have a backup, restore the file from there.

See also  unRaid Alternatives: The best free alternatives

Permission denied when editing the file
Cause: Missing write permissions after copying.
Solution: Run sudo chmod 644 /etc/nixos/configuration.nix.

Rolling Back

If you want to undo the copy operation, you can delete the restored file at /etc/nixos/configuration.nix.

Conclusion

Your configuration is now restored. As a best practice, managing your configuration with Git version control is highly recommended.

Quellen: Forum: NixOS – Forum, discourse.nixos.org, de.wikipedia.org, gnulinux.ch

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top