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

#1

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.

1 Like

#2

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.

2 Likes

#3

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.

0 Likes

#4

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…

2 Likes

#5

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

#6

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.


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

#7

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

#8

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!

0 Likes

#9

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.

0 Likes