1 # Name-based package directories
3 The structure of this directory maps almost directly to top-level package attributes.
4 Add new top-level packages to Nixpkgs using this mechanism [whenever possible](#limitations).
6 Packages found in the name-based structure are automatically included, without needing to be added to `all-packages.nix`. However if the implicit attribute defaults need to be changed for a package, this [must still be declared in `all-packages.nix`](#changing-implicit-attribute-defaults).
10 The top-level package `pkgs.some-package` may be declared by setting up this file structure:
21 Where `some-package` is the attribute name corresponding to the package, and `so` is the lowercase 2-letter prefix of the attribute name.
23 The `package.nix` may look like this:
26 # A function taking an attribute set as an argument
28 # Get access to top-level attributes for use as dependencies
33 # Make this derivation configurable using `.override { enableBar = true }`
37 # The return value must be a derivation
41 lib.optional enableBar libbar;
45 You can also split up the package definition into more files in the same directory if necessary.
47 Once defined, the package can be built from the Nixpkgs root directory using:
49 nix-build -A some-package
52 See the [general package conventions](../README.md#conventions) for more information on package definitions.
54 ### Changing implicit attribute defaults
56 The above expression is called using these arguments by default:
65 But the package might need `pkgs.libbar_2` instead.
66 While the function could be changed to take `libbar_2` directly as an argument,
67 this would change the `.override` interface, breaking code like `.override { libbar = ...; }`.
68 So instead it is preferable to use the same generic parameter name `libbar`
69 and override its value in [`pkgs/top-level/all-packages.nix`](../top-level/all-packages.nix):
73 libfoo = callPackage ../by-name/so/some-package/package.nix {
79 ## Manual migration guidelines
81 Most packages are still defined in `all-packages.nix` and the [category hierarchy](../README.md#category-hierarchy).
82 Since it would take a lot of contributor and reviewer time to migrate all packages manually,
83 an [automated migration is planned](https://github.com/NixOS/nixpkgs/pull/211832),
84 though it is expected to still take some time to get done.
85 If you're interested in helping out with this effort,
86 please see [this ticket](https://github.com/NixOS/nixpkgs-vet/issues/56).
88 Since [only PRs to packages in `pkgs/by-name` can be automatically merged](../../CONTRIBUTING.md#how-to-merge-pull-requests),
89 if package maintainers would like to use this feature, they are welcome to migrate their packages to `pkgs/by-name`.
90 To lessen PR traffic, they're encouraged to also perform some more general maintenance on the package in the same PR,
91 though this is not required and must not be expected.
93 Note that `callPackage` definitions in `all-packages.nix` with custom arguments should not be removed.
94 That is a backwards-incompatible change because it changes the `.override` interface.
95 Such packages may still be moved to `pkgs/by-name` however, in order to avoid the slightly superficial choice of directory / category in which the `default.nix` file was placed, but please keep the definition in `all-packages.nix` using `callPackage`.
96 See also [changing implicit attribute defaults](#changing-implicit-attribute-defaults).
98 Definitions like the following however, _can_ be transitioned:
102 fooWithBaz = foo.override {
105 # turned into pkgs/by-name/fo/fooWithBaz/package.nix with:
118 There's some limitations as to which packages can be defined using this structure:
120 - Only packages defined using `pkgs.callPackage`.
121 This excludes packages defined using `pkgs.python3Packages.callPackage ...`.
124 - Either change the package definition to work with `pkgs.callPackage`.
125 - Or use the [category hierarchy](../README.md#category-hierarchy).
127 - Only top-level packages.
128 This excludes packages for other package sets like `pkgs.pythonPackages.*`.
130 Refer to the definition and documentation of the respective package set to figure out how such packages can be declared.
134 CI performs [certain checks](https://github.com/NixOS/nixpkgs-vet?tab=readme-ov-file#validity-checks) on the `pkgs/by-name` structure.
135 This is done using the [`nixpkgs-vet` tool](https://github.com/NixOS/nixpkgs-vet).
137 You can locally emulate the CI check using
140 $ ./ci/nixpkgs-vet.sh master
143 See [here](../../.github/workflows/nixpkgs-vet.yml) for more info.
145 ## Recommendation for new packages with multiple versions
147 These checks of the `pkgs/by-name` structure can cause problems in combination:
148 1. New top-level packages using `callPackage` must be defined via `pkgs/by-name`.
149 2. Packages in `pkgs/by-name` cannot refer to files outside their own directory.
151 This means that outside `pkgs/by-name`, multiple already-present top-level packages can refer to some common file.
152 If you open a PR to another instance of such a package, CI will fail check 1,
153 but if you try to move the package to `pkgs/by-name`, it will fail check 2.
155 This is often the case for packages with multiple versions, such as
159 foo_1 = callPackage ../tools/foo/1.nix { };
160 foo_2 = callPackage ../tools/foo/2.nix { };
164 The best way to resolve this is to not use `callPackage` directly, such that check 1 doesn't trigger.
165 This can be done by using `inherit` on a local package set:
170 foo_1 = callPackage ../tools/foo/1.nix { };
171 foo_2 = callPackage ../tools/foo/2.nix { };
179 While this may seem pointless, this can in fact help with future package set refactorings,
180 because it establishes a clear connection between related attributes.
182 ### Further possible refactorings
184 This is not required, but the above solution also allows refactoring the definitions into a separate file:
188 inherit (import ../tools/foo pkgs)
194 # pkgs/tools/foo/default.nix
196 foo_1 = callPackage ./1.nix { };
197 foo_2 = callPackage ./2.nix { };
201 Alternatively using [`callPackages`](https://nixos.org/manual/nixpkgs/unstable/#function-library-lib.customisation.callPackagesWith)
202 if `callPackage` isn't used underneath and you want the same `.override` arguments for all attributes:
206 inherit (callPackages ../tools/foo { })
212 # pkgs/tools/foo/default.nix
216 foo_1 = stdenv.mkDerivation { /* ... */ };
217 foo_2 = stdenv.mkDerivation { /* ... */ };
221 ### Exposing the package set
223 This is not required, but the above solution also allows exposing the package set as an attribute:
227 foo-versions = import ../tools/foo pkgs;
228 # Or using callPackages
229 # foo-versions = callPackages ../tools/foo { };
231 inherit (foo-versions) foo_1 foo_2;