summaryrefslogtreecommitdiffstats
path: root/lib/config.nix
blob: c0708aeeca59595bbd1ad3b1d08756fdc1edce3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
{lib}: let
  inherit (lib.options) mkOption;
  inherit (lib.types) bool;
  inherit (lib.modules) mkRenamedOptionModule;
  inherit (lib.attrsets) mapAttrsToList;
  inherit (lib.lists) flatten;
in {
  /**
  Build a boolean NixOS option with the given default value and description.

  # Type

  ```
  mkBool :: Bool -> String -> Option
  ```

  # Arguments

  - `value`: Default boolean value for the option.
  - `description`: Documentation string for the option.

  # Example

  ```nix
  mkBool true "Enable feature X"
  => mkOption { type = bool; default = true; description = "Enable feature X"; }
  ```
  */
  mkBool = value: description:
    mkOption {
      type = bool;
      default = value;
      inherit description;
    };

  /*
  Generates a list of mkRenamedOptionModule, from a mapping of the old name to
  the new name. Nested options can optionally supply a "_name" to indicate its
  new name.

  # Example

  ```nix
  batchRenameOptions ["nvimTree"] ["nvimTree" "setupOpts"] {
      disableNetrw = "disable_netrw";
      nestedOption = {
        _name = "nested_option";
        somethingElse = "something_else";
      };
  }
  ```

  The above code is equivalent to this:

  ```nix
  [
    (
      mkRenamedOptionModule
        ["nvimTree" "disableNetrw"]
        ["nvimTree" "setupOpts" "disable_netrw"]
    )
    (
      mkRenamedOptionModule
        ["nvimTree" "nestedOption" "somethingElse"]
        ["nvimTree" "setupOpts" "nested_option" "something_else"]
    )
  ]
  ```
  */
  batchRenameOptions = oldBasePath: newBasePath: mappings: let
    genSetupOptRenames = oldSubpath: newSubpath: table:
      mapAttrsToList (
        oldName: newNameOrNestedOpts:
          if builtins.isAttrs newNameOrNestedOpts
          then
            genSetupOptRenames (oldSubpath ++ [oldName]) (newSubpath ++ [newNameOrNestedOpts._name or oldName])
            (builtins.removeAttrs newNameOrNestedOpts ["_name"])
          else
            mkRenamedOptionModule
            (oldBasePath ++ oldSubpath ++ [oldName])
            (newBasePath ++ newSubpath ++ [newNameOrNestedOpts])
      )
      table;
  in
    flatten (genSetupOptRenames [] [] mappings);
}