Merge pull request #177012 from hercules-ci/nixos-modular-system

`flake.lib.nixosSystem`: Allow `nixpkgs.system` to be set modularly; improve error message
This commit is contained in:
Silvan Mosberger 2022-08-01 15:05:16 +02:00 committed by GitHub
commit fe2c9ae814
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 11 deletions

View file

@ -20,13 +20,20 @@
nixos = import ./nixos/lib { lib = final; }; nixos = import ./nixos/lib { lib = final; };
nixosSystem = args: nixosSystem = args:
import ./nixos/lib/eval-config.nix (args // { import ./nixos/lib/eval-config.nix (
modules = args.modules ++ [ { args // {
system.nixos.versionSuffix = modules = args.modules ++ [{
".${final.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101")}.${self.shortRev or "dirty"}"; system.nixos.versionSuffix =
system.nixos.revision = final.mkIf (self ? rev) self.rev; ".${final.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101")}.${self.shortRev or "dirty"}";
} ]; system.nixos.revision = final.mkIf (self ? rev) self.rev;
}); }];
} // lib.optionalAttrs (! args?system) {
# Allow system to be set modularly in nixpkgs.system.
# We set it to null, to remove the "legacy" entrypoint's
# non-hermetic default.
system = null;
}
);
}); });
checks.x86_64-linux.tarball = jobs.tarball; checks.x86_64-linux.tarball = jobs.tarball;

View file

@ -9,7 +9,9 @@
# expressions are ever made modular at the top level) can just use # expressions are ever made modular at the top level) can just use
# types.submodule instead of using eval-config.nix # types.submodule instead of using eval-config.nix
evalConfigArgs@ evalConfigArgs@
{ # !!! system can be set modularly, would be nice to remove { # !!! system can be set modularly, would be nice to remove,
# however, removing or changing this default is too much
# of a breaking change. To set it modularly, pass `null`.
system ? builtins.currentSystem system ? builtins.currentSystem
, # !!! is this argument needed any more? The pkgs argument can , # !!! is this argument needed any more? The pkgs argument can
# be set modularly anyway. # be set modularly anyway.
@ -48,7 +50,7 @@ let
# this. Since the latter defaults to the former, the former should # this. Since the latter defaults to the former, the former should
# default to the argument. That way this new default could propagate all # default to the argument. That way this new default could propagate all
# they way through, but has the last priority behind everything else. # they way through, but has the last priority behind everything else.
nixpkgs.system = lib.mkDefault system; nixpkgs.system = lib.mkIf (system != null) (lib.mkDefault system);
_module.args.pkgs = lib.mkIf (pkgs_ != null) (lib.mkForce pkgs_); _module.args.pkgs = lib.mkIf (pkgs_ != null) (lib.mkForce pkgs_);
}; };

View file

@ -244,6 +244,14 @@ in
defaultText = literalExpression defaultText = literalExpression
''(import "''${nixos}/../lib").lib.systems.examples.aarch64-multiplatform''; ''(import "''${nixos}/../lib").lib.systems.examples.aarch64-multiplatform'';
description = '' description = ''
Systems with a recently generated <literal>hardware-configuration.nix</literal>
do not need to specify this option, unless cross-compiling, in which case
you should set <emphasis>only</emphasis> <option>nixpkgs.buildPlatform</option>.
If this is somehow not feasible, you may fall back to removing the
<option>nixpkgs.hostPlatform</option> line from the generated config and
use the old options.
Specifies the platform on which NixOS should be built. When Specifies the platform on which NixOS should be built. When
<code>nixpkgs.crossSystem</code> is unset, it also specifies <code>nixpkgs.crossSystem</code> is unset, it also specifies
the platform <emphasis>for</emphasis> which NixOS should be the platform <emphasis>for</emphasis> which NixOS should be
@ -265,6 +273,10 @@ in
default = null; default = null;
example = { system = "aarch64-linux"; config = "aarch64-unknown-linux-gnu"; }; example = { system = "aarch64-linux"; config = "aarch64-unknown-linux-gnu"; };
description = '' description = ''
Systems with a recently generated <literal>hardware-configuration.nix</literal>
may instead specify <emphasis>only</emphasis> <option>nixpkgs.buildPlatform</option>,
or fall back to removing the <option>nixpkgs.hostPlatform</option> line from the generated config.
Specifies the platform for which NixOS should be Specifies the platform for which NixOS should be
built. Specify this only if it is different from built. Specify this only if it is different from
<code>nixpkgs.localSystem</code>, the platform <code>nixpkgs.localSystem</code>, the platform
@ -280,7 +292,29 @@ in
system = mkOption { system = mkOption {
type = types.str; type = types.str;
example = "i686-linux"; example = "i686-linux";
default =
if opt.hostPlatform.isDefined
then
throw ''
Neither ${opt.system} nor any other option in nixpkgs.* is meant
to be read by modules and configurations.
Use pkgs.stdenv.hostPlatform instead.
''
else
throw ''
Neither ${opt.hostPlatform} nor or the legacy option ${opt.system} has been set.
You can set ${opt.hostPlatform} in hardware-configuration.nix by re-running
a recent version of nixos-generate-config.
The option ${opt.system} is still fully supported for NixOS 22.05 interoperability,
but will be deprecated in the future, so we recommend to set ${opt.hostPlatform}.
'';
defaultText = lib.literalMD ''
Traditionally `builtins.currentSystem`, but unset when invoking NixOS through `lib.nixosSystem`.
'';
description = '' description = ''
This option does not need to be specified for NixOS configurations
with a recently generated <literal>hardware-configuration.nix</literal>.
Specifies the Nix platform type on which NixOS should be built. Specifies the Nix platform type on which NixOS should be built.
It is better to specify <code>nixpkgs.localSystem</code> instead. It is better to specify <code>nixpkgs.localSystem</code> instead.
<programlisting> <programlisting>

View file

@ -186,7 +186,7 @@ in
dysnomia.properties = { dysnomia.properties = {
hostname = config.networking.hostName; hostname = config.networking.hostName;
inherit (config.nixpkgs.localSystem) system; inherit (pkgs.stdenv.hostPlatform) system;
supportedTypes = [ supportedTypes = [
"echo" "echo"

View file

@ -132,7 +132,7 @@ let
# If the host is 64-bit and the container is 32-bit, add a # If the host is 64-bit and the container is 32-bit, add a
# --personality flag. # --personality flag.
${optionalString (config.nixpkgs.localSystem.system == "x86_64-linux") '' ${optionalString (pkgs.stdenv.hostPlatform.system == "x86_64-linux") ''
if [ "$(< ''${SYSTEM_PATH:-/nix/var/nix/profiles/per-container/$INSTANCE/system}/system)" = i686-linux ]; then if [ "$(< ''${SYSTEM_PATH:-/nix/var/nix/profiles/per-container/$INSTANCE/system}/system)" = i686-linux ]; then
extraFlags+=" --personality=x86" extraFlags+=" --personality=x86"
fi fi