Combine nixos-rebuild build + nixos-enter to test changes

Hello!

I have become a bit more familiar with Nix; the syntax is no longer alien to me and I have even looked into simple tools to just generate configs in other “languages” from Nix expressions and utilizing the module system for a nice, layered structure. So, editing a configuration.nix and even the company’s own nixpkgs-overlay that a former eployee produced has become relatively easy and understandable now!

So, it is time to be really destructive … kind of. Right now, I have been testing changes of new configurations by doing this basically over and over - out of habbit.

  1. Edit the config
  2. nixos-rebuild switch --verbose
  3. Observe the changes (i.e. journalctl -u telegraf -f)
  4. goto 1

That is very impractical, so I started reading the various documentation. Honestly, the way most of them are written (i.e. the man pages) assume that you know what you are doing since quite a while and tracing certain information was not easy (I had like a two-figure list of Discourse tabs, at some point)…

I know that I can roll back a bad switch with nixos-rebuild --rollback - however, I would like to avoid switching to a bad config in the first place.

In the future, I need to test SNMP configurations for special devices where the vendors even offer their own MIBs and I need to effectively run telegraf --test $config. Said configuration, however, is generated with (...).formats.toml.generate "config.toml" within Telegraf’s service module, and not exposed as a property from thereon as such that I could re-use it in a script by grabbing it through nix-expr

I would like to reach a point where I would do the following:

  1. nixos-rebuild build -A(dditional things to merge into configure.nix?)=value
  2. nixos-enter ...
  3. Grab the generated configuration path (i.e. by using systemctl cat telegraf within the new, unactivated build)
  4. telegraf -config $path --test

This way, the actually running system is not interrupted at all, as Telegraf’s test mode can epically fail and never write anything to the configured outputs - so it does virtually nothing, except tell me where i’ve done a mistake.

How can I achieve that, exactly? In most cases, I just need to change one or two values; i.e. the commit hash of a package provided in our own overlay, or temporarily enable an SNMP input by setting a single attribute to true and alike.

I am pretty sure nixos-enter is sort of what I want; but I haven’t figured out just how to get there. And the fact that the config.toml is being generated and not exposed as a property also bothers me somewhat, because I have no idea how to get to it without reading the generated SystemD unit…

Kind regards and have a nice day!

What about NixOS:nixos-rebuild build-vm - NixOS Wiki or nixos-rebuild test?