Refefing attrs property with dot in rec { ... }

Hello,

In normal situaltion I could do:

  sysctl = rec {
    abc = 1;
    def = abc;
  };

But is it possible to do that when I need quote the property names?

  sysctl = rec {
    "net.ipv6.conf.all.forwarding" = 1;
    "net.ipv6.conf.default.forwarding" = "net.ipv6.conf.all.forwarding"; # will not work
  };

Thank you.

I do not see how to make it work with rec, but a let ... in will work.
For example:

let 
  sysctl = {
    "net.ipv6.conf.all.forwarding" = 1;
    "net.ipv6.conf.default.forwarding" = sysctl."net.ipv6.conf.all.forwarding"; 
   };
in sysctl
4 Likes

If this is a NixOS module, you can even do:

{ config, ... }: {
  boot.kernel.sysctl = {
    "net.ipv6.conf.all.forwarding" = 1;
    "net.ipv6.conf.default.forwarding" = config.boot.kernel.sysctl."net.ipv6.conf.all.forwarding";
  };
}

rec is generally considered an antipattern, and I’m personally not fond of excessive use of let bindings either since they make things hard to override in downstream modules (and kinda awkward to read).

5 Likes

Thank you @markuskowa and @TLATER , I was thinking that there is some less known syntax for that.

I use it as often as I can since it makes the code shorter :wink: . Thank you.