# optionalAttrs in module -\> infinite recursion with config

**URL:** https://discourse.nixos.org/t/optionalattrs-in-module-infinite-recursion-with-config/27876
**Category:** Help
**Created:** [May 4, 2023, 5:51pm UTC](https://discourse.nixos.org/t/optionalattrs-in-module-infinite-recursion-with-config/27876 "2023-05-04T17:51:00Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![jtojnar](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jtojnar/32/70_2.png) [@jtojnar](https://discourse.nixos.org/u/jtojnar)
#### Post date: [May 4, 2023, 8:26pm UTC](https://discourse.nixos.org/t/optionalattrs-in-module-infinite-recursion-with-config/27876/5 "2023-05-04T20:26:27Z")

</div>

`optionalAttrs cond as` is defined as the following Nix expression:

> <https://github.com/NixOS/nixpkgs/blob/27ccf0e793bfec2ffd9298f53ade75849b581f29/lib/attrsets.nix#L676-L676>

As a result, the module system does not see the `attrs` when `cond` evaluates to `false`.

You got infinite recursion because in order to get the value of `config`, you would need to evaluate the `optionalAttrs` but that depends on the value of `config`. Nix is a lazy language so you might expect it to still work unless you do something like:

```nix
(optionalAttrs cfg.isLinux {
  services.use_builder.isLinux = false;
})

```

However, Nix is strict in names of attributes in attribute set, so even something like the following will trigger an infinite recursion:

```nix
let c = (if builtins.isAttrs c then { bar = 2; } else {}) // { foo = 5; }; in c.foo

```

On the other hand, `lib.mkIf` is a module system primitive and will preserve the content passed to it in the Nix value and will only resolve it in the module system evaluation:

```auto
nix-repl> :p lib.mkIf false { foo = true; }
{ _type = "if"; condition = false; content = { foo = true; }; }

nix-repl> (lib.modules.evalModules {
  modules = [
    { options.foo = lib.mkEnableOption "test"; }
    (lib.mkIf false { foo = true; })
  ];
}).config
{ foo = false; }

```

The benefit is that the module system is aware of the `content` argument of `mkIf` even when the condition is `false` but the consequence is that the options defined within need to be declared in the module system and can only have valid values set.

---

_[View the full topic](https://discourse.nixos.org/t/optionalattrs-in-module-infinite-recursion-with-config/27876)._
