@@ -70,7 +70,7 @@ let
7070 startScript = cfg:
7171 ''
7272 mkdir -p -m 0755 "$root/etc" "$root/var/lib"
73− mkdir -p -m 0700 "$root/var/lib/private" "$root/root" /run/containers
73+ mkdir -p -m 0700 "$root/var/lib/private" "$root/root"
7474 if ! [ -e "$root/etc/os-release" ]; then
7575 touch "$root/etc/os-release"
7676 fi
@@ -138,7 +138,7 @@ let
138138 --bind-ro=/nix/var/nix/daemon-socket \
139139 --bind="/nix/var/nix/profiles/per-container/$INSTANCE:/nix/var/nix/profiles" \
140140 --bind="/nix/var/nix/gcroots/per-container/$INSTANCE:/nix/var/nix/gcroots" \
141− --link-journal=try-guest \
141+ ${optionalString (!cfg.ephemeral) "--link-journal=try-guest"} \
142142 --setenv PRIVATE_NETWORK="$PRIVATE_NETWORK" \
143143 --setenv HOST_BRIDGE="$HOST_BRIDGE" \
144144 --setenv HOST_ADDRESS="$HOST_ADDRESS" \
@@ -147,6 +147,7 @@ let
147147 --setenv LOCAL_ADDRESS6="$LOCAL_ADDRESS6" \
148148 --setenv HOST_PORT="$HOST_PORT" \
149149 --setenv PATH="$PATH" \
150+ ${optionalString cfg.ephemeral "--ephemeral"} \
150151 ${if cfg.additionalCapabilities != null && cfg.additionalCapabilities != [] then
151152 ''--capability="${concatStringsSep " " cfg.additionalCapabilities}"'' else ""
152153 } \
@@ -247,6 +248,8 @@ let
247248
248249 Type = "notify";
249250
251+ RuntimeDirectory = [ "containers" ] ++ lib.optional cfg.ephemeral "containers/%i";
252+
250253 # Note that on reboot, systemd-nspawn returns 133, so this
251254 # unit will be restarted. On poweroff, it returns 0, so the
252255 # unit won't be restarted.
@@ -419,6 +422,7 @@ let
419422 {
420423 extraVeths = {};
421424 additionalCapabilities = [];
425+ ephemeral = false;
422426 allowedDevices = [];
423427 hostAddress = null;
424428 hostAddress6 = null;
@@ -511,6 +515,26 @@ in
511515 information.
512516 '';
513517 };
518+
519+ ephemeral = mkOption {
520+ type = types.bool;
521+ default = false;
522+ description = ''
523+ Runs container in ephemeral mode with the empty root filesystem at boot.
524+ This way container will be bootstrapped from scratch on each boot
525+ and will be cleaned up on shutdown leaving no traces behind.
526+ Useful for completely stateless, reproducible containers.
527+
528+ Note that this option might require to do some adjustments to the container configuration,
529+ e.g. you might want to set
530+ <varname>systemd.network.networks.$interface.dhcpConfig.ClientIdentifier</varname> to "mac"
531+ if you use <varname>macvlans</varname> option.
532+ This way dhcp client identifier will be stable between the container restarts.
533+
534+ Note that the container journal will not be linked to the host if this option is enabled.
535+ '';
536+ };
537+
514538 enableTun = mkOption {
515539 type = types.bool;
516540 default = false;
@@ -659,13 +683,8 @@ in
659683 unit = {
660684 description = "Container '%i'";
661685
662− unitConfig.RequiresMountsFor = [ "/var/lib/containers/%i" ];
663−
664686 path = [ pkgs.iproute ];
665687
666− environment.INSTANCE = "%i";
667− environment.root = "/var/lib/containers/%i";
668−
669688 preStart = preStartScript dummyConfig;
670689
671690 script = startScript dummyConfig;
@@ -708,6 +727,9 @@ in
708727 script = startScript containerConfig;
709728 postStart = postStartScript containerConfig;
710729 serviceConfig = serviceDirectives containerConfig;
730+ unitConfig.RequiresMountsFor = lib.optional (!containerConfig.ephemeral) "/var/lib/containers/%i";
731+ environment.root = if containerConfig.ephemeral then "/run/containers/%i" else "/var/lib/containers/%i";
732+ environment.INSTANCE = "%i";
711733 } // (
712734 if containerConfig.autoStart then
713735 {