You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Override options of modules (documentation wanted) #16884
In particular, I would like to just add some string in front of the current value.
In an imperative language this would be:
read config.systemd.services.hydra-init.preStart into variable tmp
write config.systemd.services.hydra-init.preStart with value "MY CHANGES" + ${tmp}
In Nix, there is e.g. mkForce, but it doesn't allow to use the old value.
Next to merely copying and pasting, I would like to run tools like 'sed' on the value of config.systemd.services.hydra-init.preStart in order to do more complicated overrides.
Is this currently impossible to do?
Reacted by Bjørn Forsman, Kamil Chmielewski, HolomorphicHound, Ian Macalinao, AleXoundOS, SeerLite, Yuu Yin, Felix Schröter, contactornyc, Daniel Langbein and 2 more
1/ Using an option definition to rewrite other option definitions would add a lot of complexity to the evaluation process, as we would need to order option definitions, to let some code provided in an option definition pre-process the options.
Apart from the technical aspect, for simplicity reasons, I am against having function in option definitions. The main reason being that functions are harder to understand (even if developers are used to this notion), harder to document, and harder to reason about.
2/ The module system allow one to extend option declarations, and option declarations already have a system for post-processing all option definitions.
This apply function is used for post-processing the result of the merge of all valid option definitions. The result of the apply function is what is made visible by config.systemd.services.hydra-init.preStart.
Thus, by extending the option definition systemd.services.hydra-init with a module, you can locally (only hydra-init) extend the preStart option with an apply post-processing function.
Yeah, apply is no good and I think it should be deprecated at some point because as @tomprince says it's not composable.
But yeah, mkBefore can be used to order definitions, so to answer the original question: This is how to prepend a string to a string option (notably types.lines which is the type of preStart, types.str doesn't work because it doesn't allow merging of definitions):
There's still the odd time we might want to remove items from a list or string, and I don't see another way other than apply to do that, can we make apply more composable, perhaps with a priority system using a list of functions?
in nixpkgs currently that isn't the case, where we need to use apply to work around the lack of perfectly atomic options, we already have mkAfter / mkBefore to work around the limits of strings, especially places like extraConfig that seems like a very common pattern.
For example, tho only tangentially related qemu-vm.nix, has a bunch of hard coded values, for example it's impossible to omit the memory parameter, and it's also limited to positive integers, meaning you cannot make it dynamic at runtime or provide the secondary parameters, slots and maxmem, luckily qemu treats the last argument as priority, so we can work around it, but not every app and every possible matrix of configuration settings will happily accept this kind of "append only" work around.
Also, it would be nice if qemu-vm.nix didn't use a list for virtualisation.sharedDirectories, and hard code two unneeded directories but that's currently the case and from what I gather the only way to remove these directories is to use apply or fork nixpkgs.
Activity
copumpkin commentedon Jul 12, 2016
@nbp probably knows the answer to this best
nbp commentedon Aug 10, 2016
@butterflya sorry for the late reply.
I will reply with 2 answers:
1/ Using an option definition to rewrite other option definitions would add a lot of complexity to the evaluation process, as we would need to order option definitions, to let some code provided in an option definition pre-process the options.
Apart from the technical aspect, for simplicity reasons, I am against having function in option definitions. The main reason being that functions are harder to understand (even if developers are used to this notion), harder to document, and harder to reason about.
2/ The module system allow one to extend option declarations, and option declarations already have a system for post-processing all option definitions.
This apply function is used for post-processing the result of the merge of all valid option definitions. The result of the apply function is what is made visible by
config.systemd.services.hydra-init.preStart.Thus, by extending the option definition
systemd.services.hydra-initwith a module, you can locally (onlyhydra-init) extend thepreStartoption with an apply post-processing function.Myrl commentedon Oct 11, 2016
@nbp What does the trailing
config = {};do?nbp commentedon Oct 31, 2016
@Myrl the trailing
configwas used at one point to disambiguate the sub-module interpretation. It does not seems to be necessary now.Profpatsch commentedon Nov 12, 2016
Has been answered. If something is still unclear, feel free to reopen.
ElvishJerricco commentedon Nov 1, 2018
@volth I think that's because
services.xserveris not a submodule. I think this might work?nixos-discourse commentedon Mar 20, 2020
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/vboxmanage-executable-file-not-found-in-path/6277/7
nixos-discourse commentedon Mar 28, 2021
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/override-submodule-options/12165/3
deliciouslytyped commentedon Apr 19, 2021
This solves a longstanding question I've had. It would be good to document this.
(Generally good to be thorough in that one documents creation, destruction, mutation..wait wasn't there an acronym for that? https://en.wikipedia.org/wiki/Create,_read,_update,_and_delete I guess - and do this for each orthogonal component.)
Here is an almost-minimal expression demonstrating this functionality:
Combining this with a submodule (thanks @infinisil for the submodule example the other day), here is a demonstration adding some flags to https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/security/pam.nix#L389 (observe the content of
/etc/pam.d/su):[-]Override options of modules[/-][+]Override options of modules (documentation wanted)[/+]nixos-discourse commentedon Sep 23, 2021
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/how-to-extend-nixos-options-defaults/15174/1
tomprince commentedon Sep 30, 2021
This seems to exists now as
mkOrderandmkOverride.One downside to use
mkOption { ... apply ...}is that it doesn't work if the option you want to change already has anapply.infinisil commentedon Dec 9, 2021
Yeah,
applyis no good and I think it should be deprecated at some point because as @tomprince says it's not composable.But yeah,
mkBeforecan be used to order definitions, so to answer the original question: This is how to prepend a string to a string option (notablytypes.lineswhich is the type ofpreStart,types.strdoesn't work because it doesn't allow merging of definitions):YellowOnion commentedon Mar 23, 2023
There's still the odd time we might want to remove items from a list or string, and I don't see another way other than apply to do that, can we make apply more composable, perhaps with a priority system using a list of functions?
infinisil commentedon Mar 23, 2023
I think the better solution is to move more towards attribute-based types instead of list-based ones
YellowOnion commentedon Mar 24, 2023
in nixpkgs currently that isn't the case, where we need to use
applyto work around the lack of perfectly atomic options, we already have mkAfter / mkBefore to work around the limits of strings, especially places likeextraConfigthat seems like a very common pattern.For example, tho only tangentially related
qemu-vm.nix, has a bunch of hard coded values, for example it's impossible to omit the memory parameter, and it's also limited to positive integers, meaning you cannot make it dynamic at runtime or provide the secondary parameters,slotsandmaxmem, luckily qemu treats the last argument as priority, so we can work around it, but not every app and every possible matrix of configuration settings will happily accept this kind of "append only" work around.Also, it would be nice if
qemu-vm.nixdidn't use a list forvirtualisation.sharedDirectories, and hard code two unneeded directories but that's currently the case and from what I gather the only way to remove these directories is to use apply or fork nixpkgs.