Open danbst opened 5 years ago
Another example where this would be nice.
There is code in zram
module, which looks like:
systemd.services =
let
createZramInitService = dev:
nameValuePair "zram-init-${dev}" {
description = "Init swap on zram-based device ${dev}";
bindsTo = [ "dev-${dev}.swap" ];
after = [ "dev-${dev}.device" "zram-reloader.service" ];
requires = [ "dev-${dev}.device" "zram-reloader.service" ];
# ...
};
in listToAttrs ((map createZramInitService devices) ++ [(nameValuePair "zram-reloader"
{
description = "Reload zram kernel module when number of devices changes";
serviceConfig = {
# ...
};
restartTriggers = [ cfg.numDevices ];
restartIfChanged = true;
})]);
It's functional style is a bit mess. When rewriting in comprehension style, it will look like:
systemd.services = {
for dev in devices;
"zram-init-${dev}" = {
description = "Init swap on zram-based device ${dev}";
bindsTo = [ "dev-${dev}.swap" ];
after = [ "dev-${dev}.device" "zram-reloader.service" ];
requires = [ "dev-${dev}.device" "zram-reloader.service" ];
# ...
};
zram-reloader = {
description = "Reload zram kernel module when number of devices changes";
serviceConfig = {
# ...
};
restartTriggers = [ cfg.numDevices ];
restartIfChanged = true;
};
};
This comprehension solution is more natural than listToAttrs
, because doesn't require namevalue pair concept and builtins.listToAttrs
is trivially implemented with comprehension syntax:
listToAttrs = l: {
for x in l; ${x.name} = x.value;
};
cc @edolstra
@volth can you show the desired syntax, how ZRAM example would look like?
nameValuePair
predates dynamic attributes.
I marked this as stale due to inactivity. → More info
I closed this issue due to inactivity. → More info
To create a dictionary from list I do like this:
This is not nice. What about this:
and this
Essentially this will be syntax sugar for
inherit
: