Revert "nixos/nixpkgs: make config.nixpkgs.{localSystem,crossSystem,buildPlatform,hostPlatform} write only"

This reverts commit 0a19371146.
This commit is contained in:
Robert Hensing 2025-02-05 14:29:18 +01:00
parent 692a57e61d
commit 0b47fba230
6 changed files with 46 additions and 37 deletions

View file

@ -73,10 +73,7 @@ let
defaultPkgs =
if opt.hostPlatform.isDefined then
let
isCross =
!(lib.systems.equals (lib.systems.elaborate cfg.buildPlatform) (
lib.systems.elaborate cfg.hostPlatform
));
isCross = cfg.buildPlatform != cfg.hostPlatform;
systemArgs =
if isCross then
{
@ -198,10 +195,13 @@ in
};
hostPlatform = lib.mkOption {
type = lib.types.either lib.types.str lib.types.attrs;
type = lib.types.either lib.types.str lib.types.attrs; # TODO utilize lib.systems.parsedPlatform
example = {
system = "aarch64-linux";
};
# Make sure that the final value has all fields for sake of other modules
# referring to this. TODO make `lib.systems` itself use the module system.
apply = lib.systems.elaborate;
defaultText = lib.literalExpression ''(import "''${nixos}/../lib").lib.systems.examples.aarch64-multiplatform'';
description = ''
Specifies the platform where the NixOS configuration will run.
@ -213,13 +213,22 @@ in
};
buildPlatform = lib.mkOption {
type = lib.types.either lib.types.str lib.types.attrs;
type = lib.types.either lib.types.str lib.types.attrs; # TODO utilize lib.systems.parsedPlatform
default = cfg.hostPlatform;
example = {
system = "x86_64-linux";
};
# Make sure that the final value has all fields for sake of other modules
# referring to this.
apply =
inputBuildPlatform:
let
elaborated = lib.systems.elaborate inputBuildPlatform;
in
if lib.systems.equals elaborated cfg.hostPlatform then
cfg.hostPlatform # make identical, so that `==` equality works; see https://github.com/NixOS/nixpkgs/issues/278001
else
elaborated;
defaultText = lib.literalExpression ''config.nixpkgs.hostPlatform'';
description = ''
Specifies the platform on which NixOS should be built.
@ -236,11 +245,14 @@ in
};
localSystem = lib.mkOption {
type = lib.types.attrs;
type = lib.types.attrs; # TODO utilize lib.systems.parsedPlatform
default = { inherit (cfg) system; };
example = {
system = "aarch64-linux";
};
# Make sure that the final value has all fields for sake of other modules
# referring to this. TODO make `lib.systems` itself use the module system.
apply = lib.systems.elaborate;
defaultText = lib.literalExpression ''(import "''${nixos}/../lib").lib.systems.examples.aarch64-multiplatform'';
description = ''
Systems with a recently generated `hardware-configuration.nix`
@ -268,7 +280,7 @@ in
# is a relation between at least 2 systems in the context of a
# specific build step, not a single system.
crossSystem = lib.mkOption {
type = lib.types.nullOr lib.types.attrs;
type = lib.types.nullOr lib.types.attrs; # TODO utilize lib.systems.parsedPlatform
default = null;
example = {
system = "aarch64-linux";
@ -404,18 +416,6 @@ in
${lib.concatMapStringsSep "\n" (file: " - ${file}") opt.config.files}
'';
}
{
assertion =
(opt.hostPlatform.isDefined -> builtins.isAttrs cfg.buildPlatform -> !(cfg.buildPlatform ? parsed))
&& (opt.hostPlatform.isDefined -> builtins.isAttrs cfg.hostPlatform -> !(cfg.hostPlatform ? parsed))
&& (builtins.isAttrs cfg.localSystem -> !(cfg.localSystem ? parsed))
&& (builtins.isAttrs cfg.crossSystem -> !(cfg.crossSystem ? parsed));
message = ''
Passing fully elaborated systems to `nixpkgs.localSystem`, `nixpkgs.crossSystem`, `nixpkgs.buildPlatform`
or `nixpkgs.hostPlatform` will break composability of package sets in nixpkgs. For example, pkgs.pkgsStatic
would not work in modules anymore.
'';
}
];
};

