2022-11-21 17:40:18 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
systemBuilder =
|
2023-08-10 07:59:29 +00:00
|
|
|
''
|
2020-04-24 23:36:52 +00:00
|
|
|
mkdir $out
|
|
|
|
|
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
|
|
|
|
|
2022-11-21 17:40:18 +00:00
|
|
|
${config.system.systemBuilderCommands}
|
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
cp "$extraDependenciesPath" "$out/extra-dependencies"
|
2022-12-17 10:02:37 +00:00
|
|
|
|
|
|
|
${optionalString (!config.boot.isContainer && config.boot.bootspec.enable) ''
|
|
|
|
${config.boot.bootspec.writer}
|
2023-04-29 16:46:19 +00:00
|
|
|
${optionalString config.boot.bootspec.enableValidation
|
|
|
|
''${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
|
2023-07-15 17:15:38 +00:00
|
|
|
# makes it bootable. See `activatable-system.nix`.
|
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;
|
2023-05-24 13:37:59 +00:00
|
|
|
passAsFile = [ "extraDependencies" ];
|
2020-04-24 23:36:52 +00:00
|
|
|
buildCommand = systemBuilder;
|
|
|
|
|
|
|
|
systemd = config.systemd.package;
|
|
|
|
|
|
|
|
nixosLabel = config.system.nixos.label;
|
|
|
|
|
2022-12-17 10:02:37 +00:00
|
|
|
inherit (config.system) extraDependencies;
|
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;
|
|
|
|
|
2023-03-15 16:39:30 +00:00
|
|
|
systemWithBuildDeps = system.overrideAttrs (o: {
|
|
|
|
systemBuildClosure = pkgs.closureInfo { rootPaths = [ system.drvPath ]; };
|
|
|
|
buildCommand = o.buildCommand + ''
|
|
|
|
ln -sn $systemBuildClosure $out/build-closure
|
|
|
|
'';
|
|
|
|
});
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
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
|
|
|
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 {
|
2023-07-15 17:15:38 +00:00
|
|
|
type = types.listOf types.pathInStore;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = [];
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2023-07-15 17:15:38 +00:00
|
|
|
A list of paths that should be included in the system
|
2023-05-24 13:37:59 +00:00
|
|
|
closure but generally not visible to users.
|
|
|
|
|
|
|
|
This option has also been used for build-time checks, but the
|
|
|
|
`system.checks` option is more appropriate for that purpose as checks
|
|
|
|
should not leave a trace in the built system configuration.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
system.checks = mkOption {
|
|
|
|
type = types.listOf types.package;
|
|
|
|
default = [];
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Packages that are added as dependencies of the system's build, usually
|
|
|
|
for the purpose of validating some part of the configuration.
|
|
|
|
|
|
|
|
Unlike `system.extraDependencies`, these store paths do not
|
|
|
|
become part of the built system configuration.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-03-15 16:39:30 +00:00
|
|
|
system.includeBuildDependencies = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to include the build closure of the whole system in
|
|
|
|
its runtime closure. This can be useful for making changes
|
|
|
|
fully offline, as it includes all sources, patches, and
|
|
|
|
intermediate outputs required to build all the derivations
|
|
|
|
that the system depends on.
|
|
|
|
|
|
|
|
Note that this includes _all_ the derivations, down from the
|
|
|
|
included applications to their sources, the compilers used to
|
|
|
|
build them, and even the bootstrap compiler used to compile
|
|
|
|
the compilers. This increases the size of the system and the
|
|
|
|
time needed to download its dependencies drastically: a
|
|
|
|
minimal configuration with no extra services enabled grows
|
|
|
|
from ~670MiB in size to 13.5GiB, and takes proportionally
|
|
|
|
longer to download.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
config = {
|
2023-03-30 22:05:00 +00:00
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = config.system.copySystemConfiguration -> !lib.inPureEvalMode;
|
|
|
|
message = "system.copySystemConfiguration is not supported with flakes";
|
|
|
|
}
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
'';
|
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
system.systemBuilderArgs = {
|
2023-07-15 17:15:38 +00:00
|
|
|
|
|
|
|
# Legacy environment variables. These were used by the activation script,
|
|
|
|
# but some other script might still depend on them, although unlikely.
|
|
|
|
installBootLoader = config.system.build.installBootLoader;
|
|
|
|
localeArchive = "${config.i18n.glibcLocales}/lib/locale/locale-archive";
|
|
|
|
distroId = config.system.nixos.distroId;
|
|
|
|
perl = pkgs.perl.withPackages (p: with p; [ ConfigIniFiles FileSlurp ]);
|
|
|
|
# End if legacy environment variables
|
|
|
|
|
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
# Not actually used in the builder. `passedChecks` is just here to create
|
|
|
|
# the build dependencies. Checks are similar to build dependencies in the
|
|
|
|
# sense that if they fail, the system build fails. However, checks do not
|
|
|
|
# produce any output of value, so they are not used by the system builder.
|
|
|
|
# In fact, using them runs the risk of accidentally adding unneeded paths
|
|
|
|
# to the system closure, which defeats the purpose of the `system.checks`
|
|
|
|
# option, as opposed to `system.extraDependencies`.
|
|
|
|
passedChecks = concatStringsSep " " config.system.checks;
|
|
|
|
}
|
|
|
|
// lib.optionalAttrs (config.system.forbiddenDependenciesRegex != "") {
|
2022-12-17 10:02:37 +00:00
|
|
|
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; }))
|
|
|
|
]; };
|
|
|
|
};
|
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
|
2023-03-15 16:39:30 +00:00
|
|
|
system.build.toplevel = if config.system.includeBuildDependencies then systemWithBuildDeps else system;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|