Can I move /etc/nixos to my dotfiles and symlink it back to /etc/nixos?

I’m fairly new to NixOS, actually I just installed it yesterday (on both my desktop and laptop for extra motivation to get things working lol), and I’m trying to figure out a good way to manage the /etc/nixos/*.nix config files.

At first I thought that maybe I should just import /some-path-to-dotfiles, but then I noticed someone mentioning that they keep the whole /etc/nixos under git, which I kinda liked because I’ve been using etckeeper lately.

But taking this a step further, I’m thinking that maybe I don’t even want to manage yet another git repo with config, but rather move the NixOS config (as in the whole /etc/nixos directory) into my dotfiles, and symlink it back to /etc/nixos.

Is there any downside in doing this? I guess it would not be ideal in case of reinstalls, where a new computer would start with its own /etc/nixos, and I’d need to somehow get on my dotfiles and replace the config?

I’ve googled around a bit but haven’t found a canonical answer. Would you guys recommend doing the symlink, or rather import everything into the original config, or maybe something else?

3 Likes

This is what I do, in my dotfiles (note that I am heavily refactoring them at the moment so the documentation is slightly out of date). I have everything in a single git repository and symlink the configuration.nix file from the repo, see the How section in the README.

1 Like

There’s no need to symlink — /etc/nixos/configuration.nix is just the default location, and you can change it. When you run nixos-rebuild, it looks up the value of “nixos-config” in the NIX_PATH environment variable, so you can point that wherever you want.

Example (as root):

export NIX_PATH="nixos-config=/path/to/configuration.nix"
nixos-rebuild switch

See also the nix.nixPath configuration option.

7 Likes

Cool! Didn’t know about this, might have to take a look to make things even more automagic.

Thanks guys! I guess this brings a question though, does it matter if my user owns /etc/nixos (or whatever NIX_PATH points to)?

I assume that since I run nixos-rebuild as root (with sudo) it would be able to read the configuration in my homedir anyway, right?

But coming back to the initial thought, now as I’m writing this, it seems rather silly in terms of setting up a new computer. From what I understand, I’d do something like nixos-generate-config on a new machine, and then replace/merge with my config from dotfiles, and re-configure the rest of the system based on that, right? So from that point (and avoiding as many manual steps as needed), I wouldn’t even have my user to clone the dotfiles with at that point.

I’m maybe optimizing too much at this point, but it does seem a bit silly to first install without my config, create a user by hand, clone dotfiles, replace /etc/nixos/configuration.nix, and rebuild.

But then again it would seem even more silly to do something like have an initial configuration.nix that does exactly that, and in the end replace itself with the dotfiles, right? Am I overthinking this? I sortof like the idea of being able to spin up new machines easily (and reinstall fast), but this initial step seems rather silly heh.

1 Like

I usually do a bare minimum install with
nixos-generate-config/nixos-install, and then as soon as I reboot I’ll
rebuild with my normal configuration. The only reason for this is that
I’d rather get out of the installer as soon as possible to stop having
to worry about multiple Nix stores and stuff.

You should be able to set nixos-config for nixos-install too, though.
So you could fetch your configuration while still in the installer and
install it all at once.

I’m doing just what you are asking - symlinking my /etc/nixos to my dotfiles. I also have a small script to set up the said link and move the hardware config in case of a new install. This has, however, felt a bit fragile. Thanks @qyliss, I didn’t know you could use NIX_PATH to achieve the same effect. It seems to be a far better alternative than to symlink from /etc/nixos.

Thanks! Just one last question if I may. How do you actually do your initial configuration? Do you just get network up & setup the initial user, and then move onto using dotfiles? I guess I may have overreacted to doing the initial install by hand, since it’s just a matter of uncommenting a few lines hehe :slight_smile:

Do you just get network up & setup the initial user, and then move
onto using dotfiles?

That’s exactly what I do. It could probably be better, but it’s not
like I have to do it very often.

Thanks guys! I guess this brings a question though, does it matter if my user owns /etc/nixos (or whatever NIX_PATH points to)?

Yeah your user can own /etc/nixos. Rather than symlink or setup the NIX_PATH, I just keep /etc/nixos directly as a git repo owned by my user.

When you boot up on the very first install, you can install git with nix-env, clone the repo as root, build the OS, and then at a later point chown /etc/nixos to yourself once your user exists.

1 Like

In your normal usage, where are you setting the NIX_PATH environment variable? It needs to be root’s environment variable, no?

Kinda? I do something effectively the same: use a store path built from source symlinked to /etc and NIX_PATH:

I do this because I’m lazy and it solves weirdness with desktop env libs (qt) behaving weirdly with env programs. Lemme know if this is what you’re l looking for.

I ended up using the NIX_PATH variable for configuring my machines and moved my dotfiles to /etc/nixos and chowned it to my user, works flawlessly and I really like it (here).

1 Like