Get name of attribute in attribute


As in the title; is it possible to get the name of the attribute in the attribute itself, such as { Hello = "${thisAttr}, world!"; }?

Thank you kindly for the help!

You can use lib.genAttrs:

lib.genAttrs [ "Hello" ] (name: "${name}, world!")

Yes, but each of my values are different; I’ve decided to mapAttrs over everything and make the values functions, as suggested by u/balsoft on Reddit. But I always did love genAttrs! :joy_cat:

mapAttrs requires you to pass a set, and if your values are functions, then you’ll have to have some sort of dummy value in the set. Unless you mean that your set will be something like { "foo" = (blah: ...); }? (But then they’re all different functions, why not just hardcode the values, why the dynamic key inspection?)

What’s your end goal here, if I may ask?

1 Like

Basically, I’m forgetful, and I want to use the nixosConfigurations flake attribute, but I don’t want to keep repeating the names; using a function I can just do something like hostId: { config, ... }: { ... } to set the networking.hostId value!

Maybe it’d be better to just define a shortcut option for the full name, or are you trying to define all of your host IDs in one place?

1 Like

Actually, I just realized something: I pass in the hostname with specialArgs; I don’t need anything else! :sob: Sorry; thanks for reminding me! :joy_cat: