Merge pull request #177672 from hercules-ci/nixos-generate-config-add-nixpkgs.system

nixos-generate-config: Add nixpkgs.hostPlatform to hardware-configuration.nix

authored by Bernardo Meurer and committed by GitHub c7eacbf8 efe792bb

+261 -12
+2 -1
lib/default.nix
··· 131 131 getValues getFiles 132 132 optionAttrSetToDocList optionAttrSetToDocList' 133 133 scrubOptionValue literalExpression literalExample literalDocBook 134 - showOption showFiles unknownModule mkOption mkPackageOption 134 + showOption showOptionWithDefLocs showFiles 135 + unknownModule mkOption mkPackageOption 135 136 mdDoc literalMD; 136 137 inherit (self.types) isType setType defaultTypeMerge defaultFunctor 137 138 isOptionType mkOptionType;
+6
lib/options.nix
··· 7 7 collect 8 8 concatLists 9 9 concatMap 10 + concatMapStringsSep 10 11 elemAt 11 12 filter 12 13 foldl' ··· 339 340 else ": " + value; 340 341 in "\n- In `${def.file}'${result}" 341 342 ) defs; 343 + 344 + showOptionWithDefLocs = opt: '' 345 + ${showOption opt.loc}, with values defined in: 346 + ${concatMapStringsSep "\n" (defFile: " - ${defFile}") opt.files} 347 + ''; 342 348 343 349 unknownModule = "<unknown-file>"; 344 350
+59
nixos/doc/manual/from_md/release-notes/rl-2211.section.xml
··· 33 33 </listitem> 34 34 <listitem> 35 35 <para> 36 + The <literal>nixpkgs.hostPlatform</literal> and 37 + <literal>nixpkgs.buildPlatform</literal> options have been 38 + added. These cover and override the 39 + <literal>nixpkgs.{system,localSystem,crossSystem}</literal> 40 + options. 41 + </para> 42 + <itemizedlist spacing="compact"> 43 + <listitem> 44 + <para> 45 + <literal>hostPlatform</literal> is the platform or 46 + <quote><literal>system</literal></quote> string of the 47 + NixOS system described by the configuration. 48 + </para> 49 + </listitem> 50 + <listitem> 51 + <para> 52 + <literal>buildPlatform</literal> is the platform that is 53 + responsible for building the NixOS configuration. It 54 + defaults to the <literal>hostPlatform</literal>, for a 55 + non-cross build configuration. To cross compile, set 56 + <literal>buildPlatform</literal> to a different value. 57 + </para> 58 + </listitem> 59 + </itemizedlist> 60 + <para> 61 + The new options convey the same information, but with fewer 62 + options, and following the Nixpkgs terminology. 63 + </para> 64 + <para> 65 + The existing options 66 + <literal>nixpkgs.{system,localSystem,crossSystem}</literal> 67 + have not been formally deprecated, to allow for evaluation of 68 + the change and to allow for a transition period so that in 69 + time the ecosystem can switch without breaking compatibility 70 + with any supported NixOS release. 71 + </para> 72 + </listitem> 73 + <listitem> 74 + <para> 75 + <literal>nixos-generate-config</literal> now generates 76 + configurations that can be built in pure mode. This is 77 + achieved by setting the new 78 + <literal>nixpkgs.hostPlatform</literal> option. 79 + </para> 80 + <para> 81 + You may have to unset the <literal>system</literal> parameter 82 + in <literal>lib.nixosSystem</literal>, or similarly remove 83 + definitions of the 84 + <literal>nixpkgs.{system,localSystem,crossSystem}</literal> 85 + options. 86 + </para> 87 + <para> 88 + Alternatively, you can remove the 89 + <literal>hostPlatform</literal> line and use NixOS like you 90 + would in NixOS 22.05 and earlier. 91 + </para> 92 + </listitem> 93 + <listitem> 94 + <para> 36 95 PHP now defaults to PHP 8.1, updated from 8.0. 37 96 </para> 38 97 </listitem>
+27
nixos/doc/manual/release-notes/rl-2211.section.md
··· 17 17 built for `stdenv.hostPlatform` (i.e. produced by `stdenv.cc`) by evaluating 18 18 `stdenv.buildPlatform.canExecute stdenv.hostPlatform`. 19 19 20 + - The `nixpkgs.hostPlatform` and `nixpkgs.buildPlatform` options have been added. 21 + These cover and override the `nixpkgs.{system,localSystem,crossSystem}` options. 22 + 23 + - `hostPlatform` is the platform or "`system`" string of the NixOS system 24 + described by the configuration. 25 + - `buildPlatform` is the platform that is responsible for building the NixOS 26 + configuration. It defaults to the `hostPlatform`, for a non-cross 27 + build configuration. To cross compile, set `buildPlatform` to a different 28 + value. 29 + 30 + The new options convey the same information, but with fewer options, and 31 + following the Nixpkgs terminology. 32 + 33 + The existing options `nixpkgs.{system,localSystem,crossSystem}` have not 34 + been formally deprecated, to allow for evaluation of the change and to allow 35 + for a transition period so that in time the ecosystem can switch without 36 + breaking compatibility with any supported NixOS release. 37 + 38 + - `nixos-generate-config` now generates configurations that can be built in pure 39 + mode. This is achieved by setting the new `nixpkgs.hostPlatform` option. 40 + 41 + You may have to unset the `system` parameter in `lib.nixosSystem`, or similarly 42 + remove definitions of the `nixpkgs.{system,localSystem,crossSystem}` options. 43 + 44 + Alternatively, you can remove the `hostPlatform` line and use NixOS like you 45 + would in NixOS 22.05 and earlier. 46 + 20 47 - PHP now defaults to PHP 8.1, updated from 8.0. 21 48 22 49 - `hardware.nvidia` has a new option `open` that can be used to opt in the opensource version of NVIDIA kernel driver. Note that the driver's support for GeForce and Workstation GPUs is still alpha quality, see [NVIDIA Releases Open-Source GPU Kernel Modules](https://developer.nvidia.com/blog/nvidia-releases-open-source-gpu-kernel-modules/) for the official announcement.
+9
nixos/modules/installer/tools/nixos-generate-config.pl
··· 84 84 } 85 85 86 86 87 + # nixpkgs.system 88 + my ($status, @systemLines) = runCommand("nix-instantiate --impure --eval --expr builtins.currentSystem"); 89 + if ($status != 0 || join("", @systemLines) =~ /error/) { 90 + die "Failed to retrieve current system type from nix.\n"; 91 + } 92 + chomp(my $system = @systemLines[0]); 93 + push @attrs, "nixpkgs.hostPlatform = lib.mkDefault $system;"; 94 + 95 + 87 96 my $cpuinfo = read_file "/proc/cpuinfo"; 88 97 89 98
+100 -8
nixos/modules/misc/nixpkgs.nix
··· 55 55 check = builtins.isAttrs; 56 56 }; 57 57 58 - defaultPkgs = import ../../.. { 59 - inherit (cfg) config overlays localSystem crossSystem; 60 - }; 58 + hasBuildPlatform = opt.buildPlatform.highestPrio < (mkOptionDefault {}).priority; 59 + hasHostPlatform = opt.hostPlatform.isDefined; 60 + hasPlatform = hasHostPlatform || hasBuildPlatform; 61 + 62 + # Context for messages 63 + hostPlatformLine = optionalString hasHostPlatform "${showOptionWithDefLocs opt.hostPlatform}"; 64 + buildPlatformLine = optionalString hasBuildPlatform "${showOptionWithDefLocs opt.buildPlatform}"; 65 + platformLines = optionalString hasPlatform '' 66 + Your system configuration configures nixpkgs with platform parameters: 67 + ${hostPlatformLine 68 + }${buildPlatformLine 69 + }''; 70 + 71 + legacyOptionsDefined = 72 + optional opt.system.isDefined opt.system 73 + ++ (optional (opt.localSystem.highestPrio < (mkOptionDefault {}).priority) opt.localSystem) 74 + ++ (optional (opt.crossSystem.highestPrio < (mkOptionDefault {}).priority) opt.crossSystem) 75 + ; 76 + 77 + defaultPkgs = 78 + if opt.hostPlatform.isDefined 79 + then 80 + let isCross = cfg.buildPlatform != cfg.hostPlatform; 81 + systemArgs = 82 + if isCross 83 + then { 84 + localSystem = cfg.buildPlatform; 85 + crossSystem = cfg.hostPlatform; 86 + } 87 + else { 88 + localSystem = cfg.hostPlatform; 89 + }; 90 + in 91 + import ../../.. ({ 92 + inherit (cfg) config overlays; 93 + } // systemArgs) 94 + else 95 + import ../../.. { 96 + inherit (cfg) config overlays localSystem crossSystem; 97 + }; 61 98 62 99 finalPkgs = if opt.pkgs.isDefined then cfg.pkgs.appendOverlays cfg.overlays else defaultPkgs; 63 100 ··· 157 194 ''; 158 195 }; 159 196 197 + hostPlatform = mkOption { 198 + type = types.either types.str types.attrs; # TODO utilize lib.systems.parsedPlatform 199 + example = { system = "aarch64-linux"; config = "aarch64-unknown-linux-gnu"; }; 200 + # Make sure that the final value has all fields for sake of other modules 201 + # referring to this. TODO make `lib.systems` itself use the module system. 202 + apply = lib.systems.elaborate; 203 + defaultText = literalExpression 204 + ''(import "''${nixos}/../lib").lib.systems.examples.aarch64-multiplatform''; 205 + description = '' 206 + Specifies the platform where the NixOS configuration will run. 207 + 208 + To cross-compile, set also <code>nixpkgs.buildPlatform</code>. 209 + 210 + Ignored when <code>nixpkgs.pkgs</code> is set. 211 + ''; 212 + }; 213 + 214 + buildPlatform = mkOption { 215 + type = types.either types.str types.attrs; # TODO utilize lib.systems.parsedPlatform 216 + default = cfg.hostPlatform; 217 + example = { system = "x86_64-linux"; config = "x86_64-unknown-linux-gnu"; }; 218 + # Make sure that the final value has all fields for sake of other modules 219 + # referring to this. 220 + apply = lib.systems.elaborate; 221 + defaultText = literalExpression 222 + ''config.nixpkgs.hostPlatform''; 223 + description = '' 224 + Specifies the platform on which NixOS should be built. 225 + By default, NixOS is built on the system where it runs, but you can 226 + change where it's built. Setting this option will cause NixOS to be 227 + cross-compiled. 228 + 229 + For instance, if you're doing distributed multi-platform deployment, 230 + or if you're building machines, you can set this to match your 231 + development system and/or build farm. 232 + 233 + Ignored when <code>nixpkgs.pkgs</code> is set. 234 + ''; 235 + }; 236 + 160 237 localSystem = mkOption { 161 238 type = types.attrs; # TODO utilize lib.systems.parsedPlatform 162 239 default = { inherit (cfg) system; }; ··· 176 253 deployment, or when building virtual machines. See its 177 254 description in the Nixpkgs manual for more details. 178 255 179 - Ignored when <code>nixpkgs.pkgs</code> is set. 256 + Ignored when <code>nixpkgs.pkgs</code> or <code>hostPlatform</code> is set. 180 257 ''; 181 258 }; 182 259 260 + # TODO deprecate. "crossSystem" is a nonsense identifier, because "cross" 261 + # is a relation between at least 2 systems in the context of a 262 + # specific build step, not a single system. 183 263 crossSystem = mkOption { 184 264 type = types.nullOr types.attrs; # TODO utilize lib.systems.parsedPlatform 185 265 default = null; ··· 193 273 should be set as null, the default. See its description in the 194 274 Nixpkgs manual for more details. 195 275 196 - Ignored when <code>nixpkgs.pkgs</code> is set. 276 + Ignored when <code>nixpkgs.pkgs</code> or <code>hostPlatform</code> is set. 197 277 ''; 198 278 }; 199 279 ··· 216 296 </programlisting> 217 297 See <code>nixpkgs.localSystem</code> for more information. 218 298 219 - Ignored when <code>nixpkgs.localSystem</code> is set. 220 - Ignored when <code>nixpkgs.pkgs</code> is set. 299 + Ignored when <code>nixpkgs.pkgs</code>, <code>nixpkgs.localSystem</code> or <code>nixpkgs.hostPlatform</code> is set. 221 300 ''; 222 301 }; 223 302 }; ··· 240 319 else "nixpkgs.localSystem"; 241 320 pkgsSystem = finalPkgs.stdenv.targetPlatform.system; 242 321 in { 243 - assertion = nixosExpectedSystem == pkgsSystem; 322 + assertion = !hasPlatform -> nixosExpectedSystem == pkgsSystem; 244 323 message = "The NixOS nixpkgs.pkgs option was set to a Nixpkgs invocation that compiles to target system ${pkgsSystem} but NixOS was configured for system ${nixosExpectedSystem} via NixOS option ${nixosOption}. The NixOS system settings must match the Nixpkgs target system."; 245 324 } 246 325 ) 326 + { 327 + assertion = hasPlatform -> legacyOptionsDefined == []; 328 + message = '' 329 + Your system configures nixpkgs with the platform parameter${optionalString hasBuildPlatform "s"}: 330 + ${hostPlatformLine 331 + }${buildPlatformLine 332 + } 333 + However, it also defines the legacy options: 334 + ${concatMapStrings showOptionWithDefLocs legacyOptionsDefined} 335 + For a future proof system configuration, we recommend to remove 336 + the legacy definitions. 337 + ''; 338 + } 247 339 ]; 248 340 }; 249 341
+58 -3
nixos/modules/misc/nixpkgs/test.nix
··· 1 1 { evalMinimalConfig, pkgs, lib, stdenv }: 2 + let 3 + eval = mod: evalMinimalConfig { 4 + imports = [ ../nixpkgs.nix mod ]; 5 + }; 6 + withHost = eval { 7 + nixpkgs.hostPlatform = "aarch64-linux"; 8 + }; 9 + withHostAndBuild = eval { 10 + nixpkgs.hostPlatform = "aarch64-linux"; 11 + nixpkgs.buildPlatform = "aarch64-darwin"; 12 + }; 13 + ambiguous = { 14 + _file = "ambiguous.nix"; 15 + nixpkgs.hostPlatform = "aarch64-linux"; 16 + nixpkgs.buildPlatform = "aarch64-darwin"; 17 + nixpkgs.system = "x86_64-linux"; 18 + nixpkgs.localSystem.system = "x86_64-darwin"; 19 + nixpkgs.crossSystem.system = "i686-linux"; 20 + imports = [ 21 + { _file = "repeat.nix"; 22 + nixpkgs.hostPlatform = "aarch64-linux"; 23 + } 24 + ]; 25 + }; 26 + getErrors = module: 27 + let 28 + uncheckedEval = lib.evalModules { modules = [ ../nixpkgs.nix module ]; }; 29 + in map (ass: ass.message) (lib.filter (ass: !ass.assertion) uncheckedEval.config.assertions); 30 + in 2 31 lib.recurseIntoAttrs { 3 32 invokeNixpkgsSimple = 4 - (evalMinimalConfig ({ config, modulesPath, ... }: { 5 - imports = [ (modulesPath + "/misc/nixpkgs.nix") ]; 33 + (eval { 6 34 nixpkgs.system = stdenv.hostPlatform.system; 7 - }))._module.args.pkgs.hello; 35 + })._module.args.pkgs.hello; 36 + assertions = 37 + assert withHost._module.args.pkgs.stdenv.hostPlatform.system == "aarch64-linux"; 38 + assert withHost._module.args.pkgs.stdenv.buildPlatform.system == "aarch64-linux"; 39 + assert withHostAndBuild._module.args.pkgs.stdenv.hostPlatform.system == "aarch64-linux"; 40 + assert withHostAndBuild._module.args.pkgs.stdenv.buildPlatform.system == "aarch64-darwin"; 41 + assert builtins.trace (lib.head (getErrors ambiguous)) 42 + getErrors ambiguous == 43 + ['' 44 + Your system configures nixpkgs with the platform parameters: 45 + nixpkgs.hostPlatform, with values defined in: 46 + - repeat.nix 47 + - ambiguous.nix 48 + nixpkgs.buildPlatform, with values defined in: 49 + - ambiguous.nix 50 + 51 + However, it also defines the legacy options: 52 + nixpkgs.system, with values defined in: 53 + - ambiguous.nix 54 + nixpkgs.localSystem, with values defined in: 55 + - ambiguous.nix 56 + nixpkgs.crossSystem, with values defined in: 57 + - ambiguous.nix 58 + 59 + For a future proof system configuration, we recommend to remove 60 + the legacy definitions. 61 + '']; 62 + pkgs.emptyFile; 8 63 }