[Nix-dev] NixOS modules: restrict the values of a `types.listOf x`

aszlig aszlig at redmoonstudios.org
Fri Jan 22 09:58:19 CET 2016


On Fri, Jan 22, 2016 at 01:31:37PM +0900, Eric Sagnes wrote:
> I would like to force an option of type `types.listOf packages` to
> allow only a certain list of packages. Something like `types.someOf
> packages [ pkgs.pluginA pkgs.pluginB pkgs.pluginC ]`.
>
> So that if a user set `myModule.plugins = [ pkgs.pluginA pkgs.firefox
> ]`, it gives an error like "Invalid value `pkgs.firefox` for
> `myModule.plugins. Values should be some of [ pkgs.pluginA
> pkgs.pluginB pkgs.pluginC ]";
>
> Is there a way to achieve this?

You can use something like:

  listOf (enum [ pkgs.pluginA pkgs.firefox ])

However, I'd advise against that because on comparison the list elements
are unboxed and thus can lead to an infinite recursion.

I'd suggest the following in this case:

  listOf (addCheck package (p: p.meta.isFooPlugin or false))

That way only packages with a meta.isFooPlugin attribute can be added to
that list and nothing else. And it also avoids unboxing the full
attribute set, because it checks only the meta attribute and whether it
is a derivation/store path.

a!
-- 
aszlig
Universal dilettante
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 230 bytes
Desc: Digital signature
Url : http://lists.science.uu.nl/pipermail/nix-dev/attachments/20160122/7b095f11/attachment.bin 


More information about the nix-dev mailing list