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
sudocommand. - 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.nixIf successful, the output will display a list of paths:
/nix/store/a1b2c3d4e5f6...-source/configuration.nix
/nix/store/f7g8h9i0j1k2...-source/configuration.nixIf 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 metadataStep 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.nixReplace /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.nixStep 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.nixTroubleshooting
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.
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

