···3636 inherit (lib.types)
3737 mkOptionType
3838 ;
3939+ inherit (lib.lists)
4040+ last
4141+ ;
3942 prioritySuggestion = ''
4043 Use `lib.mkForce value` or `lib.mkDefault value` to change the priority on any of these definitions.
4144 '';
···107110 /* Creates an Option attribute set for an option that specifies the
108111 package a module should use for some purpose.
109112110110- The package is specified as a list of strings representing its attribute path in nixpkgs.
113113+ Type: mkPackageOption :: pkgs -> (string|[string]) ->
114114+ { default? :: [string], example? :: null|string|[string], extraDescription? :: string } ->
115115+ option
111116112112- Because of this, you need to pass nixpkgs itself as the first argument.
117117+ The package is specified in the third argument under `default` as a list of strings
118118+ representing its attribute path in nixpkgs (or another package set).
119119+ Because of this, you need to pass nixpkgs itself (or a subset) as the first argument.
113120114114- The second argument is the name of the option, used in the description "The <name> package to use.".
121121+ The second argument may be either a string or a list of strings.
122122+ It provides the display name of the package in the description of the generated option
123123+ (using only the last element if the passed value is a list)
124124+ and serves as the fallback value for the `default` argument.
115125116116- You can also pass an example value, either a literal string or a package's attribute path.
126126+ To include extra information in the description, pass `extraDescription` to
127127+ append arbitrary text to the generated description.
128128+ You can also pass an `example` value, either a literal string or an attribute path.
117129118118- You can omit the default path if the name of the option is also attribute path in nixpkgs.
130130+ The default argument can be omitted if the provided name is
131131+ an attribute of pkgs (if name is a string) or a
132132+ valid attribute path in pkgs (if name is a list).
119133120120- Type: mkPackageOption :: pkgs -> string -> { default :: [string]; example :: null | string | [string]; } -> option
134134+ If you wish to explicitly provide no default, pass `null` as `default`.
121135122136 Example:
123137 mkPackageOption pkgs "hello" { }
···129143 example = "pkgs.haskell.packages.ghc92.ghc.withPackages (hkgs: [ hkgs.primes ])";
130144 }
131145 => { _type = "option"; default = «derivation /nix/store/jxx55cxsjrf8kyh3fp2ya17q99w7541r-ghc-8.10.7.drv»; defaultText = { ... }; description = "The GHC package to use."; example = { ... }; type = { ... }; }
146146+147147+ Example:
148148+ mkPackageOption pkgs [ "python39Packages" "pytorch" ] {
149149+ extraDescription = "This is an example and doesn't actually do anything.";
150150+ }
151151+ => { _type = "option"; default = «derivation /nix/store/gvqgsnc4fif9whvwd9ppa568yxbkmvk8-python3.9-pytorch-1.10.2.drv»; defaultText = { ... }; description = "The pytorch package to use. This is an example and doesn't actually do anything."; type = { ... }; }
152152+132153 */
133154 mkPackageOption =
134134- # Package set (a specific version of nixpkgs)
155155+ # Package set (a specific version of nixpkgs or a subset)
135156 pkgs:
136157 # Name for the package, shown in option description
137158 name:
138138- { default ? [ name ], example ? null }:
139139- let default' = if !isList default then [ default ] else default;
159159+ {
160160+ # The attribute path where the default package is located
161161+ default ? name,
162162+ # A string or an attribute path to use as an example
163163+ example ? null,
164164+ # Additional text to include in the option description
165165+ extraDescription ? "",
166166+ }:
167167+ let
168168+ name' = if isList name then last name else name;
169169+ default' = if isList default then default else [ default ];
170170+ defaultPath = concatStringsSep "." default';
171171+ defaultValue = attrByPath default'
172172+ (throw "${defaultPath} cannot be found in pkgs") pkgs;
140173 in mkOption {
174174+ defaultText = literalExpression ("pkgs." + defaultPath);
141175 type = lib.types.package;
142142- description = "The ${name} package to use.";
143143- default = attrByPath default'
144144- (throw "${concatStringsSep "." default'} cannot be found in pkgs") pkgs;
145145- defaultText = literalExpression ("pkgs." + concatStringsSep "." default');
176176+ description = "The ${name'} package to use."
177177+ + (if extraDescription == "" then "" else " ") + extraDescription;
178178+ ${if default != null then "default" else null} = defaultValue;
146179 ${if example != null then "example" else null} = literalExpression
147180 (if isList example then "pkgs." + concatStringsSep "." example else example);
148181 };
149182150183 /* Like mkPackageOption, but emit an mdDoc description instead of DocBook. */
151151- mkPackageOptionMD = args: name: extra:
152152- let option = mkPackageOption args name extra;
184184+ mkPackageOptionMD = pkgs: name: extra:
185185+ let option = mkPackageOption pkgs name extra;
153186 in option // { description = lib.mdDoc option.description; };
154187155188 /* This option accepts anything, but it does not produce any result.
···101101102102**Note**: You shouldn’t necessarily make package options for all of your modules. You can always overwrite a specific package throughout nixpkgs by using [nixpkgs overlays](https://nixos.org/manual/nixpkgs/stable/#chap-overlays).
103103104104-The default package is specified as a list of strings representing its attribute path in nixpkgs. Because of this, you need to pass nixpkgs itself as the first argument.
104104+The package is specified in the third argument under `default` as a list of strings
105105+representing its attribute path in nixpkgs (or another package set).
106106+Because of this, you need to pass nixpkgs itself (or a subset) as the first argument.
107107+108108+The second argument may be either a string or a list of strings.
109109+It provides the display name of the package in the description of the generated option
110110+(using only the last element if the passed value is a list)
111111+and serves as the fallback value for the `default` argument.
105112106106-The second argument is the name of the option, used in the description "The \<name\> package to use.". You can also pass an example value, either a literal string or a package's attribute path.
113113+To include extra information in the description, pass `extraDescription` to
114114+append arbitrary text to the generated description.
115115+You can also pass an `example` value, either a literal string or an attribute path.
107116108108-You can omit the default path if the name of the option is also attribute path in nixpkgs.
117117+The default argument can be omitted if the provided name is
118118+an attribute of pkgs (if name is a string) or a
119119+valid attribute path in pkgs (if name is a list).
120120+121121+If you wish to explicitly provide no default, pass `null` as `default`.
109122110123During the transition to CommonMark documentation `mkPackageOption` creates an option with a DocBook description attribute, once the transition is completed it will create a CommonMark description instead. `mkPackageOptionMD` always creates an option with a CommonMark description attribute and will be removed some time after the transition is completed.
111124···138151 defaultText = lib.literalExpression "pkgs.ghc";
139152 example = lib.literalExpression "pkgs.haskell.packages.ghc92.ghc.withPackages (hkgs: [ hkgs.primes ])";
140153 description = lib.mdDoc "The GHC package to use.";
154154+}
155155+```
156156+:::
157157+158158+::: {#ex-options-declarations-util-mkPackageOption-extraDescription .example}
159159+```nix
160160+mkPackageOption pkgs [ "python39Packages" "pytorch" ] {
161161+ extraDescription = "This is an example and doesn't actually do anything.";
162162+}
163163+# is like
164164+lib.mkOption {
165165+ type = lib.types.package;
166166+ default = pkgs.python39Packages.pytorch;
167167+ defaultText = lib.literalExpression "pkgs.python39Packages.pytorch";
168168+ description = "The pytorch package to use. This is an example and doesn't actually do anything.";
141169}
142170```
143171:::