Is it possible to recover configuration.nix from an older generation?

Is there any way to print out what my configuration.nix was on a previous generation of the system? I just realized I deleted something I want to get back. I have a full system backup but I can’t browse the backup, only restore it, and I’d rather not do that. I’m guessing the configuration.nix itself isn’t actually saved anywhere though.

4 Likes

No, not in general, I’m afraid. The best practice seems to version-control your configuration.nix (and other custom files you include from it). EDIT: there’s a system.copySystemConfiguration option for the simple cases.

4 Likes

Thanks. Wish I knew about that config option before. I do have secondary files but they’re less important. Might be nice to have a second option to specify additional paths though.

Thanks. Wish I knew about that config option before. I do have secondary files but they’re less important. Might be nice to have a second option to specify additional paths though.

For full generality you could add to your systemPackages a runCommand package that copies whatever you care about to “$out/share/system-nix-configuration” or whatever you prefer…

3 Likes

It’s also not a post-hoc solution, but in addition to version-controlling my system config with git, my system runs on zfs and I have znapzend making periodic snapshots. That’s also saved my skin a couple of times when I’ve not been diligent enough about actually committing changes…

On a different-but-related topic, I’ve been meaning for a long time to wrap my nixos-rebuild so that it automatically makes a commit labelled with the system generation on a separate branch of my system config repo whenever updating the system profile. But we all know how those “one-day” projects are…

1 Like

I have something like that now it wasnt that big of a deal. so my /etc/nixos is part of my git dotfiles project and to run a nixos-rebuild I use this script.
https://github.com/mogorman/dotfiles/blob/a490976e51a0f9afbe4df52754d7ef3d4116566d/nixos/rebuild_and_switch.sh
and in my configuration.nix there is this

environment.etc."nixos/active".text = config.system.nixos.label;

that way i always know the git release of nixos as well as its config git version.

2 Likes

I like to copy my Nix expressions tree into the store, and then use that for my NIX_PATH. It lets me hack on my tree without breaking Nixpkgs for the rest of the system, and it means that I can always find the entire tree for a given system if I need it.

3 Likes

That’s pretty cool.

Because I rather use options/modules to toggle such things I’ve adapted it into a simple module.

Thanks a lot!

Personally I’d just keep my configuration Nix files in a git repo and put this somewhere in the config:

{
  environment.etc."dotfiles-rev".text =
    (builtins.fetchGit { url = ./.; ref = "HEAD"; }).rev;
}

builtins.fetchGit returns a set with a rev field, so you can use that to log the configuration’s revision in a file. It’s not perfect. I use ref = "HEAD" so that if I have uncommitted changes, the rev is the most recent revision instead of “000000…”, so a dirtywork tree always makes an inaccurate file since the rev doesn’t include uncommitted changes. Anyway you can do one further by actually putting the whole repo somewhere.

{
  environment.etc."dotfiles-src".source = builtins.fetchGit ./.;
}

Not specifying ref = "HEAD" makes it use a dirty worktree instead of the most recent commit, so you’ll get the exact source of the configuration this way.