···
See [here](../../.github/workflows/check-by-name.yml) for more info.
122
+
## Recommendation for new packages with multiple versions
124
+
These checks of the `pkgs/by-name` structure can cause problems in combination:
125
+
1. New top-level packages using `callPackage` must be defined via `pkgs/by-name`.
126
+
2. Packages in `pkgs/by-name` cannot refer to files outside their own directory.
128
+
This means that outside `pkgs/by-name`, multiple already-present top-level packages can refer to some common file.
129
+
If you open a PR to another instance of such a package, CI will fail check 1,
130
+
but if you try to move the package to `pkgs/by-name`, it will fail check 2.
132
+
This is often the case for packages with multiple versions, such as
135
+
foo_1 = callPackage ../tools/foo/1.nix { };
136
+
foo_2 = callPackage ../tools/foo/2.nix { };
139
+
The best way to resolve this is to not use `callPackage` directly, such that check 1 doesn't trigger.
140
+
This can be done by using `inherit` on a local package set:
144
+
foo_1 = callPackage ../tools/foo/1.nix { };
145
+
foo_2 = callPackage ../tools/foo/2.nix { };
152
+
While this may seem pointless, this can in fact help with future package set refactorings,
153
+
because it establishes a clear connection between related attributes.
155
+
### Further possible refactorings
157
+
This is not required, but the above solution also allows refactoring the definitions into a separate file:
160
+
inherit (import ../tools/foo pkgs)
165
+
# pkgs/tools/foo/default.nix
167
+
foo_1 = callPackage ./1.nix { };
168
+
foo_2 = callPackage ./2.nix { };
172
+
Alternatively using [`callPackages`](https://nixos.org/manual/nixpkgs/unstable/#function-library-lib.customisation.callPackagesWith)
173
+
if `callPackage` isn't used underneath and you want the same `.override` arguments for all attributes:
176
+
inherit (callPackages ../tools/foo { })
181
+
# pkgs/tools/foo/default.nix
185
+
foo_1 = stdenv.mkDerivation { /* ... */ };
186
+
foo_2 = stdenv.mkDerivation { /* ... */ };
190
+
### Exposing the package set
192
+
This is not required, but the above solution also allows exposing the package set as an attribute:
195
+
foo-versions = import ../tools/foo pkgs;
196
+
# Or using callPackages
197
+
# foo-versions = callPackages ../tools/foo { };
199
+
inherit (foo-versions) foo_1 foo_2;