Skip to content

Override options of modules (documentation wanted) #16884

Closed

Description

@butterflya

I want to override config.systemd.services.hydra-init as defined in
https://github.com/NixOS/hydra/blob/master/hydra-module.nix without modifying that file directly.

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?

Activity

  1. copumpkin commented on Jul 12, 2016

    @copumpkin
    Member

    @nbp probably knows the answer to this best

  2. added
    0.kind: question
    on Aug 10, 2016
  3. nbp commented on Aug 10, 2016

    @nbp
    Member

    @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.

    {lib, ...}:
    
    { config = {
        systemd.services.hydra-init = {...}: {
          options = {
             preStart = lib.mkOption {
               apply = str: myFun str;
             };
          };
          config = {};
        };
      };
    }

    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.

  4. Myrl commented on Oct 11, 2016

    @Myrl
    Contributor

    @nbp What does the trailing config = {}; do?

  5. nbp commented on Oct 31, 2016

    @nbp
    Member

    @Myrl the trailing config was used at one point to disambiguate the sub-module interpretation. It does not seems to be necessary now.

  6. Profpatsch commented on Nov 12, 2016

    @Profpatsch
    Member

    Has been answered. If something is still unclear, feel free to reopen.

  7. ElvishJerricco commented on Nov 1, 2018

    @ElvishJerricco
    Contributor

    @volth I think that's because services.xserver is not a submodule. I think this might work?

    options.services.xserver.config = lib.mkOption {
      apply = old: "new";
    };
    
  8. nixos-discourse commented on Mar 20, 2020

    @nixos-discourse
  9. nixos-discourse commented on Mar 28, 2021

    @nixos-discourse

    This issue has been mentioned on NixOS Discourse. There might be relevant details there:

    https://discourse.nixos.org/t/override-submodule-options/12165/3

  10. deliciouslytyped commented on Apr 19, 2021

    @deliciouslytyped
    Contributor

    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:

    #! /usr/bin/env nix-shell
    #! nix-shell -i "nix-build -A etc" -p""
    let
      modules = [ ({lib, ...}: {
        options.networking.firewall.enable = lib.mkOption {  
          apply = v: builtins.trace v v; # Should trace `false`
          };
        config.networking.firewall.enable = false;
        }) ];
    in rec {
      result = import <nixpkgs/nixos/lib/eval-config.nix> { inherit modules; };
      inherit (result.config.system.build) etc;
      }
    • But I don't know why this is in options.networking whereas with nbp's example it should be config.networking?

    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):

    #! /usr/bin/env nix-shell
    #! nix-shell -i "nix-build -A etc" -p""
    let
      module = {lib, ...}: with lib // lib.types; {
        options.security.pam.services = mkOption {
          type = attrsOf (submodule ({...}: {
            options.text = mkOption {
              apply = v: builtins.replaceStrings [ "pam_unix.so" ] [ "pam_unix.so audit debug" ] v; 
              };
            }));     
          };
        };
    in rec {
      result = import <nixpkgs/nixos/lib/eval-config.nix> { modules = [ module ]; };
      etc = result.config.system.build.etc;
      }
    • Though I'm now left with some questions on how this is supposed to work... what happens if you do this multiple times? (priority system like always?)
  11. changed the title [-]Override options of modules[/-] [+]Override options of modules (documentation wanted)[/+] on Apr 19, 2021
  12. nixos-discourse commented on Sep 23, 2021

    @nixos-discourse
  13. tomprince commented on Sep 30, 2021

    @tomprince
    Contributor

    as we would need to order option definitions

    This seems to exists now as mkOrder and mkOverride.

    One downside to use mkOption { ... apply ...} is that it doesn't work if the option you want to change already has an apply.

  14. infinisil commented on Dec 9, 2021

    @infinisil
    Member

    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):

    { lib, ... }: {
      systemd.services.hydra-init.preStart = lib.mkBefore ''
        echo foo
      '';
    }
  15. added
    6.topic: module system
    on Dec 9, 2021
  16. YellowOnion commented on Mar 23, 2023

    @YellowOnion
    Contributor

    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?

  17. infinisil commented on Mar 23, 2023

    @infinisil
    Member

    I think the better solution is to move more towards attribute-based types instead of list-based ones

  18. YellowOnion commented on Mar 24, 2023

    @YellowOnion
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: question6.topic: module system

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions