Skip to content

Commit

Permalink
lib.modules.doRename: Add doc comments
Browse files Browse the repository at this point in the history
I don't think these are rendered yet, but this at least provides
the content.

Follow-up to #285612
  • Loading branch information
roberth committed Feb 5, 2024
1 parent 29c7665 commit 4833d64
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion lib/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,68 @@ let
(opt.highestPrio or defaultOverridePriority)
(f opt.value);

doRename = { from, to, visible, warn, use, withPriority ? true, condition ? true }:
/*
Return a module that help declares an option that has been renamed.
When a value is defined for the old option, it is forwarded to the `to` option.
*/
doRename = {
# List of strings representing the attribute path of the old option.
from,
# List of strings representing the attribute path of the new option.
to,
# Boolean, whether the old option is to be included in documentation.
visible,
# Whether to warn when a value is defined for the old option.
# NOTE: This requires the NixOS assertions module to be imported, so
# - this generally does not work in submodules
# - this may or may not work outside NixOS
warn,
# A function that is applied to the merged option value, to form the value
# of the old `from` option. Example: ``lib.warn "Obsolete option `${opt.old}' is used. Use `${opt.to}' instead."``
use,
# Legacy option, enabled by default: whether to preserve the priority of definitions in `old`.
withPriority ? true,
# A boolean that defines the `mkIf` condition for `to`.
# If the condition evaluates to `true`, and the `to` path points into an
# `attrsOf (submodule ...)`, then `doRename` would cause an empty module to
# be created, even if the `from` option is undefined.
# By setting this to an expression that may return `false`, you can inhibit
# this undesired behavior.
#
# Example:
#
# ```nix
# { config, lib, ... }:
# let
# inherit (lib) mkOption mkEnableOption types doRename;
# in
# {
# options = {
#
# # Old service
# services.foo.enable = mkEnableOption "foo";
#
# # New multi-instance service
# services.foos = mkOption {
# type = types.attrsOf (types.submodule …);
# };
# };
# imports = [
# (doRename {
# from = [ "services" "foo" "bar" ];
# to = [ "services" "foos" "" "bar" ];
# visible = true;
# warn = false;
# use = x: x;
# withPriority = true;
# # Only define services.foos."" if needed. (It's not just about `bar`)
# condition = config.services.foo.enable;
# })
# ];
# }
# ```
condition ? true
}:
{ config, options, ... }:
let
fromOpt = getAttrFromPath from options;
Expand Down

0 comments on commit 4833d64

Please sign in to comment.