This by itself passes nix flake check, but when I try to add another module that actually adds a swap device…
swapDevices = [ { device = "/dev/whatever"; } ];
I get this error:
The option `swapDevices."[definition 1-entry 1]".options' in `/nix/store/...-source/nixos/modules/config/swap.nix' is already declared in `/nix/store/.../swap-defaults.nix'.
Honestly, I think I’m going to patch nixpkgs after all, unless/until someone else jumps in and gives a better answer.
I’d also like to set random-encryption-related swap options in this way, each in a different module, so hopefully it won’t be too hard to make sure that your PR also works for options that are declared in nested submodules like that.
It’s a patch to the merge logic of the attrsOf (and in a real version probably lazyAttrsOf as well) type. Currently that merge function collects all definitions, zips over their attributes, and recursively merges the list of values at each attribute. In my patch, the merge function collects all non-mkForAllAttrs definitions, zips over their attributes, appends the values at each attribute with the results of applying any mkForAllAttrs functions to that attribute name, and recursively merges that combined list.
If this is redundant with the option merging approach it may not be worth it to pursue, but it’s a little easier to use perhaps?
My PR got through a light round of feedback, but seems likely to languish indefinitely if there isn’t more interest in it. I’m not going to push on it because this isn’t a feature I need. If you’d want to use it, show your support.
I get around this issue by defining a new option and constructing the final configuration from that. E.g.
{ config, lib, ... }:
{
options.custom.swapDevices = lib.mkOption {
type = lib.types.listOf lib.types.attrs; # Probably want to use a more complete type...
};
config.swapDevices = map (attrs: attrs // {
options = (attrs.options or []) ++ [ "defaults" "nofail" "discard=pages" ];
}) config.custom.swapDevices;
}
This doesn’t work, though, when you do not control every module that adds entries to swapDevices (and not to custom.swapDevices). In that case the PR by @rhendric could solve it.