2022-11-21 17:40:18 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
systemBuilder =
|
|
|
|
let
|
|
|
|
kernelPath = "${config.boot.kernelPackages.kernel}/" +
|
|
|
|
"${config.system.boot.loader.kernelFile}";
|
|
|
|
initrdPath = "${config.system.build.initialRamdisk}/" +
|
|
|
|
"${config.system.boot.loader.initrdFile}";
|
|
|
|
in ''
|
|
|
|
mkdir $out
|
|
|
|
|
|
|
|
# Containers don't have their own kernel or initrd. They boot
|
|
|
|
# directly into stage 2.
|
2022-11-21 17:40:18 +00:00
|
|
|
${optionalString config.boot.kernel.enable ''
|
2020-04-24 23:36:52 +00:00
|
|
|
if [ ! -f ${kernelPath} ]; then
|
|
|
|
echo "The bootloader cannot find the proper kernel image."
|
|
|
|
echo "(Expecting ${kernelPath})"
|
|
|
|
false
|
|
|
|
fi
|
|
|
|
|
|
|
|
ln -s ${kernelPath} $out/kernel
|
|
|
|
ln -s ${config.system.modulesTree} $out/kernel-modules
|
|
|
|
${optionalString (config.hardware.deviceTree.package != null) ''
|
|
|
|
ln -s ${config.hardware.deviceTree.package} $out/dtbs
|
|
|
|
''}
|
|
|
|
|
|
|
|
echo -n "$kernelParams" > $out/kernel-params
|
|
|
|
|
|
|
|
ln -s ${initrdPath} $out/initrd
|
|
|
|
|
|
|
|
ln -s ${config.system.build.initialRamdiskSecretAppender}/bin/append-initrd-secrets $out
|
|
|
|
|
|
|
|
ln -s ${config.hardware.firmware}/lib/firmware $out/firmware
|
|
|
|
''}
|
|
|
|
|
|
|
|
echo "$activationScript" > $out/activate
|
2021-09-18 10:52:07 +00:00
|
|
|
echo "$dryActivationScript" > $out/dry-activate
|
2020-04-24 23:36:52 +00:00
|
|
|
substituteInPlace $out/activate --subst-var out
|
2021-09-18 10:52:07 +00:00
|
|
|
substituteInPlace $out/dry-activate --subst-var out
|
|
|
|
chmod u+x $out/activate $out/dry-activate
|
|
|
|
unset activationScript dryActivationScript
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
${if config.boot.initrd.systemd.enable then ''
|
|
|
|
cp ${config.system.build.bootStage2} $out/prepare-root
|
|
|
|
substituteInPlace $out/prepare-root --subst-var-by systemConfig $out
|
|
|
|
# This must not be a symlink or the abs_path of the grub builder for the tests
|
|
|
|
# will resolve the symlink and we end up with a path that doesn't point to a
|
|
|
|
# system closure.
|
|
|
|
cp "$systemd/lib/systemd/systemd" $out/init
|
|
|
|
'' else ''
|
|
|
|
cp ${config.system.build.bootStage2} $out/init
|
|
|
|
substituteInPlace $out/init --subst-var-by systemConfig $out
|
|
|
|
''}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
ln -s ${config.system.build.etc}/etc $out/etc
|
|
|
|
ln -s ${config.system.path} $out/sw
|
|
|
|
ln -s "$systemd" $out/systemd
|
|
|
|
|
|
|
|
echo -n "systemd ${toString config.systemd.package.interfaceVersion}" > $out/init-interface-version
|
|
|
|
echo -n "$nixosLabel" > $out/nixos-version
|
|
|
|
echo -n "${config.boot.kernelPackages.stdenv.hostPlatform.system}" > $out/system
|
|
|
|
|
|
|
|
mkdir $out/bin
|
|
|
|
export localeArchive="${config.i18n.glibcLocales}/lib/locale/locale-archive"
|
2023-01-20 10:41:00 +00:00
|
|
|
export distroId=${config.system.nixos.distroId};
|
2020-04-24 23:36:52 +00:00
|
|
|
substituteAll ${./switch-to-configuration.pl} $out/bin/switch-to-configuration
|
|
|
|
chmod +x $out/bin/switch-to-configuration
|
2021-12-19 01:06:50 +00:00
|
|
|
${optionalString (pkgs.stdenv.hostPlatform == pkgs.stdenv.buildPlatform) ''
|
|
|
|
if ! output=$($perl/bin/perl -c $out/bin/switch-to-configuration 2>&1); then
|
|
|
|
echo "switch-to-configuration syntax is not valid:"
|
|
|
|
echo "$output"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
''}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-11-21 17:40:18 +00:00
|
|
|
${config.system.systemBuilderCommands}
|
|
|
|
|
2022-12-17 10:02:37 +00:00
|
|
|
echo -n "$extraDependencies" > $out/extra-dependencies
|
|
|
|
|
|
|
|
${optionalString (!config.boot.isContainer && config.boot.bootspec.enable) ''
|
|
|
|
${config.boot.bootspec.writer}
|
2022-12-28 21:21:41 +00:00
|
|
|
${config.boot.bootspec.validator} "$out/${config.boot.bootspec.filename}"
|
2022-12-17 10:02:37 +00:00
|
|
|
''}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
${config.system.extraSystemBuilderCmds}
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Putting it all together. This builds a store path containing
|
|
|
|
# symlinks to the various parts of the built configuration (the
|
|
|
|
# kernel, systemd units, init scripts, etc.) as well as a script
|
|
|
|
# `switch-to-configuration' that activates the configuration and
|
|
|
|
# makes it bootable.
|
2022-11-21 17:40:18 +00:00
|
|
|
baseSystem = pkgs.stdenvNoCC.mkDerivation ({
|
2020-08-20 17:08:02 +00:00
|
|
|
name = "nixos-system-${config.system.name}-${config.system.nixos.label}";
|
2020-04-24 23:36:52 +00:00
|
|
|
preferLocalBuild = true;
|
|
|
|
allowSubstitutes = false;
|
|
|
|
buildCommand = systemBuilder;
|
|
|
|
|
2020-11-24 20:58:05 +00:00
|
|
|
inherit (pkgs) coreutils;
|
2020-04-24 23:36:52 +00:00
|
|
|
systemd = config.systemd.package;
|
|
|
|
shell = "${pkgs.bash}/bin/sh";
|
|
|
|
su = "${pkgs.shadow.su}/bin/su";
|
2020-11-24 20:58:05 +00:00
|
|
|
utillinux = pkgs.util-linux;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
kernelParams = config.boot.kernelParams;
|
2022-01-26 04:04:25 +00:00
|
|
|
installBootLoader = config.system.build.installBootLoader;
|
2020-04-24 23:36:52 +00:00
|
|
|
activationScript = config.system.activationScripts.script;
|
2021-09-18 10:52:07 +00:00
|
|
|
dryActivationScript = config.system.dryActivationScript;
|
2020-04-24 23:36:52 +00:00
|
|
|
nixosLabel = config.system.nixos.label;
|
|
|
|
|
2022-12-17 10:02:37 +00:00
|
|
|
inherit (config.system) extraDependencies;
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
# Needed by switch-to-configuration.
|
2022-08-12 12:06:08 +00:00
|
|
|
perl = pkgs.perl.withPackages (p: with p; [ ConfigIniFiles FileSlurp ]);
|
2022-11-21 17:40:18 +00:00
|
|
|
} // config.system.systemBuilderArgs);
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Handle assertions and warnings
|
|
|
|
|
|
|
|
failedAssertions = map (x: x.message) (filter (x: !x.assertion) config.assertions);
|
|
|
|
|
|
|
|
baseSystemAssertWarn = if failedAssertions != []
|
|
|
|
then throw "\nFailed assertions:\n${concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}"
|
|
|
|
else showWarnings config.warnings baseSystem;
|
|
|
|
|
|
|
|
# Replace runtime dependencies
|
2021-08-05 21:33:18 +00:00
|
|
|
system = foldr ({ oldDependency, newDependency }: drv:
|
2020-04-24 23:36:52 +00:00
|
|
|
pkgs.replaceDependency { inherit oldDependency newDependency drv; }
|
|
|
|
) baseSystemAssertWarn config.system.replaceRuntimeDependencies;
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
imports = [
|
2022-01-26 04:04:25 +00:00
|
|
|
../build.nix
|
2020-04-24 23:36:52 +00:00
|
|
|
(mkRemovedOptionModule [ "nesting" "clone" ] "Use `specialisation.«name» = { inheritParentConfig = true; configuration = { ... }; }` instead.")
|
|
|
|
(mkRemovedOptionModule [ "nesting" "children" ] "Use `specialisation.«name».configuration = { ... }` instead.")
|
|
|
|
];
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
system.boot.loader.id = mkOption {
|
|
|
|
internal = true;
|
|
|
|
default = "";
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Id string of the used bootloader.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
system.boot.loader.kernelFile = mkOption {
|
|
|
|
internal = true;
|
2021-02-05 17:12:51 +00:00
|
|
|
default = pkgs.stdenv.hostPlatform.linux-kernel.target;
|
2021-12-06 16:07:01 +00:00
|
|
|
defaultText = literalExpression "pkgs.stdenv.hostPlatform.linux-kernel.target";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = types.str;
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Name of the kernel file to be passed to the bootloader.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
system.boot.loader.initrdFile = mkOption {
|
|
|
|
internal = true;
|
|
|
|
default = "initrd";
|
|
|
|
type = types.str;
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Name of the initrd file to be passed to the bootloader.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-11-21 17:40:18 +00:00
|
|
|
system.build = {
|
2022-01-26 04:04:25 +00:00
|
|
|
installBootLoader = mkOption {
|
|
|
|
internal = true;
|
|
|
|
# "; true" => make the `$out` argument from switch-to-configuration.pl
|
|
|
|
# go to `true` instead of `echo`, hiding the useless path
|
|
|
|
# from the log.
|
|
|
|
default = "echo 'Warning: do not know how to make this configuration bootable; please enable a boot loader.' 1>&2; true";
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-01-26 04:04:25 +00:00
|
|
|
A program that writes a bootloader installation script to the path passed in the first command line argument.
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
See `nixos/modules/system/activation/switch-to-configuration.pl`.
|
2022-01-26 04:04:25 +00:00
|
|
|
'';
|
|
|
|
type = types.unique {
|
|
|
|
message = ''
|
|
|
|
Only one bootloader can be enabled at a time. This requirement has not
|
|
|
|
been checked until NixOS 22.05. Earlier versions defaulted to the last
|
|
|
|
definition. Change your configuration to enable only one bootloader.
|
|
|
|
'';
|
|
|
|
} (types.either types.str types.package);
|
|
|
|
};
|
|
|
|
|
|
|
|
toplevel = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
readOnly = true;
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-01-26 04:04:25 +00:00
|
|
|
This option contains the store path that typically represents a NixOS system.
|
|
|
|
|
|
|
|
You can read this path in a custom deployment tool for example.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
system.copySystemConfiguration = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
If enabled, copies the NixOS configuration file
|
2022-08-21 13:32:41 +00:00
|
|
|
(usually {file}`/etc/nixos/configuration.nix`)
|
2020-04-24 23:36:52 +00:00
|
|
|
and links it from the resulting system
|
2022-08-21 13:32:41 +00:00
|
|
|
(getting to {file}`/run/current-system/configuration.nix`).
|
2020-04-24 23:36:52 +00:00
|
|
|
Note that only this single file is copied, even if it imports others.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-11-21 17:40:18 +00:00
|
|
|
system.systemBuilderCommands = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
internal = true;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
This code will be added to the builder creating the system store path.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
system.systemBuilderArgs = mkOption {
|
|
|
|
type = types.attrsOf types.unspecified;
|
|
|
|
internal = true;
|
|
|
|
default = {};
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
`lib.mkDerivation` attributes that will be passed to the top level system builder.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-12-17 10:02:37 +00:00
|
|
|
system.forbiddenDependenciesRegex = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = "-dev$";
|
|
|
|
type = types.str;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
A POSIX Extended Regular Expression that matches store paths that
|
|
|
|
should not appear in the system closure, with the exception of {option}`system.extraDependencies`, which is not checked.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
system.extraSystemBuilderCmds = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
internal = true;
|
|
|
|
default = "";
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
This code will be added to the builder creating the system store path.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
system.extraDependencies = mkOption {
|
|
|
|
type = types.listOf types.package;
|
|
|
|
default = [];
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
A list of packages that should be included in the system
|
|
|
|
closure but not otherwise made available to users. This is
|
|
|
|
primarily used by the installation tests.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
system.replaceRuntimeDependencies = mkOption {
|
|
|
|
default = [];
|
2021-10-06 13:57:05 +00:00
|
|
|
example = lib.literalExpression "[ ({ original = pkgs.openssl; replacement = pkgs.callPackage /path/to/openssl { }; }) ]";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = types.listOf (types.submodule (
|
|
|
|
{ ... }: {
|
|
|
|
options.original = mkOption {
|
|
|
|
type = types.package;
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc "The original package to override.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
options.replacement = mkOption {
|
|
|
|
type = types.package;
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc "The replacement package.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
apply = map ({ original, replacement, ... }: {
|
|
|
|
oldDependency = original;
|
|
|
|
newDependency = replacement;
|
|
|
|
});
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
List of packages to override without doing a full rebuild.
|
|
|
|
The original derivation and replacement derivation must have the same
|
|
|
|
name length, and ideally should have close-to-identical directory layout.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
system.name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default =
|
|
|
|
if config.networking.hostName == ""
|
|
|
|
then "unnamed"
|
|
|
|
else config.networking.hostName;
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression ''
|
|
|
|
if config.networking.hostName == ""
|
|
|
|
then "unnamed"
|
|
|
|
else config.networking.hostName;
|
|
|
|
'';
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
The name of the system used in the {option}`system.build.toplevel` derivation.
|
2022-08-12 12:06:08 +00:00
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
That derivation has the following name:
|
2022-08-21 13:32:41 +00:00
|
|
|
`"nixos-system-''${config.system.name}-''${config.system.nixos.label}"`
|
2020-08-20 17:08:02 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
|
|
|
system.extraSystemBuilderCmds =
|
|
|
|
optionalString
|
|
|
|
config.system.copySystemConfiguration
|
|
|
|
''ln -s '${import ../../../lib/from-env.nix "NIXOS_CONFIG" <nixos-config>}' \
|
|
|
|
"$out/configuration.nix"
|
2022-12-17 10:02:37 +00:00
|
|
|
'' +
|
|
|
|
optionalString
|
|
|
|
(config.system.forbiddenDependenciesRegex != "")
|
|
|
|
''
|
|
|
|
if [[ $forbiddenDependenciesRegex != "" && -n $closureInfo ]]; then
|
|
|
|
if forbiddenPaths="$(grep -E -- "$forbiddenDependenciesRegex" $closureInfo/store-paths)"; then
|
|
|
|
echo -e "System closure $out contains the following disallowed paths:\n$forbiddenPaths"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
2022-12-17 10:02:37 +00:00
|
|
|
system.systemBuilderArgs = lib.optionalAttrs (config.system.forbiddenDependenciesRegex != "") {
|
|
|
|
inherit (config.system) forbiddenDependenciesRegex;
|
|
|
|
closureInfo = pkgs.closureInfo { rootPaths = [
|
|
|
|
# override to avoid infinite recursion (and to allow using extraDependencies to add forbidden dependencies)
|
|
|
|
(config.system.build.toplevel.overrideAttrs (_: { extraDependencies = []; closureInfo = null; }))
|
|
|
|
]; };
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
system.build.toplevel = system;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|