Home Explore Blog Models CI



nixpkgs

2nd chunk of `nixos/doc/manual/development/option-declarations.section.md`
a7c4fdf27fdaa11217c5cd9785d2efece9d44f2287db60530000000100000cc1
mkPackageOption pkgs "name" {
  default = [
    "path"
    "in"
    "pkgs"
  ];
  example = "literal example";
}
```

Creates an Option attribute set for an option that specifies the package a module should use for some purpose.

**Note**: You should make package options for your modules, where applicable. While one can always overwrite a specific package throughout nixpkgs by using [nixpkgs overlays](https://nixos.org/manual/nixpkgs/stable/#chap-overlays), they slow down nixpkgs evaluation significantly and are harder to debug when issues arise.

The package is specified in the third argument under `default` as a list of strings
representing its attribute path in nixpkgs (or another package set).
Because of this, you need to pass nixpkgs itself (or a subset) as the first argument.

The second argument may be either a string or a list of strings.
It provides the display name of the package in the description of the generated option
(using only the last element if the passed value is a list)
and serves as the fallback value for the `default` argument.

To include extra information in the description, pass `extraDescription` to
append arbitrary text to the generated description.
You can also pass an `example` value, either a literal string or an attribute path.

The default argument can be omitted if the provided name is
an attribute of pkgs (if name is a string) or a
valid attribute path in pkgs (if name is a list).

If you wish to explicitly provide no default, pass `null` as `default`.

[]{#ex-options-declarations-util-mkPackageOption}
Examples:

::: {#ex-options-declarations-util-mkPackageOption-hello .example}
### Simple `mkPackageOption` usage
```nix
lib.mkPackageOption pkgs "hello" { }
  # is like
  lib.mkOption
  {
    type = lib.types.package;
    default = pkgs.hello;
    defaultText = lib.literalExpression "pkgs.hello";
    description = "The hello package to use.";
  }
```
:::

::: {#ex-options-declarations-util-mkPackageOption-ghc .example}
### `mkPackageOption` with explicit default and example
```nix
lib.mkPackageOption pkgs "GHC"
  {
    default = [ "ghc" ];
    example = "pkgs.haskellPackages.ghc.withPackages (hkgs: [ hkgs.primes ])";
  }
  # is like
  lib.mkOption
  {
    type = lib.types.package;
    default = pkgs.ghc;
    defaultText = lib.literalExpression "pkgs.ghc";
    example = lib.literalExpression "pkgs.haskellPackages.ghc.withPackages (hkgs: [ hkgs.primes ])";
    description = "The GHC package to use.";
  }
```
:::

::: {#ex-options-declarations-util-mkPackageOption-extraDescription .example}
### `mkPackageOption` with additional description text
```nix
mkPackageOption pkgs [ "python312Packages" "torch" ]
  {
    extraDescription = "This is an example and doesn't actually do anything.";
  }
  # is like
  lib.mkOption
  {
    type = lib.types.package;
    default = pkgs.python312Packages.torch;
    defaultText = lib.literalExpression "pkgs.python312Packages.torch";
    description = "The pytorch package to use. This is an example and doesn't actually do anything.";
  }
```
:::

## Extensible Option Types {#sec-option-declarations-eot}

Extensible option types is a feature that allows to extend certain types
declaration through multiple module files. This feature only work with a

Title: NixOS `mkPackageOption` Details and Extensible Option Types
Summary
This chunk elaborates on the `mkPackageOption` utility function, explaining its arguments including `pkgs` (the package set), the display name (string or list of strings), `default` (attribute path to the package), `example`, and `extraDescription` for adding custom text to the option's description. It stresses the importance of using `mkPackageOption` to define module-specific package dependencies, which is more efficient and debuggable than using global Nixpkgs overlays. Several usage examples are provided. The chunk then introduces 'Extensible Option Types,' a feature designed to allow type declarations to be extended across multiple module files.