1# This file turns the pkgs/by-name directory (see its README.md for more info) into an overlay that adds all the defined packages. 2# No validity checks are done here, 3# instead this file is optimised for performance, 4# and validity checks are done by CI on PRs. 5 6# Type: Path -> Overlay 7baseDirectory: 8let 9 # Because of Nix's import-value cache, importing lib is free 10 lib = import ../../lib; 11 12 inherit (builtins) 13 readDir 14 ; 15 16 inherit (lib.attrsets) 17 mapAttrs 18 mapAttrsToList 19 mergeAttrsList 20 ; 21 22 # Package files for a single shard 23 # Type: String -> String -> AttrsOf Path 24 namesForShard = 25 shard: type: 26 if type != "directory" then 27 # Ignore all non-directories. Technically only README.md is allowed as a file in the base directory, so we could alternatively: 28 # - Assume that README.md is the only file and change the condition to `shard == "README.md"` for a minor performance improvement. 29 # This would however cause very poor error messages if there's other files. 30 # - Ensure that README.md is the only file, throwing a better error message if that's not the case. 31 # However this would make for a poor code architecture, because one type of error would have to be duplicated in the validity checks and here. 32 # Additionally in either of those alternatives, we would have to duplicate the hardcoding of "README.md" 33 { } 34 else 35 mapAttrs (name: _: baseDirectory + "/${shard}/${name}/package.nix") ( 36 readDir (baseDirectory + "/${shard}") 37 ); 38 39 # The attribute set mapping names to the package files defining them 40 # This is defined up here in order to allow reuse of the value (it's kind of expensive to compute) 41 # if the overlay has to be applied multiple times 42 packageFiles = mergeAttrsList (mapAttrsToList namesForShard (readDir baseDirectory)); 43in 44# TODO: Consider optimising this using `builtins.deepSeq packageFiles`, 45# which could free up the above thunks and reduce GC times. 46# Currently this would be hard to measure until we have more packages 47# and ideally https://github.com/NixOS/nix/pull/8895 48self: super: 49{ 50 # This attribute is necessary to allow CI to ensure that all packages defined in `pkgs/by-name` 51 # don't have an overriding definition in `all-packages.nix` with an empty (`{ }`) second `callPackage` argument. 52 # It achieves that with an overlay that modifies both `callPackage` and this attribute to signal whether `callPackage` is used 53 # and whether it's defined by this file here or `all-packages.nix`. 54 # TODO: This can be removed once `pkgs/by-name` can handle custom `callPackage` arguments without `all-packages.nix` (or any other way of achieving the same result). 55 # Because at that point the code in ./stage.nix can be changed to not allow definitions in `all-packages.nix` to override ones from `pkgs/by-name` anymore and throw an error if that happens instead. 56 _internalCallByNamePackageFile = file: self.callPackage file { }; 57} 58// mapAttrs (name: self._internalCallByNamePackageFile) packageFiles