nixos/nixos-containers: add flake to container start script

Integrated the flake container setup into the spawn script for systemd-nspawn.
The trickiest part of this was ensuring the underlying per-container is built.
With the .conf file created, running `nixos-container update` creates all the necessary per-container structure.
We call this command at start to ensure the structure is created only if the per-container system isnt there.
Note: This also means the flake gets updated to branch HEAD when the container is started for the first time.
This commit is contained in:
Gwendolyn Kornak 2025-06-01 12:53:30 -07:00 committed by Jade Lovelace
parent 9a3173dacd
commit b5b04bb146

View File

@ -113,6 +113,11 @@ let
cp --remove-destination /etc/resolv.conf "$root/etc/resolv.conf"
if [ -n "$FLAKE" ] && [ ! -e "/nix/var/nix/profiles/per-container/$INSTANCE/system" ]; then
# we create the etc/nixos-container config file, then if we utilize the update function, we can then build all the necessary system files for the container
${lib.getExe nixos-container} update "$INSTANCE"
fi
declare -a extraFlags
if [ "$PRIVATE_NETWORK" = 1 ]; then
@ -944,7 +949,10 @@ in
unitConfig.RequiresMountsFor = "${stateDirectory}/%i";
path = [ pkgs.iproute2 ];
path = [
pkgs.iproute2
config.nix.package
];
environment = {
root = "${stateDirectory}/%i";