How strict are you in not editing `hardware-configuration.nix`?

At the top of my hardware-configuration.nix there is a nice warning not to edit the file as it may be overwritten:

# Do not modify this file!  It was generated by ‘nixos-generate-config’
# and may be overwritten by future invocations.  Please make changes
# to /etc/nixos/configuration.nix instead.

I admit that I’ve edited it a bunch, as to me it makes sense to put things there like filesystem mount options, swap file options, auto-unlock settings for luks volumes, kernel boot parameters. In my mind these are mostly specific to the machine and make sense to go there. If I was moving to a new machine I would mostly copy configuration.nix, not hardware-configuration.nix.

I know I could heed the warning and put all my changes into configuration.nix instead, but I’m wondering in practice whether that’s worthwhile.

How strictly do you heed this warning, and avoid editing this file?

You are free to create any structure you want. If you have multiple machines, you would very quickly start modularizing the setup anyway and come up with an entirely new structure.

I make changes to it when I need. One example in which you can’t really on nixos-generate-config is it doesn’t work well with btrfs, it doesn’t include all mounts options.

From the config directory:

$ find modules -type f | wc -l
315

Includes custom modules as well as configuration.

$ find modules -type f -name hardware-configuration.nix | wc -l
0

Not a single file named hardware-configuration.nix :wink:

Heed the warning or don’t - it doesn’t matter.

After installation I maintain that file completely by hand.

The parts that change the most are usually related to how partitions are mounted. And I hate how nixos-generate-config always creates mount points for ephemerally mounted drives. So I had to remove them manually anyway.

And change all the /dev/disk/by-uuid/* to /dev/disk/by-label/*

4 Likes

One thing I didn’t see mentioned so far is that one benefit of not editing hardware-configuration.nix is that you can in theory re-run nixos-generate-config and get the same output (or at least something relatively similar).

This can be helpful in the following situations:

  1. You’ve made some changes to hardware-configuration.nix but they are causing problems, so you want to go back to the “default”. (Although in this situation it might be better to just version control your NixOS configuration.)

  2. You’ve upgraded to a later version of NixOS, and you want to see if there have been any changes to nixos-generate-config that give you a configuration more appropriate for your hardware (or a recent kernel). If you’ve edited hardware-configuration.nix by hand, it can be a little difficult to make out what changes are necessary and need to be copied over, and which things are just strict improvements by people in how nixos-generate-config works.

3 Likes

Personally, I’m quite strict about it, for the reasons @cdepillabout mentions.

It’s no real additional effort to override the configuration in configuration.nix if need be, and I think that’s preferable - the config needs to be somewhere after all, no harm in having the autogenerated file around as well, even if I override its settings a bit.

1 Like

To be honest, as network and DHCP config has moved from generated configuration.nix to hardware-configuration.nix somewhen between 19.09 and 22.05, there has to happen careful manual join anyway, whether you edited the hardware configuration or not.

Who knows whether or not the network was the first or the last or neither.

I’m considering creating a hardware-configuration-overrides.nix file and including it to specify the overrides in a separate place, just in case.

Using NixOS on approximately 80 hosts - not a single hardware-configuration.nix in sight. What exactly is the use case here?

I realize that my earlier mail came across little “short”.

There is nothing magical about the filenames generated other than configuration.nix (outside flakes). You could easily store the hardware configuration in a file named after your favourite character in “Veep” or “Arrested Development” (both fantastic shows btw, but I digress).

Yes, nixos-generate-config will write out hardware-configuration.nix but the generated configuration is really just meant as inspiration for you to get started and you will inevitably end up with “hardware configuration” spread out over multiple files/modules as you start accomodating new hardware.

1 Like

I’d be interested to know how you integrate new machines in your setup. Whether you install a new host manually (so that there is an initial hardware configuration that you then add to your - supposedly version-controlled - Nix configuration) or whether your process is entirely automated (apart from attaching the machine to the network with a network cable, maybe).

For newcomers, specifically the ones that are not into tinkering with and configuring hardware, the name of the file hardware-configuration.nix indeed suggests that this is “something magic” you want to keep and better not touch. I’ve seen tutorials for NixOS that exclude hardware configuration from the version-controlled Nix configuration. Also see the Reddit Flakes solution to hardware-configuration.nix to understand how people consider hardware-configuration.nix magic that needs separate treatment.

You’re certainly right that, technically speaking, you don’t need a file named hardware-configuration.nix. But unless you’re the only one managing your setup and you don’t rely on external input (tips & tricks) it’s probably a good convention to find the stuff related to hardware. Some people move the hardware configuration to a directory in a folder structure representing the host names. So, yes, it seems like the warning at the top of the file shouldn’t be taken dead-seriously.

Those tutorials are simply wrong. It’s “just” a module, as Peter said, you can have the contents of the modules spread out over any number of files in any structure, whatever works best for you really.

But it better be under version control or you end up in a nightmare when your config gets wiped and nixos-generate-config doesn’t get you exactly back where you hoped… and if you use flakes then you are creating an inherently broken config.

There’s no universal convention; talk to your team and make one.
I personally use something like a hosts/ directory which contains each of the host-specific entry points - that does not inherently mean that all the hardware-specific stuff lives there, since some hardware can be the same on multiple systems (e.g. 10 hosts with the same GPU model).