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

**URL:** https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659
**Category:** Help
**Created:** [April 12, 2019, 7:08am UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659 "2019-04-12T07:08:17Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![lilyball](https://discourse.nixos.org/user_avatar/discourse.nixos.org/lilyball/32/939_2.png) [@lilyball](https://discourse.nixos.org/u/lilyball)
#### Post date: [April 12, 2019, 7:08am UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/1 "2019-04-12T07:08:17Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![vcunat](https://discourse.nixos.org/user_avatar/discourse.nixos.org/vcunat/32/11_2.png) [@vcunat](https://discourse.nixos.org/u/vcunat)
#### Post date: [April 12, 2019, 7:30am UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/2 "2019-04-12T07:30:14Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![lilyball](https://discourse.nixos.org/user_avatar/discourse.nixos.org/lilyball/32/939_2.png) [@lilyball](https://discourse.nixos.org/u/lilyball)
#### Post date: [April 12, 2019, 6:04pm UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/3 "2019-04-12T18:04:02Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![7c6f434c](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/7/8e7dd6/32.png) [@7c6f434c](https://discourse.nixos.org/u/7c6f434c)
#### Post date: [April 12, 2019, 6:22pm UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/4 "2019-04-12T18:22:25Z")

</div>

> 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…

---

<div class="post-metadata">

### Author: ![lheckemann](https://discourse.nixos.org/user_avatar/discourse.nixos.org/lheckemann/32/415_2.png) [@lheckemann](https://discourse.nixos.org/u/lheckemann)
#### Post date: [April 12, 2019, 6:42pm UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/5 "2019-04-12T18:42:13Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![mogorman](https://discourse.nixos.org/user_avatar/discourse.nixos.org/mogorman/32/114_2.png) [@mogorman](https://discourse.nixos.org/u/mogorman)
#### Post date: [April 12, 2019, 10:41pm UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/6 "2019-04-12T22:41:33Z")

</div>

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](https://github.com/mogorman/dotfiles/blob/a490976e51a0f9afbe4df52754d7ef3d4116566d/nixos/rebuild_and_switch.sh)  
and in my configuration.nix there is this

```nix
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.

---

<div class="post-metadata">

### Author: ![qyliss](https://discourse.nixos.org/user_avatar/discourse.nixos.org/qyliss/32/34285_2.png) [@qyliss](https://discourse.nixos.org/u/qyliss)
#### Post date: [April 17, 2019, 10:56am UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/7 "2019-04-17T10:56:04Z")

</div>

I like to [copy](https://edef.eu/~qyliss/nixlib/file/modules/nix/default.nix.html) 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.

---

<div class="post-metadata">

### Author: ![derped](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@derped](https://discourse.nixos.org/u/derped)
#### Post date: [April 18, 2019, 5:54am UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/8 "2019-04-18T05:54:11Z")

</div>

That’s pretty cool.

Because I rather use options/modules to toggle such things I’ve adapted it into a simple [module](https://git.ophanim.de/derped/nixos/src/branch/master/options/copySysConf.nix).

Thanks a lot!

---

<div class="post-metadata">

### Author: ![ElvishJerricco](https://discourse.nixos.org/user_avatar/discourse.nixos.org/elvishjerricco/32/984_2.png) [@ElvishJerricco](https://discourse.nixos.org/u/ElvishJerricco)
#### Post date: [April 18, 2019, 5:45pm UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/9 "2019-04-18T17:45:46Z")

</div>

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

```nix
{
  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.

```nix
{
  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.

---

<div class="post-metadata">

### Author: ![Raviexthegod](https://discourse.nixos.org/user_avatar/discourse.nixos.org/raviexthegod/32/32981_2.png) [@Raviexthegod](https://discourse.nixos.org/u/Raviexthegod)
#### Post date: [June 23, 2025, 10:12pm UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/10 "2025-06-23T22:12:16Z")

</div>

Technically? No. Though there is a sort of hack-job way I managed to do it, which should work as long as you have access to load the previous configuration, for obvious reasons, and have the current configuration in the same folder it would be in before. The basic way to do it is to do the command “nix flake metadata” in the folder where the folder, which should give you the store path. Then if you travel there, you can use the cp command, as sudo with the -r flag so it copies the whole directory, to a separate directory. Do note, it still retains the read-only permissions, meaning you would then need to do a chown and chmod to get the files to the correct permissions. All-In-All, however, I find it much easier to just version control from the get-go, simply because it’s much easier than copying from the store and dealing with permissions.

---

<div class="post-metadata">

### Author: ![Seven0492](https://discourse.nixos.org/user_avatar/discourse.nixos.org/seven0492/32/36535_2.png) [@Seven0492](https://discourse.nixos.org/u/Seven0492)
#### Post date: [March 9, 2026, 2:20am UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/11 "2026-03-09T02:20:39Z")

</div>

For post-mortem reference and late-night debuggers, here’s how to find old `/etc/nixos` files for every _flake_ generation not deleted by `nix-collect-garbage`:

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

or if you only want the directories then

`ls /nix/store/*-source/configuration.nix | awk -F ‘/’ ‘{print “/” $2 “/” $3 “/” $4}’`

For unknowable reasons, post-build profiles in `/nix/var/nix/profiles` do not keep track of their configuration source. At best, you can use `nix profile diff-closures --profile /nix/var/nix/profiles/system` to examine app changes and diff configurations like so `diff /…-source/configuration.nix /etc/nixos/configuration.nix`.

I highly recommend using git for version control or adding comments to configuration.nix for each rebuild like `# Configured ACME for Nginx SSL certificates` to distinguish it.

—

How I used this new-found knowledge : `nixos-rebuild switch -–flake /…-source` with each configuration until my local NextCloud works, figure out the breaking change and revert it in `/etc/nixos/configuration.nix`. Now, I use git and don’t worry about it 🙂

---

<div class="post-metadata">

### Author: ![waffle8946](https://discourse.nixos.org/user_avatar/discourse.nixos.org/waffle8946/32/27221_2.png) [@waffle8946](https://discourse.nixos.org/u/waffle8946)
#### Post date: [March 9, 2026, 2:51am UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/12 "2026-03-09T02:51:42Z")

</div>

That only works for flakes, since flakes are copied to the store before eval.

---

<div class="post-metadata">

### Author: ![Seven0492](https://discourse.nixos.org/user_avatar/discourse.nixos.org/seven0492/32/36535_2.png) [@Seven0492](https://discourse.nixos.org/u/Seven0492)
#### Post date: [March 9, 2026, 2:59am UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/13 "2026-03-09T02:59:05Z")

</div>

So, it’s impossible for non-configured-to-be-copied, non-flake and non-version-controlled setups. Otherwise, options are available.  
Edit : Thanks for the precision waffle8946

---

<div class="post-metadata">

### Author: ![existensil](https://discourse.nixos.org/user_avatar/discourse.nixos.org/existensil/32/28130_2.png) [@existensil](https://discourse.nixos.org/u/existensil)
#### Post date: [March 14, 2026, 2:44am UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/14 "2026-03-14T02:44:41Z")

</div>

I’m using flakes and this saved by butt after rsyncing my configs to `/etc/nixos` with a `–delete` flag present, nuking `hardware-configuration.nix`, which can also be recovered this way.

Thank you!

---

<div class="post-metadata">

### Author: ![TLATER](https://discourse.nixos.org/user_avatar/discourse.nixos.org/tlater/32/4257_2.png) [@TLATER](https://discourse.nixos.org/u/TLATER)
#### Post date: [March 14, 2026, 2:48am UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/15 "2026-03-14T02:48:50Z")

</div>

For the sake of anyone running into these kinds of issues: Use git.

There is no reason not to use version control for text files. That way you avoid the need for things like `rsync` as well, and get _much_ more granular, usable config syncing.

Using NixOS without git kind of defies the purpose of putting your system configuration in a centralized set of text files.

---

<div class="post-metadata">

### Author: ![Seven0492](https://discourse.nixos.org/user_avatar/discourse.nixos.org/seven0492/32/36535_2.png) [@Seven0492](https://discourse.nixos.org/u/Seven0492)
#### Post date: [March 22, 2026, 2:16am UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/16 "2026-03-22T02:16:59Z")

</div>

Update here! I dug a little deeper into `man 1 nix-store-query`:

_–requisites / -R_

```auto
      Prints out the set of requisites ⟨../../glossary.md#gloss-requisite⟩ (better known as the closure ⟨../../glossary.md#gloss-closure⟩) of the
      store path paths.

      This query has one option:

      • --include-outputs Also include the existing output paths of store derivation ⟨../../glossary.md#gloss-store-derivation⟩s, and their clo‐
         sures.

      This query can be used to implement various kinds of deployment. A source deployment is obtained by distributing the closure of a store de‐
      rivation. A binary deployment is obtained by distributing the closure of an output path. A cache deployment (combined source/binary deploy‐
      ment, including binaries of build-time-only dependencies) is obtained by distributing the closure of a store derivation and specifying the
      option --include-outputs.

```

I highly recommend looking up and understanding Nix closures for our purpose.

Regardless, this command finds _flake_ system sources per-profile:

`nix-store --query --requisites $(realpath /nix/var/nix/profiles/system-${NIXOS_PROFILE_ID}-link) | grep -- '-source$'`

INFO: usually nixpkgs is listed alongside the `/etc/nixos` copy.

---

<div class="post-metadata">

### Author: ![waffle8946](https://discourse.nixos.org/user_avatar/discourse.nixos.org/waffle8946/32/27221_2.png) [@waffle8946](https://discourse.nixos.org/u/waffle8946)
#### Post date: [March 22, 2026, 2:55am UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/17 "2026-03-22T02:55:40Z")

</div>

> [@Seven0492](#):
>
> nix-store --query --requisites $(realpath /nix/var/nix/profiles/system-${NIXOS\_PROFILE\_ID}-link) | grep – ‘-source$’

That `/nix/var/nix/...` would be best replaced by `/run/current-system`. Still, that query will list all paths with the name `source`, i.e. a lot of irrelevant paths.

---

<div class="post-metadata">

### Author: ![Seven0492](https://discourse.nixos.org/user_avatar/discourse.nixos.org/seven0492/32/36535_2.png) [@Seven0492](https://discourse.nixos.org/u/Seven0492)
#### Post date: [March 22, 2026, 2:58pm UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/18 "2026-03-22T14:58:00Z")

</div>

Well, grep here returns paths finishing with ‘`-source`’, so I only get two source derivations: nixpkgs and the `/etc/nixos` flake copy. ~~I have no idea how to limit this further~~ , so any input is appreciated 🙂

You’re right @waffle8946 that `/run/current-system` is simpler. However, the title of this thread is basically ‘how to recover configuration.nix from an older generation’. Thus, my reasoning.

EDIT:

I took a crack at better filtering.

Here’s my result : `ls $(nix-store --query --requisites $(realpath /nix/var/nix/profiles/system-250-link) | grep -- '-source$') | awk 'BEGIN{ RS="" ; FS="\n\n" ; ORS="\n\n" } ; /configuration.nix/ { print }'`

Basically, it lists the directory contents of the previous sub-command, separates the fields by an empty line and returns the field containing `configuration.nix` if it exists. Problem solved !

---

<div class="post-metadata">

### Author: ![n0099](https://discourse.nixos.org/user_avatar/discourse.nixos.org/n0099/32/35787_2.png) [@n0099](https://discourse.nixos.org/u/n0099)
#### Post date: [September 13, 2026, 7:17pm UTC](https://discourse.nixos.org/t/is-it-possible-to-recover-configuration-nix-from-an-older-generation/2659/19 "2026-09-13T19:17:17Z")

</div>

Related: [See diff between generation configurations. Or see previous generation's configuration.nix](https://discourse.nixos.org/t/see-diff-between-generation-configurations-or-see-previous-generations-configuration-nix/5493)
