core/pkgs/top-level/default.nix

161 lines
5.3 KiB
Nix
Raw Normal View History

2024-06-30 08:16:52 +00:00
/*
This function composes the Nix Packages collection. It:
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
1. Elaborates `localSystem` and `crossSystem` with defaults as needed.
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
2. Applies the final stage to the given `config` if it is a function
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
3. Defaults to no non-standard config and no cross-compilation target
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
4. Uses the above to infer the default standard environment's (stdenv's)
stages if no stdenv's are provided
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
5. Folds the stages to yield the final fully booted package set for the
chosen stdenv
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
Use `impure.nix` to also infer the `system` based on the one on which
evaluation is taking place, and the configuration from environment variables
or dot-files.
*/
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
{
# The system packages will be built on. See the manual for the
2024-05-02 00:46:19 +00:00
# subtle division of labor between these two `*System`s and the three
# `*Platform`s.
2024-06-30 08:16:52 +00:00
localSystem,
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
lib ? import ../../lib,
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# The system packages will ultimately be run on.
crossSystem ? localSystem,
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# Allow a configuration attribute set to be passed in as an argument.
config ? { },
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# List of overlays layers used to extend Nixpkgs.
overlays ? [ ],
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# List of overlays to apply to target packages only.
crossOverlays ? [ ],
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# A function booting the final package set for a specific standard
2024-05-02 00:46:19 +00:00
# environment. See below for the arguments given to that function, the type of
# list it returns.
2024-06-30 08:16:52 +00:00
stdenvStages ? import ../stdenv,
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# Ignore unexpected args.
2024-05-02 00:46:19 +00:00
...
2024-06-30 08:16:52 +00:00
}@args:
2024-05-02 00:46:19 +00:00
let # Rename the function arguments
config0 = config;
crossSystem0 = crossSystem;
2024-06-30 08:16:52 +00:00
in
let
2024-05-02 00:46:19 +00:00
inherit (lib) throwIfNot;
checked =
2024-06-30 08:16:52 +00:00
throwIfNot (lib.isList overlays) "The overlays argument to nixpkgs must be a list." lib.foldr
(x: throwIfNot (lib.isFunction x) "All overlays passed to nixpkgs must be functions.")
(r: r)
overlays
throwIfNot
(lib.isList crossOverlays)
"The crossOverlays argument to nixpkgs must be a list."
lib.foldr
(x: throwIfNot (lib.isFunction x) "All crossOverlays passed to nixpkgs must be functions.")
(r: r)
crossOverlays;
2024-05-02 00:46:19 +00:00
localSystem = lib.systems.elaborate args.localSystem;
# Condition preserves sharing which in turn affects equality.
#
# See `lib.systems.equals` documentation for more details.
#
# Note that it is generally not possible to compare systems as given in
# parameters, e.g. if systems are initialized as
#
# localSystem = { system = "x86_64-linux"; };
# crossSystem = { config = "x86_64-unknown-linux-gnu"; };
#
# Both systems are semantically equivalent as the same vendor and ABI are
# inferred from the system double in `localSystem`.
crossSystem =
2024-06-30 08:16:52 +00:00
let
system = lib.systems.elaborate crossSystem0;
in
if crossSystem0 == null || lib.systems.equals system localSystem then localSystem else system;
2024-05-02 00:46:19 +00:00
# Allow both:
# { /* the config */ } and
# { pkgs, ... } : { /* the config */ }
2024-06-30 08:16:52 +00:00
config1 = if lib.isFunction config0 then config0 { inherit pkgs; } else config0;
2024-05-02 00:46:19 +00:00
configEval = lib.evalModules {
modules = [
./config.nix
2024-06-30 08:16:52 +00:00
(
{ options, ... }:
{
_file = "nixpkgs.config";
config = config1;
}
)
2024-05-02 00:46:19 +00:00
];
class = "nixpkgsConfig";
};
# take all the rest as-is
config = lib.showWarnings configEval.config.warnings configEval.config;
# A few packages make a new package set to draw their dependencies from.
# (Currently to get a cross tool chain, or forced-i686 package.) Rather than
# give `all-packages.nix` all the arguments to this function, even ones that
# don't concern it, we give it this function to "re-call" nixpkgs, inheriting
# whatever arguments it doesn't explicitly provide. This way,
# `all-packages.nix` doesn't know more than it needs too.
#
# It's OK that `args` doesn't include default arguments from this file:
# they'll be deterministically inferred. In fact we must *not* include them,
# because it's important that if some parameter which affects the default is
# substituted with a different argument, the default is re-inferred.
#
# To put this in concrete terms, this function is basically just used today to
# use package for a different platform for the current platform (namely cross
# compiling toolchains and 32-bit packages on x86_64). In both those cases we
# want the provided non-native `localSystem` argument to affect the stdenv
# chosen.
#
# NB!!! This thing gets its `config` argument from `args`, i.e. it's actually
# `config0`. It is important to keep it to `config0` format (as opposed to the
# result of `evalModules`, i.e. the `config` variable above) throughout all
# nixpkgs evaluations since the above function `config0 -> config` implemented
# via `evalModules` is not idempotent. In other words, if you add `config` to
# `newArgs`, expect strange very hard to debug errors! (Yes, I'm speaking from
# experience here.)
nixpkgsFun = newArgs: import ./. (args // newArgs);
# Partially apply some arguments for building bootstraping stage pkgs
# sets. Only apply arguments which no stdenv would want to override.
2024-06-30 08:16:52 +00:00
allPackages = newArgs: import ./stage.nix ({ inherit lib nixpkgsFun; } // newArgs);
2024-05-02 00:46:19 +00:00
boot = import ../stdenv/booter.nix { inherit lib allPackages; };
stages = stdenvStages {
2024-06-30 08:16:52 +00:00
inherit
lib
localSystem
crossSystem
config
overlays
crossOverlays
;
2024-05-02 00:46:19 +00:00
};
pkgs = boot stages;
2024-06-30 08:16:52 +00:00
in
checked pkgs