all: refactor to follow RFC140

This commit is contained in:
xeals 2023-09-21 12:27:35 +10:00
parent 029f6e7795
commit 93b6195542
Signed by: xeals
SSH Key Fingerprint: SHA256:pRv+8swQDA+/LuZ7NHj9m006BbKexlNK62OUA01ZZBc
21 changed files with 103 additions and 28 deletions

View File

@ -1,15 +0,0 @@
{ pkgs
, lib ? pkgs.lib
, unitDir ? "unit"
, packageFun ? "package.nix"
, root ? "${./pkgs}/${unitDir}"
}:
let
shards = lib.attrNames (builtins.readDir root);
namesForShard = shard: lib.mapAttrs'
(name: _: { inherit name; value = root + "/${shard}/${name}"; })
(builtins.readDir (root + "/${shard}"));
namesToPath = lib.foldl' lib.recursiveUpdate { } (map namesForShard shards);
units = lib.mapAttrs (_: path: pkgs.callPackage (path + "/${packageFun}") { }) namesToPath;
in
units

View File

@ -8,10 +8,10 @@
{ pkgs ? import <nixpkgs> { } }: { pkgs ? import <nixpkgs> { } }:
let let
legacy = import ./pkgs/top-level/all-packages.nix { inherit pkgs; }; system = pkgs.stdenv.hostPlatform.system;
units = import ./callUnitRoot.nix { inherit pkgs; root = ./pkgs/unit; }; packages = import ./pkgs/top-level { localSystem = system; inherit pkgs; };
in in
legacy // units // { packages // {
# The `lib`, `modules`, and `overlay` names are special # The `lib`, `modules`, and `overlay` names are special
lib = import ./lib { inherit pkgs; }; # functions lib = import ./lib { inherit pkgs; }; # functions
modules = import ./modules; # NixOS modules modules = import ./modules; # NixOS modules

View File

@ -15,16 +15,7 @@
pkgs = import nixpkgs { inherit system; }; pkgs = import nixpkgs { inherit system; };
in in
{ {
# nixos/rfcs#140 packages = import ./pkgs/top-level { localSystem = system; inherit pkgs; };
# Only produces the package set of the proposed functionality.
# Unstable names are variables.
packages =
let
legacyPackages = import ./pkgs/top-level/all-packages.nix { inherit pkgs; };
unitPackages = import ./callUnitRoot.nix { inherit pkgs; };
onlyAvailable = lib.filterAttrs (_: drv: builtins.elem system (drv.meta.platforms or [ ]));
in
onlyAvailable (legacyPackages // unitPackages);
checks = { checks = {
nixpkgs-fmt = pkgs.writeShellScriptBin "nixpkgs-fmt-check" '' nixpkgs-fmt = pkgs.writeShellScriptBin "nixpkgs-fmt-check" ''

View File

@ -0,0 +1,56 @@
# This file turns the pkgs/by-name directory (see its README.md for more info)
# into an overlay that adds all the defined packages.
#
# No validity checks are done here, instead this file is optimised for
# performance, and validity checks are done by CI on PRs.
#
# This file is based on Nixpkgs' `pkgs/top-level/by-name-overlay.nix` in order
# to utilise the same infrastructure and layout, with some adjustments to fit
# our derivative project.
{ lib
, pkgs
}:
# Type: Path -> Overlay
baseDirectory:
let
inherit (builtins)
readDir
;
inherit (lib.attrsets)
mapAttrs
mapAttrsToList
mergeAttrsList
;
# Package files for a single shard
# Type: String -> String -> AttrsOf Path
namesForShard = shard: type:
if type != "directory" then
# Ignore all non-directories. Technically only README.md is allowed as a file in the base directory, so we could alternatively:
# - Assume that README.md is the only file and change the condition to `shard == "README.md"` for a minor performance improvement.
# This would however cause very poor error messages if there's other files.
# - Ensure that README.md is the only file, throwing a better error message if that's not the case.
# 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.
# Additionally in either of those alternatives, we would have to duplicate the hardcoding of "README.md"
{ }
else
mapAttrs
(name: _: baseDirectory + "/${shard}/${name}/package.nix")
(readDir (baseDirectory + "/${shard}"));
# The attribute set mapping names to the package files defining them
# This is defined up here in order to allow reuse of the value (it's kind of expensive to compute)
# if the overlay has to be applied multiple times
packageFiles = mergeAttrsList (mapAttrsToList namesForShard (readDir baseDirectory));
in
# TODO: Consider optimising this using `builtins.deepSeq packageFiles`,
# which could free up the above thunks and reduce GC times.
# Currently this would be hard to measure until we have more packages
# and ideally https://github.com/NixOS/nix/pull/8895
_self: _super:
mapAttrs
(_name: file: pkgs.callPackage file { })
packageFiles

View File

@ -0,0 +1,19 @@
# Composes the packages collection.
{
# The system packages will be build and used on.
localSystem
# Nixpkgs
, pkgs
# Nixpkgs lib
, lib ? pkgs.lib
}:
let
allPackages = import ./stage.nix {
inherit lib pkgs;
};
available = lib.filterAttrs
(_: drv: builtins.elem localSystem (drv.meta.platforms or [ ]));
in
available allPackages

24
pkgs/top-level/stage.nix Normal file
View File

@ -0,0 +1,24 @@
# Composes a single bootstrapping of the package collection. The result is a set
# of all the packages for some particular platform.
{ lib
, pkgs
}:
let
# An overlay to auto-call packages in .../by-name.
autoCalledPackages =
import ./by-name-overlay.nix { inherit pkgs lib; } ../by-name;
allPackages = _self: _super:
import ./all-packages.nix { inherit pkgs; };
toFix = (lib.flip lib.composeManyExtensions) (_self: { }) [
autoCalledPackages
allPackages
];
in
# Return the complete set of packages.
lib.fix toFix