What is the /etc/NIXOS file for?

In the Encypted Btrfs Root with Opt-in State on NixOS article, they mention mapping the /etc/nixos directory and the /etc/NIXOS file into persistent storage.

  • What is the /etc/NIXOS file? It has no content. It is some sort of marker? Does it need to be preserved or mapped?
  • I know /etc/nixos is where system configuration files are by default. However, I’ve got them stored in another directory, which works fine, and /etc/nixos remains empty. Does the directory still need to exist?

I don’t do anything with /etc/NIXOS (I use ZFS rollbacks and a persistent folder for /etc/nixos).

I’d assumed that it lets nix know that you are running in nixOS, and that it recreates it itself.

I’m fairly sure that /etc/nixos does not need to exist as long as the system can find the config on reboot (as it will regenerate the output from that). Having said that I have not tried it… :slight_smile:

1 Like

Yep, this is the correct answer. Whenever you switch NixOS configurations, it ensures you’re targeting an actual NixOS system. It doesn’t hold any data but its existence does tell nixos-rebuild whether or not the target rootfs is a NixOS system.

1 Like

That makes sense. So then it is important to persist that file. Thanks!

Is it though? I never persisted the file and nixos-rebuild worked fine.

Does NixOS recreate the file on boot? If so, persistence should not be necessary.

The file is created during activation. Note that switch-to-configuration is the program that bails if /etc/NIXOS doesn’t exist. So nixos-rebuild will bail without /etc/NIXOS because it calls switch-to-configuration, but activation does not bail without it. While activation is one of the steps in switch-to-configuration, it also occurs independently during boot, which causes the file to be created.


Awesome answer, very useful! Thanks!