Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/modules: Add function to create option alias that respects priority #53397

Merged
merged 4 commits into from Jan 14, 2019

Conversation

cdepillabout
Copy link
Member

@cdepillabout cdepillabout commented Jan 4, 2019

Motivation for this change

This commit adds a function mkAliasOptionModuleWithPriority. This function will make an alias to an existing option and copy over the priority. (This is in contrast to mkAliasOptionModule which does not copy over the priority.)

This functionality is needed for PRs like #53041 (specifically #53041 (comment)).

Here's the use-case:

There exists an option powerManagement.cpuFreqGovernor. On some systems, this is set in hardware-configuration.nix by nixos-generate-config:

{
  powerManagement.cpuFreqGovernor = mkDefault "powersave";
}

Note how it gets a low priority. The end-user (or other modules) should be able to easily override it.

In #53041, I wanted to add options to set the CPU max and min frequency:

{
  powerManagement.cpufreq.max = 2200000;
  powerManagement.cpufreq.min = 1500000;
}

I thought it was silly to have the options called both powerManagement.cpuFreqGovernor and powerManagement.cpufreq.max, so I renamed powerManagement.cpuFreqGovernor to powerManagement.cpufreq.governor. This is fine, but it will break for people that are using hardware-configuration.nix generated with an older version of nixos-generate-config (since in their config it is called powerManagement.cpuFreqGovernor).

In order to accommodate those people, I added the alias powerManagement.cpuFreqGovernor to powerManagement.cpufreq.governor:

mkAliasOptionModule [ "powerManagement" "cpuFreqGovernor" ] [ "powerManagement" "cpufreq" "governor" ]

This should let people use their older hardware-configuration.nix with the powerManagement.cpuFreqGovernor option.

However, there is one edge case this doesn't work for.

Imagine that the user has powerManagement.cpuFreqGovernor set in hardware-configuration.nix:

{
  powerManagement.cpuFreqGovernor = mkDefault "powersave";
}

The user then tries to set powerManagement.cpufreq.governor to something else in configuration.nix:

{
  powerManagement.cpufreq.governor = "ondemand";
}

This actually will fail. The user will get an error that powerManagement.cpufreq.governor is defined two places (which is true, but the definition in hardware-configuration.nix should have low priority).

What's going on here? Well, the mkAliasOptionModule function doesn't actually copy over the priority of the alias option to the normal option. So to the nix module system, it really does look like powerManagement.cpufreq.governor is being set two different places.

This PR adds a function mkAliasOptionModuleWithPriority that does correctly(?) copy over the priority from the alias option to the normal option.

Tests have also been added to make sure this is working correctly.

Pinging @infinisil since he helped a lot with #53041. Also pinging @edolstra @nbp since they seem to be code-owners of the stuff in lib/.

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Assured whether relevant documentation is up to date
  • Fits CONTRIBUTING.md.

…he priority

This commit adds a function `mkAliasOptionModuleWithPriority`.  This
function will make an alias to an existing option and copy over the
priority.

This functionality is needed for PRs like NixOS#53041.  In that case
`nixos-generate-config` added an option to `hardware-configuration.nix`
with `mkDefault`.  That option was then changed and an alias created for
the old name.

The end user should be able to set the non-alias option in their
`configuration.nix` and have everything work correctly.  Without this
function, the priority for the option won't be copied over correctly
and the end-user will get a message saying they have the same option
set to two different values.
Copy link
Member

@nbp nbp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! For some reasons I thought this was the case but this might be a really long time ago...
A minor change and making the test cases more robust and this should be good for landing.

lib/modules.nix Outdated Show resolved Hide resolved
withPriority = true;
};

doRename = { from, to, visible, warn, use, withPriority ? false }:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once this issue is fixed. Open a follow-up issue / PR to evaluate the impact of switching this flag to true and replacing mkAliasAndWrapDefinitions with the one you are adding.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

lib/tests/modules.sh Show resolved Hide resolved
@cdepillabout
Copy link
Member Author

@nbp Thanks for the review and the good suggestions. I have fixed the things you've pointed out.

This PR should be ready for a final review and hopefully merging.

@cdepillabout
Copy link
Member Author

Hey @nbp, any chance getting this merged?

Copy link
Member

@nbp nbp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay and thanks for the patches. These are great changes!

@nbp nbp merged commit a3beabf into NixOS:master Jan 14, 2019
@cdepillabout cdepillabout deleted the aliasoptionmodule-set-priority branch January 15, 2019 01:44
infinisil pushed a commit that referenced this pull request Feb 23, 2019
…lias.

This commit changes the `mkAliasOptionModule` function to make sure that
the priority for the aliased option is propagated to the non-aliased
option.

This also affects the `mkRenamedOptionModule` function in a similar
fashion.

This also removes the `mkAliasOptionModuleWithPriority` function, since
its functionality is now subsumed by `mkAliasOptionModule`.

This change was recommended by @nbp:
#53397 (comment)
@infinisil infinisil added the 6.topic: module system About NixOS module system internals label Mar 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants