nixos/containers: Add hot reload on deploy

This commit is contained in:
Jack O'Sullivan 2022-04-18 15:46:38 +01:00
parent 91e3e55077
commit 6bc27ac4db
2 changed files with 33 additions and 15 deletions

View File

@ -62,6 +62,8 @@ let
system = mkOpt' path "${ctrProfiles name}/system" "Path to NixOS system configuration."; system = mkOpt' path "${ctrProfiles name}/system" "Path to NixOS system configuration.";
containerSystem = mkOpt' path "/nix/var/nix/profiles/system" "Path to NixOS system configuration from within container."; containerSystem = mkOpt' path "/nix/var/nix/profiles/system" "Path to NixOS system configuration from within container.";
autoStart = mkBoolOpt' true "Whether to start the container automatically at boot."; autoStart = mkBoolOpt' true "Whether to start the container automatically at boot.";
hotReload = mkBoolOpt' true
"Whether to apply new configuration by running `switch-to-configuration` instead of rebooting the container.";
# Yoinked from nixos/modules/virtualisation/nixos-containers.nix # Yoinked from nixos/modules/virtualisation/nixos-containers.nix
bindMounts = mkOption { bindMounts = mkOption {
@ -167,13 +169,7 @@ in
Bridge = c.networkZone; Bridge = c.networkZone;
}; };
}; };
services."systemd-nspawn@${n}" = { services."systemd-nspawn@${n}" =
# systemd.nspawn units can't set the root directory directly, but /run/machines/${n} is one of the search paths
environment.root = "/run/machines/${n}";
restartTriggers = [
(''${n}.nspawn:${hashString "sha256" (toJSON config.systemd.nspawn."${n}")}'')
];
preStart =
let let
sysProfile = "${ctrProfiles n}/system"; sysProfile = "${ctrProfiles n}/system";
system = if system = if
@ -185,6 +181,14 @@ in
system else system else
c.containerSystem; c.containerSystem;
in in
{
# systemd.nspawn units can't set the root directory directly, but /run/machines/${n} is one of the search paths
environment.root = "/run/machines/${n}";
restartTriggers = [
(''${n}.nspawn:${hashString "sha256" (toJSON config.systemd.nspawn."${n}")}'')
];
preStart =
'' ''
mkdir -p -m 0755 \ mkdir -p -m 0755 \
/nix/var/nix/{profiles,gcroots}/per-container/${n} \ /nix/var/nix/{profiles,gcroots}/per-container/${n} \
@ -202,6 +206,16 @@ in
touch "$root"/etc/os-release touch "$root"/etc/os-release
ln -sf "${containerSystem}"/init "$root"/sbin/init ln -sf "${containerSystem}"/init "$root"/sbin/init
''; '';
postStop =
''
rm -rf "$root"
'';
reload =
''
[ -e "${system}"/bin/switch-to-configuration ] && \
systemd-run --pipe --machine ${n} -- "${containerSystem}"/bin/switch-to-configuration test
'';
wantedBy = optional c.autoStart "machines.target"; wantedBy = optional c.autoStart "machines.target";
}; };
network.networks."80-container-${n}-vb" = { network.networks."80-container-${n}-vb" = {

View File

@ -6,12 +6,16 @@ let
cfg = config.my.deploy; cfg = config.my.deploy;
ctrProfiles = optionalAttrs cfg.generate.containers.enable (mapAttrs' (n: c: { ctrProfiles = optionalAttrs cfg.generate.containers.enable (mapAttrs' (n: c:
let
ctrConfig = systems."${n}".configuration.config;
in
{
name = "container-${n}"; name = "container-${n}";
value = { value = {
path = pkgs.deploy-rs.lib.activate.custom systems."${n}".configuration.config.my.buildAs.container path = pkgs.deploy-rs.lib.activate.custom ctrConfig.my.buildAs.container
'' ''
systemctl restart systemd-nspawn@${n} systemctl ${if c.hotReload then "reload" else "restart"} systemd-nspawn@${n}
''; '';
profilePath = "/nix/var/nix/profiles/per-container/${n}/system"; profilePath = "/nix/var/nix/profiles/per-container/${n}/system";