Infinite recursion of config: how to modify swapDevices without changing `hardware-configuration.nix`?

Other than:

is there another / better(?) way for me to modify an attribute of a list of submodules defined in another file?

In this case, I’m specifically looking at adding e.g. discardPolicy = "both"; to config.swapDevices.*, but I’m currently doing my first NixOS + ZFS on mirrored root setup, so I’m also looking at adding "nofail" to my mirrored EFI filesystems and hoping to find a general approach.

Initially, I was really hoping that I had missed a language feature somewhere and could just literally put swapDevices.*.discardPolicy = "both"; :laughing: that obviously didn’t work.

Next, I was really thinking that I could use the let cfg = config... trick to work around infinite recursion issues (as is common to see in modules):

swapDevices = let devs = config.swapDevices; in
    builtins.map (dev: dev // {discardPolicy = "both";}) devs;

(where config.swapDevices is defined in hardware-configuration.nix, which I thought I’d try not to modify for once.)

This seems to work in the repl:

nix-repl> builtins.map (dev: dev // {discardPolicy = "both";}) nixosConfigurations.nas.config.swapDevices
[ { ... } { ... } ]

But when I try I get infinite recursion:

# nixos-rebuild switch --flake .#nas
building the system configuration...
error: infinite recursion encountered

       at /nix/store/15q3niqkdvkyf7mvbyh1r8szp4xwk9nz-source/lib/modules.nix:728:9:

          727|     in warnDeprecation opt //
          728|       { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
             |         ^
          729|         inherit (res.defsFinal') highestPrio;
(use '--show-trace' to show detailed location information)

Is duplicating everything my only / best option if I’m trying not to change hardware-configuration.nix?

TIA!

swapDevices = [ things you want to *add* ];

You can not “change” or “remove” entries that are added via another module.


Though to be honest: Yes there is this comment at the top, though do you really intend regenerating that file often? In my experience most users just tweak it to their needs by hand (or remove it completely) and also do some manual sync with what gets printed by nixos-generate-config --show-hardware-config on an irregular basis.

Thanks for your input. This is probably my 5th NixOS install, and I wanted to try a little harder to do things the “right” way, since this machine will hopefully be longer lived than the others. It seems like I should probably just continue to stray from that path by modifying hardware-configuration.nix and ignoring the warning.