View file

@ -40,11 +40,20 @@ in
The Nixpkgs overlays that `pkgs` was initialized with.
'';
};
# buildPlatform and hostPlatform left out on purpose:
# - They are not supposed to be changed with this read-only module.
# - They are not supposed to be read either, according to the description
# of "system" in the traditional nixpkgs module.
#
hostPlatform = mkOption {
internal = true;
readOnly = true;
description = ''
The platform of the machine that is running the NixOS configuration.
'';
};
buildPlatform = mkOption {
internal = true;
readOnly = true;
description = ''
The platform of the machine that built the NixOS configuration.
'';
};
# NOTE: do not add the legacy options such as localSystem here. Let's keep
# this module simple and let module authors upgrade their code instead.
};
@ -52,8 +61,12 @@ in
config = {
_module.args.pkgs =
# find mistaken definitions
builtins.seq cfg.config builtins.seq cfg.overlays cfg.pkgs;
builtins.seq cfg.config builtins.seq cfg.overlays builtins.seq cfg.hostPlatform builtins.seq
cfg.buildPlatform
cfg.pkgs;
nixpkgs.config = cfg.pkgs.config;
nixpkgs.overlays = cfg.pkgs.overlays;
nixpkgs.hostPlatform = cfg.pkgs.stdenv.hostPlatform;
nixpkgs.buildPlatform = cfg.pkgs.stdenv.buildPlatform;
};
}

View file

@ -506,12 +506,8 @@ in
config = {
nixpkgs =
if options.nixpkgs?hostPlatform
then {
hostPlatform =
if host.options.nixpkgs.hostPlatform.isDefined
then host.config.nixpkgs.hostPlatform
else lib.defaultTo host.config.nixpkgs.localSystem host.config.nixpkgs.crossSystem;
} else { localSystem = lib.defaultTo host.config.nixpkgs.localSystem host.config.nixpkgs.crossSystem; }
then { inherit (host.pkgs.stdenv) hostPlatform; }
else { localSystem = host.pkgs.stdenv.hostPlatform; }
;
boot.isContainer = true;
networking.hostName = mkDefault name;

View file

@ -40,7 +40,7 @@
verityStore = {
enable = true;
# by default the module works with systemd-boot, for simplicity this test directly boots the UKI
ukiPath = "/EFI/BOOT/BOOT${lib.toUpper pkgs.stdenv.hostPlatform.efiArch}.EFI";
ukiPath = "/EFI/BOOT/BOOT${lib.toUpper config.nixpkgs.hostPlatform.efiArch}.EFI";
};
name = "appliance-verity-store-image";
@ -51,7 +51,7 @@
repartConfig = {
Type = "esp";
Format = "vfat";
SizeMinBytes = if pkgs.stdenv.hostPlatform.isx86_64 then "64M" else "96M";
SizeMinBytes = if config.nixpkgs.hostPlatform.isx86_64 then "64M" else "96M";
};
};
${partitionIds.store-verity}.repartConfig = {

View file

@ -53,7 +53,7 @@ in
"esp" = {
contents =
let
efiArch = pkgs.stdenv.hostPlatform.efiArch;
efiArch = config.nixpkgs.hostPlatform.efiArch;
in
{
"/EFI/BOOT/BOOT${lib.toUpper efiArch}.EFI".source =
@ -70,7 +70,7 @@ in
# aarch64 kernel seems to generally be a little bigger than the
# x86_64 kernel. To stay on the safe side, leave some more slack
# for every platform other than x86_64.
SizeMinBytes = if pkgs.stdenv.hostPlatform.isx86_64 then "64M" else "96M";
SizeMinBytes = if config.nixpkgs.hostPlatform.isx86_64 then "64M" else "96M";
};
};
"swap" = {

View file

@ -17721,7 +17721,7 @@ with pkgs;
[(
{ lib, ... }: {
config.nixpkgs.pkgs = lib.mkDefault pkgs;
config.nixpkgs.localSystem = lib.mkDefault ({ config = lib.systems.parse.tripleFromSystem stdenv.hostPlatform; });
config.nixpkgs.localSystem = lib.mkDefault stdenv.hostPlatform;
}
)] ++ (
if builtins.isList configuration