How deep `mkIf`` should be added?

Hi Folks,

I understand that config = mkIf x {...} is a problem and I might end up in an infinite recursion.

Also my understanding is that deeper I add mkIf, the better.

I have a few examples, could you please tell me which one is good and which one is just unnecessary?

I have some custom options in one file:

{
  options =
    {
      my_custom_foo = mkOption { type = types.bool; };
      my_custom_bar = mkOption { type = types.bool; };
    };
}

Now I want to use them in different files:

1. example

  {
    config =
      {
        # This looks strange at first sight but basically
        #     I'm disabling the service only when custom option
        #     is enabled and I'm not touching the service setting
        #     if custom option is disabled.
        systemd.services.NetworkManager-wait-online.enable =
          mkIf
            my_custom_foo
            false;

        # this:
        #     systemd.services.NetworkManager-wait-online.enable = ! my_custom_foo
        # would have a different meening since I'd always set the value

        # I could also do
        #     systemd.services.NetworkManager-wait-online = mkIf my_custom_foo { enable = false; };
        # but deeper is better?
      };
  }

2. example

  {
    config =
      {
        services.nfs.server =
          mkIf
            my_custom_foo
            {
              enable = true;
              createMountPoints = my_custom_bar;
              exports =
                ''
                  /data *(rw,no_root_squash,no_subtree_check,fsid=0)
                '';
            };
      };
  }

3. example (modified 2. example)

  {
    config =
      {
        services.nfs.server = {
          enable =
            mkIf
              my_custom_foo
              true;

          createMountPoints =
            mkIf
              my_custom_foo
              my_custom_bar;

          exports =
            mkIf
              my_custom_foo
              ''
                /data *(rw,no_root_squash,no_subtree_check,fsid=0)
              '';
        };
      };
  }
1 Like