From b5b04bb146551531bf5d453fce900ca039ce6483 Mon Sep 17 00:00:00 2001 From: Gwendolyn Kornak Date: Sun, 1 Jun 2025 12:53:30 -0700 Subject: [PATCH] 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. --- nixos/modules/virtualisation/nixos-containers.nix | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/nixos/modules/virtualisation/nixos-containers.nix b/nixos/modules/virtualisation/nixos-containers.nix index e7f3c050bd49..d692da48e30b 100644 --- a/nixos/modules/virtualisation/nixos-containers.nix +++ b/nixos/modules/virtualisation/nixos-containers.nix @@ -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";