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.";
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.";
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
bindMounts = mkOption {
@ -167,24 +169,26 @@ in
Bridge = c.networkZone;
};
};
services."systemd-nspawn@${n}" = {
services."systemd-nspawn@${n}" =
let
sysProfile = "${ctrProfiles n}/system";
system = if
config.my.build.isDevVM then
systems."${n}".configuration.config.my.buildAs.container else
c.system;
containerSystem = if
config.my.build.isDevVM then
system else
c.containerSystem;
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 =
let
sysProfile = "${ctrProfiles n}/system";
system = if
config.my.build.isDevVM then
systems."${n}".configuration.config.my.buildAs.container else
c.system;
containerSystem = if
config.my.build.isDevVM then
system else
c.containerSystem;
in
''
mkdir -p -m 0755 \
/nix/var/nix/{profiles,gcroots}/per-container/${n} \
@ -202,6 +206,16 @@ in
touch "$root"/etc/os-release
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";
};
network.networks."80-container-${n}-vb" = {

View File

@ -6,12 +6,16 @@ let
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}";
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";