@ -4,6 +4,11 @@ with lib;
let
configurationPrefix = optionalString ( versionAtLeast config . system . stateVersion " 2 2 . 0 5 " ) " n i x o s - " ;
configurationDirectoryName = " ${ configurationPrefix } c o n t a i n e r s " ;
configurationDirectory = " / e t c / ${ configurationDirectoryName } " ;
stateDirectory = " / v a r / l i b / ${ configurationPrefix } c o n t a i n e r s " ;
# The container's init script, a small wrapper around the regular
# NixOS stage-2 init script.
containerInit = ( cfg :
@ -77,7 +82,7 @@ let
startScript = cfg :
''
mkdir - p - m 0755 " $ r o o t / e t c " " $ r o o t / v a r / l i b "
mkdir - p - m 0700 " $ r o o t / v a r / l i b / p r i v a t e " " $ r o o t / r o o t " /run/containers
mkdir - p - m 0700 " $ r o o t / v a r / l i b / p r i v a t e " " $ r o o t / r o o t " /run/nixos- containers
if ! [ - e " $ r o o t / e t c / o s - r e l e a s e " ] ; then
touch " $ r o o t / e t c / o s - r e l e a s e "
fi
@ -249,11 +254,11 @@ let
SyslogIdentifier = " c o n t a i n e r % i " ;
EnvironmentFile = " - / e t c / c o n t a i n e r s / % i . c o n f " ;
EnvironmentFile = " - ${ configurationDirectory } / % i . c o n f " ;
Type = " n o t i f y " ;
RuntimeDirectory = lib . optional cfg . ephemeral " c o n t a i n e r s / % i " ;
RuntimeDirectory = lib . optional cfg . ephemeral " ${ configurationDirectoryName } / % i " ;
# Note that on reboot, systemd-nspawn returns 133, so this
# unit will be restarted. On poweroff, it returns 0, so the
@ -740,12 +745,12 @@ in
unit = {
description = " C o n t a i n e r ' % i ' " ;
unitConfig . RequiresMountsFor = " / v a r / l i b / c o n t a i n e r s / % i " ;
unitConfig . RequiresMountsFor = " ${ stateDirectory } / % i " ;
path = [ pkgs . iproute2 ] ;
environment = {
root = " / v a r / l i b / c o n t a i n e r s / % i " ;
root = " ${ stateDirectory } / % i " ;
INSTANCE = " % i " ;
} ;
@ -782,8 +787,8 @@ in
script = startScript containerConfig ;
postStart = postStartScript containerConfig ;
serviceConfig = serviceDirectives containerConfig ;
unitConfig . RequiresMountsFor = lib . optional ( ! containerConfig . ephemeral ) " / v a r / l i b / c o n t a i n e r s / % i " ;
environment . root = if containerConfig . ephemeral then " / r u n / c o n t a i n e r s / % i " else " / v a r / l i b / c o n t a i n e r s / % i " ;
unitConfig . RequiresMountsFor = lib . optional ( ! containerConfig . ephemeral ) " ${ stateDirectory } / % i " ;
environment . root = if containerConfig . ephemeral then " / r u n / n i x o s - co n t a i n e r s / % i " else " ${ stateDirectory } / % i " ;
} // (
if containerConfig . autoStart then
{
@ -792,7 +797,7 @@ in
after = [ " n e t w o r k . t a r g e t " ] ;
restartTriggers = [
containerConfig . path
config . environment . etc . " c o n t a i n e r s / ${ name } . c o n f " . source
config . environment . etc . " ${ configurationDirectoryName } / ${ name } . c o n f " . source
] ;
restartIfChanged = true ;
}
@ -800,12 +805,12 @@ in
) ) config . containers )
) ) ;
# Generate a configuration file in /etc/containers for each
# Generate a configuration file in /etc/nixos- containers for each
# container so that container@.target can get the container
# configuration.
environment . etc =
let mkPortStr = p : p . protocol + " : " + ( toString p . hostPort ) + " : " + ( if p . containerPort == null then toString p . hostPort else toString p . containerPort ) ;
in mapAttrs' ( name : cfg : nameValuePair " c o n t a i n e r s / ${ name } . c o n f "
in mapAttrs' ( name : cfg : nameValuePair " ${ configurationDirectoryName } / ${ name } . c o n f "
{ text =
''
SYSTEM_PATH = $ { cfg . path }
@ -854,7 +859,11 @@ in
ENV { INTERFACE } == " v [ e b ] - * " , ENV { NM_UNMANAGED } = " 1 "
'' ;
environment . systemPackages = [ pkgs . nixos-container ] ;
environment . systemPackages = [
( pkgs . nixos-container . override {
inherit stateDirectory configurationDirectory ;
} )
] ;
boot . kernelModules = [
" b r i d g e "