nixos-images/nix/kexec-installer/module.nix

104 lines
3 KiB
Nix
Raw Normal View History

2024-06-23 15:13:27 +02:00
{
config,
lib,
modulesPath,
pkgs,
...
}:
let
2024-06-23 15:13:27 +02:00
restore-network = pkgs.writers.writePython3 "restore-network" {
flakeIgnore = [ "E501" ];
} ./restore_routes.py;
# does not link with iptables enabled
iprouteStatic = pkgs.pkgsStatic.iproute2.override { iptables = null; };
2024-06-23 15:13:27 +02:00
kexec-tools = pkgs.pkgsStatic.kexec-tools.overrideAttrs (old: {
patches = old.patches ++ [
(pkgs.fetchpatch {
url = "https://marc.info/?l=kexec&m=166636009110699&q=mbox";
hash = "sha256-wi0/Ajy/Ac+7npKEvDsMzgNhEWhOMFeoUWcpgGrmVDc=";
})
];
nativeBuildInputs = (old.nativeBuildInputs or []) ++ [
pkgs.pkgsStatic.buildPackages.autoreconfHook
];
meta = old.meta // {
badPlatforms = [ ]; # allow riscv64
};
});
in
{
2022-09-04 14:57:14 +02:00
imports = [
(modulesPath + "/installer/netboot/netboot-minimal.nix")
../installer.nix
../networkd.nix
../serial.nix
../restore-remote-access.nix
2022-09-04 14:57:14 +02:00
];
2023-05-07 21:06:12 +02:00
options = {
system.kexec-installer.name = lib.mkOption {
type = lib.types.str;
default = "nixos-kexec-installer";
description = ''
The variant of the kexec installer to use.
'';
2022-11-27 19:56:24 +01:00
};
2023-05-07 21:06:12 +02:00
};
2023-05-07 21:06:12 +02:00
config = {
# This is a variant of the upstream kexecScript that also allows embedding
# a ssh key.
2024-06-23 15:13:27 +02:00
system.build.kexecRun =
pkgs.runCommand "kexec-run" { nativeBuildInputs = [ pkgs.buildPackages.shellcheck ]; }
''
install -D -m 0755 ${./kexec-run.sh} $out
2023-05-07 21:06:12 +02:00
2024-06-23 15:13:27 +02:00
sed -i \
-e 's|@init@|${config.system.build.toplevel}/init|' \
-e 's|@kernelParams@|${lib.escapeShellArgs config.boot.kernelParams}|' \
$out
2023-05-07 21:06:12 +02:00
2024-06-23 15:13:27 +02:00
shellcheck $out
'';
2023-05-07 21:06:12 +02:00
system.build.kexecTarball = pkgs.runCommand "kexec-tarball" { } ''
2023-05-07 21:06:12 +02:00
mkdir kexec $out
cp "${config.system.build.netbootRamdisk}/initrd" kexec/initrd
cp "${config.system.build.kernel}/${config.system.boot.loader.kernelFile}" kexec/bzImage
cp "${config.system.build.kexecRun}" kexec/run
2024-06-23 15:13:27 +02:00
cp "${kexec-tools}/bin/kexec" kexec/kexec
2023-05-07 21:06:12 +02:00
cp "${iprouteStatic}/bin/ip" kexec/ip
${lib.optionalString (pkgs.hostPlatform == pkgs.buildPlatform) ''
kexec/ip -V
kexec/kexec --version
''}
2023-05-07 21:06:12 +02:00
tar -czvf $out/${config.system.kexec-installer.name}-${pkgs.stdenv.hostPlatform.system}.tar.gz kexec
'';
# for detection if we are on kexec
environment.etc.is_kexec.text = "true";
systemd.services.restore-network = {
before = [ "network-pre.target" ];
wants = [ "network-pre.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = [
"${restore-network} /root/network/addrs.json /root/network/routes-v4.json /root/network/routes-v6.json /etc/systemd/network"
];
};
unitConfig.ConditionPathExists = [
"/root/network/addrs.json"
"/root/network/routes-v4.json"
"/root/network/routes-v6.json"
];
};
2023-05-07 21:06:12 +02:00
};
2022-09-04 14:57:14 +02:00
}