2022-10-18 16:24:24 +00:00
|
|
|
{ config, lib, pkgs, ... }@host:
|
2013-11-27 15:54:20 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2013-11-27 15:54:20 +00:00
|
|
|
|
2014-03-18 09:49:25 +00:00
|
|
|
let
|
|
|
|
|
2020-05-08 14:05:29 +00:00
|
|
|
configurationPrefix = optionalString (versionAtLeast config.system.stateVersion "22.05") "nixos-";
|
|
|
|
configurationDirectoryName = "${configurationPrefix}containers";
|
|
|
|
configurationDirectory = "/etc/${configurationDirectoryName}";
|
|
|
|
stateDirectory = "/var/lib/${configurationPrefix}containers";
|
|
|
|
|
2022-08-04 11:49:00 +00:00
|
|
|
nixos-container = pkgs.nixos-container.override {
|
|
|
|
inherit stateDirectory configurationDirectory;
|
|
|
|
};
|
|
|
|
|
2014-05-07 15:00:46 +00:00
|
|
|
# The container's init script, a small wrapper around the regular
|
|
|
|
# NixOS stage-2 init script.
|
2016-05-06 22:04:28 +00:00
|
|
|
containerInit = (cfg:
|
|
|
|
let
|
|
|
|
renderExtraVeth = (name: cfg:
|
|
|
|
''
|
|
|
|
echo "Bringing ${name} up"
|
|
|
|
ip link set dev ${name} up
|
2016-09-22 10:58:39 +00:00
|
|
|
${optionalString (cfg.localAddress != null) ''
|
2016-05-06 22:04:28 +00:00
|
|
|
echo "Setting ip for ${name}"
|
2016-09-22 10:58:39 +00:00
|
|
|
ip addr add ${cfg.localAddress} dev ${name}
|
2016-05-06 22:04:28 +00:00
|
|
|
''}
|
2016-09-22 10:58:39 +00:00
|
|
|
${optionalString (cfg.localAddress6 != null) ''
|
2016-05-06 22:04:28 +00:00
|
|
|
echo "Setting ip6 for ${name}"
|
2016-09-22 10:58:39 +00:00
|
|
|
ip -6 addr add ${cfg.localAddress6} dev ${name}
|
2016-05-06 22:04:28 +00:00
|
|
|
''}
|
2016-09-22 10:58:39 +00:00
|
|
|
${optionalString (cfg.hostAddress != null) ''
|
2016-05-06 22:04:28 +00:00
|
|
|
echo "Setting route to host for ${name}"
|
2016-09-22 10:58:39 +00:00
|
|
|
ip route add ${cfg.hostAddress} dev ${name}
|
2016-05-06 22:04:28 +00:00
|
|
|
''}
|
2016-09-22 10:58:39 +00:00
|
|
|
${optionalString (cfg.hostAddress6 != null) ''
|
2016-05-06 22:04:28 +00:00
|
|
|
echo "Setting route6 to host for ${name}"
|
2016-09-22 10:58:39 +00:00
|
|
|
ip -6 route add ${cfg.hostAddress6} dev ${name}
|
2016-05-06 22:04:28 +00:00
|
|
|
''}
|
|
|
|
''
|
|
|
|
);
|
|
|
|
in
|
|
|
|
pkgs.writeScript "container-init"
|
|
|
|
''
|
2018-03-01 19:38:53 +00:00
|
|
|
#! ${pkgs.runtimeShell} -e
|
2014-05-07 15:00:46 +00:00
|
|
|
|
2021-04-25 17:36:51 +00:00
|
|
|
# Exit early if we're asked to shut down.
|
|
|
|
trap "exit 0" SIGRTMIN+3
|
|
|
|
|
2016-05-06 22:04:28 +00:00
|
|
|
# Initialise the container side of the veth pair.
|
2019-03-19 10:01:57 +00:00
|
|
|
if [ -n "$HOST_ADDRESS" ] || [ -n "$HOST_ADDRESS6" ] ||
|
|
|
|
[ -n "$LOCAL_ADDRESS" ] || [ -n "$LOCAL_ADDRESS6" ] ||
|
|
|
|
[ -n "$HOST_BRIDGE" ]; then
|
2016-05-06 22:04:28 +00:00
|
|
|
ip link set host0 name eth0
|
|
|
|
ip link set dev eth0 up
|
2016-01-30 22:00:39 +00:00
|
|
|
|
2016-05-06 22:04:28 +00:00
|
|
|
if [ -n "$LOCAL_ADDRESS" ]; then
|
|
|
|
ip addr add $LOCAL_ADDRESS dev eth0
|
|
|
|
fi
|
|
|
|
if [ -n "$LOCAL_ADDRESS6" ]; then
|
|
|
|
ip -6 addr add $LOCAL_ADDRESS6 dev eth0
|
|
|
|
fi
|
|
|
|
if [ -n "$HOST_ADDRESS" ]; then
|
|
|
|
ip route add $HOST_ADDRESS dev eth0
|
|
|
|
ip route add default via $HOST_ADDRESS
|
|
|
|
fi
|
|
|
|
if [ -n "$HOST_ADDRESS6" ]; then
|
|
|
|
ip -6 route add $HOST_ADDRESS6 dev eth0
|
|
|
|
ip -6 route add default via $HOST_ADDRESS6
|
|
|
|
fi
|
2016-01-30 22:00:39 +00:00
|
|
|
fi
|
2016-05-06 22:04:28 +00:00
|
|
|
|
2020-12-15 01:40:12 +00:00
|
|
|
${concatStringsSep "\n" (mapAttrsToList renderExtraVeth cfg.extraVeths)}
|
|
|
|
|
2021-04-25 17:36:51 +00:00
|
|
|
# Start the regular stage 2 script.
|
|
|
|
# We source instead of exec to not lose an early stop signal, which is
|
|
|
|
# also the only _reliable_ shutdown signal we have since early stop
|
|
|
|
# does not execute ExecStop* commands.
|
|
|
|
set +e
|
|
|
|
. "$1"
|
2016-05-06 22:04:28 +00:00
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
nspawnExtraVethArgs = (name: cfg: "--network-veth-extra=${name}");
|
2016-09-22 10:58:39 +00:00
|
|
|
|
|
|
|
startScript = cfg:
|
2016-05-06 22:04:28 +00:00
|
|
|
''
|
|
|
|
mkdir -p -m 0755 "$root/etc" "$root/var/lib"
|
2020-05-08 14:05:29 +00:00
|
|
|
mkdir -p -m 0700 "$root/var/lib/private" "$root/root" /run/nixos-containers
|
2016-05-06 22:04:28 +00:00
|
|
|
if ! [ -e "$root/etc/os-release" ]; then
|
|
|
|
touch "$root/etc/os-release"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! [ -e "$root/etc/machine-id" ]; then
|
|
|
|
touch "$root/etc/machine-id"
|
|
|
|
fi
|
|
|
|
|
|
|
|
mkdir -p -m 0755 \
|
|
|
|
"/nix/var/nix/profiles/per-container/$INSTANCE" \
|
|
|
|
"/nix/var/nix/gcroots/per-container/$INSTANCE"
|
|
|
|
|
|
|
|
cp --remove-destination /etc/resolv.conf "$root/etc/resolv.conf"
|
|
|
|
|
|
|
|
if [ "$PRIVATE_NETWORK" = 1 ]; then
|
2018-10-29 11:26:18 +00:00
|
|
|
extraFlags+=" --private-network"
|
|
|
|
fi
|
|
|
|
|
2019-03-19 10:01:57 +00:00
|
|
|
if [ -n "$HOST_ADDRESS" ] || [ -n "$LOCAL_ADDRESS" ] ||
|
|
|
|
[ -n "$HOST_ADDRESS6" ] || [ -n "$LOCAL_ADDRESS6" ]; then
|
2016-05-06 22:04:28 +00:00
|
|
|
extraFlags+=" --network-veth"
|
2019-01-04 21:24:13 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$HOST_PORT" ]; then
|
|
|
|
OIFS=$IFS
|
|
|
|
IFS=","
|
|
|
|
for i in $HOST_PORT
|
|
|
|
do
|
|
|
|
extraFlags+=" --port=$i"
|
|
|
|
done
|
|
|
|
IFS=$OIFS
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$HOST_BRIDGE" ]; then
|
|
|
|
extraFlags+=" --network-bridge=$HOST_BRIDGE"
|
2016-05-06 22:04:28 +00:00
|
|
|
fi
|
|
|
|
|
2016-09-22 10:58:39 +00:00
|
|
|
extraFlags+=" ${concatStringsSep " " (mapAttrsToList nspawnExtraVethArgs cfg.extraVeths)}"
|
2016-05-06 22:04:28 +00:00
|
|
|
|
|
|
|
for iface in $INTERFACES; do
|
|
|
|
extraFlags+=" --network-interface=$iface"
|
|
|
|
done
|
|
|
|
|
|
|
|
for iface in $MACVLANS; do
|
|
|
|
extraFlags+=" --network-macvlan=$iface"
|
|
|
|
done
|
|
|
|
|
|
|
|
# If the host is 64-bit and the container is 32-bit, add a
|
|
|
|
# --personality flag.
|
2022-07-10 11:18:31 +00:00
|
|
|
${optionalString (pkgs.stdenv.hostPlatform.system == "x86_64-linux") ''
|
2016-05-06 22:04:28 +00:00
|
|
|
if [ "$(< ''${SYSTEM_PATH:-/nix/var/nix/profiles/per-container/$INSTANCE/system}/system)" = i686-linux ]; then
|
|
|
|
extraFlags+=" --personality=x86"
|
2014-05-07 15:00:46 +00:00
|
|
|
fi
|
2016-05-06 22:04:28 +00:00
|
|
|
''}
|
|
|
|
|
2022-10-29 20:22:57 +00:00
|
|
|
export SYSTEMD_NSPAWN_UNIFIED_HIERARCHY=1
|
|
|
|
|
2016-05-06 22:04:28 +00:00
|
|
|
# Run systemd-nspawn without startup notification (we'll
|
2021-04-25 17:36:51 +00:00
|
|
|
# wait for the container systemd to signal readiness)
|
|
|
|
# Kill signal handling means systemd-nspawn will pass a system-halt signal
|
|
|
|
# to the container systemd when it receives SIGTERM for container shutdown;
|
|
|
|
# containerInit and stage2 have to handle this as well.
|
2016-05-06 22:04:28 +00:00
|
|
|
exec ${config.systemd.package}/bin/systemd-nspawn \
|
|
|
|
--keep-unit \
|
|
|
|
-M "$INSTANCE" -D "$root" $extraFlags \
|
|
|
|
$EXTRA_NSPAWN_FLAGS \
|
|
|
|
--notify-ready=yes \
|
2021-04-25 17:36:51 +00:00
|
|
|
--kill-signal=SIGRTMIN+3 \
|
2016-05-06 22:04:28 +00:00
|
|
|
--bind-ro=/nix/store \
|
|
|
|
--bind-ro=/nix/var/nix/db \
|
|
|
|
--bind-ro=/nix/var/nix/daemon-socket \
|
|
|
|
--bind="/nix/var/nix/profiles/per-container/$INSTANCE:/nix/var/nix/profiles" \
|
|
|
|
--bind="/nix/var/nix/gcroots/per-container/$INSTANCE:/nix/var/nix/gcroots" \
|
2019-08-18 19:37:38 +00:00
|
|
|
${optionalString (!cfg.ephemeral) "--link-journal=try-guest"} \
|
2016-05-06 22:04:28 +00:00
|
|
|
--setenv PRIVATE_NETWORK="$PRIVATE_NETWORK" \
|
|
|
|
--setenv HOST_BRIDGE="$HOST_BRIDGE" \
|
|
|
|
--setenv HOST_ADDRESS="$HOST_ADDRESS" \
|
|
|
|
--setenv LOCAL_ADDRESS="$LOCAL_ADDRESS" \
|
|
|
|
--setenv HOST_ADDRESS6="$HOST_ADDRESS6" \
|
|
|
|
--setenv LOCAL_ADDRESS6="$LOCAL_ADDRESS6" \
|
2016-12-02 21:49:38 +00:00
|
|
|
--setenv HOST_PORT="$HOST_PORT" \
|
2016-05-06 22:04:28 +00:00
|
|
|
--setenv PATH="$PATH" \
|
2019-08-18 19:37:38 +00:00
|
|
|
${optionalString cfg.ephemeral "--ephemeral"} \
|
2023-03-19 20:44:31 +00:00
|
|
|
${optionalString (cfg.additionalCapabilities != null && cfg.additionalCapabilities != [])
|
|
|
|
''--capability="${concatStringsSep "," cfg.additionalCapabilities}"''
|
2016-09-25 15:33:01 +00:00
|
|
|
} \
|
2023-03-19 20:44:31 +00:00
|
|
|
${optionalString (cfg.tmpfs != null && cfg.tmpfs != [])
|
|
|
|
''--tmpfs=${concatStringsSep " --tmpfs=" cfg.tmpfs}''
|
2016-11-22 01:11:33 +00:00
|
|
|
} \
|
2016-05-06 22:04:28 +00:00
|
|
|
${containerInit cfg} "''${SYSTEM_PATH:-/nix/var/nix/profiles/system}/init"
|
2016-09-22 10:58:39 +00:00
|
|
|
'';
|
2016-05-06 22:04:28 +00:00
|
|
|
|
2016-09-22 10:58:39 +00:00
|
|
|
preStartScript = cfg:
|
2016-05-06 22:04:28 +00:00
|
|
|
''
|
|
|
|
# Clean up existing machined registration and interfaces.
|
|
|
|
machinectl terminate "$INSTANCE" 2> /dev/null || true
|
|
|
|
|
2019-03-19 10:01:57 +00:00
|
|
|
if [ -n "$HOST_ADDRESS" ] || [ -n "$LOCAL_ADDRESS" ] ||
|
|
|
|
[ -n "$HOST_ADDRESS6" ] || [ -n "$LOCAL_ADDRESS6" ]; then
|
2016-05-06 22:04:28 +00:00
|
|
|
ip link del dev "ve-$INSTANCE" 2> /dev/null || true
|
|
|
|
ip link del dev "vb-$INSTANCE" 2> /dev/null || true
|
2014-05-07 15:00:46 +00:00
|
|
|
fi
|
|
|
|
|
2016-05-06 22:04:28 +00:00
|
|
|
${concatStringsSep "\n" (
|
|
|
|
mapAttrsToList (name: cfg:
|
2021-01-24 09:19:10 +00:00
|
|
|
"ip link del dev ${name} 2> /dev/null || true "
|
2016-09-22 10:58:39 +00:00
|
|
|
) cfg.extraVeths
|
2016-05-06 22:04:28 +00:00
|
|
|
)}
|
2016-09-22 10:58:39 +00:00
|
|
|
'';
|
|
|
|
|
2016-05-06 22:04:28 +00:00
|
|
|
postStartScript = (cfg:
|
|
|
|
let
|
2016-09-22 10:58:39 +00:00
|
|
|
ipcall = cfg: ipcmd: variable: attribute:
|
|
|
|
if cfg.${attribute} == null then
|
2016-05-06 22:04:28 +00:00
|
|
|
''
|
|
|
|
if [ -n "${variable}" ]; then
|
|
|
|
${ipcmd} add ${variable} dev $ifaceHost
|
|
|
|
fi
|
|
|
|
''
|
|
|
|
else
|
2021-01-24 09:19:10 +00:00
|
|
|
"${ipcmd} add ${cfg.${attribute}} dev $ifaceHost";
|
2016-09-22 10:58:39 +00:00
|
|
|
renderExtraVeth = name: cfg:
|
|
|
|
if cfg.hostBridge != null then
|
2016-05-06 22:04:28 +00:00
|
|
|
''
|
|
|
|
# Add ${name} to bridge ${cfg.hostBridge}
|
|
|
|
ip link set dev ${name} master ${cfg.hostBridge} up
|
|
|
|
''
|
|
|
|
else
|
|
|
|
''
|
2019-01-11 18:50:15 +00:00
|
|
|
echo "Bring ${name} up"
|
|
|
|
ip link set dev ${name} up
|
2016-09-22 10:58:39 +00:00
|
|
|
# Set IPs and routes for ${name}
|
|
|
|
${optionalString (cfg.hostAddress != null) ''
|
|
|
|
ip addr add ${cfg.hostAddress} dev ${name}
|
|
|
|
''}
|
|
|
|
${optionalString (cfg.hostAddress6 != null) ''
|
|
|
|
ip -6 addr add ${cfg.hostAddress6} dev ${name}
|
|
|
|
''}
|
|
|
|
${optionalString (cfg.localAddress != null) ''
|
|
|
|
ip route add ${cfg.localAddress} dev ${name}
|
|
|
|
''}
|
|
|
|
${optionalString (cfg.localAddress6 != null) ''
|
|
|
|
ip -6 route add ${cfg.localAddress6} dev ${name}
|
|
|
|
''}
|
|
|
|
'';
|
2016-05-06 22:04:28 +00:00
|
|
|
in
|
|
|
|
''
|
2019-03-19 10:01:57 +00:00
|
|
|
if [ -n "$HOST_ADDRESS" ] || [ -n "$LOCAL_ADDRESS" ] ||
|
|
|
|
[ -n "$HOST_ADDRESS6" ] || [ -n "$LOCAL_ADDRESS6" ]; then
|
2016-05-06 22:04:28 +00:00
|
|
|
if [ -z "$HOST_BRIDGE" ]; then
|
|
|
|
ifaceHost=ve-$INSTANCE
|
|
|
|
ip link set dev $ifaceHost up
|
|
|
|
|
|
|
|
${ipcall cfg "ip addr" "$HOST_ADDRESS" "hostAddress"}
|
|
|
|
${ipcall cfg "ip -6 addr" "$HOST_ADDRESS6" "hostAddress6"}
|
|
|
|
${ipcall cfg "ip route" "$LOCAL_ADDRESS" "localAddress"}
|
|
|
|
${ipcall cfg "ip -6 route" "$LOCAL_ADDRESS6" "localAddress6"}
|
|
|
|
fi
|
|
|
|
fi
|
2020-12-15 01:40:12 +00:00
|
|
|
${concatStringsSep "\n" (mapAttrsToList renderExtraVeth cfg.extraVeths)}
|
2016-05-06 22:04:28 +00:00
|
|
|
''
|
|
|
|
);
|
2014-05-07 15:00:46 +00:00
|
|
|
|
2016-09-25 15:33:01 +00:00
|
|
|
serviceDirectives = cfg: {
|
|
|
|
ExecReload = pkgs.writeScript "reload-container"
|
|
|
|
''
|
2018-03-01 19:38:53 +00:00
|
|
|
#! ${pkgs.runtimeShell} -e
|
2022-08-04 11:49:00 +00:00
|
|
|
${nixos-container}/bin/nixos-container run "$INSTANCE" -- \
|
2016-09-25 15:33:01 +00:00
|
|
|
bash --login -c "''${SYSTEM_PATH:-/nix/var/nix/profiles/system}/bin/switch-to-configuration test"
|
|
|
|
'';
|
|
|
|
|
|
|
|
SyslogIdentifier = "container %i";
|
|
|
|
|
2020-05-08 14:05:29 +00:00
|
|
|
EnvironmentFile = "-${configurationDirectory}/%i.conf";
|
2016-09-25 15:33:01 +00:00
|
|
|
|
|
|
|
Type = "notify";
|
|
|
|
|
2020-05-08 14:05:29 +00:00
|
|
|
RuntimeDirectory = lib.optional cfg.ephemeral "${configurationDirectoryName}/%i";
|
2019-08-18 19:37:38 +00:00
|
|
|
|
2016-09-25 15:33:01 +00:00
|
|
|
# Note that on reboot, systemd-nspawn returns 133, so this
|
|
|
|
# unit will be restarted. On poweroff, it returns 0, so the
|
|
|
|
# unit won't be restarted.
|
|
|
|
RestartForceExitStatus = "133";
|
|
|
|
SuccessExitStatus = "133";
|
|
|
|
|
2019-07-31 14:19:18 +00:00
|
|
|
# Some containers take long to start
|
|
|
|
# especially when you automatically start many at once
|
|
|
|
TimeoutStartSec = cfg.timeoutStartSec;
|
|
|
|
|
2016-09-25 15:33:01 +00:00
|
|
|
Restart = "on-failure";
|
|
|
|
|
nixos/containers: Introduce several tweaks to systemd-nspawn from upstream systemd
* Lets container@.service be activated by machines.target instead of
multi-user.target
According to the systemd manpages, all containers that are registered
by machinectl, should be inside machines.target for easy stopping
and starting container units altogether
* make sure container@.service and container.slice instances are
actually located in machine.slice
https://plus.google.com/112206451048767236518/posts/SYAueyXHeEX
See original commit: https://github.com/NixOS/systemd/commit/45d383a3b8
* Enable Cgroup delegation for nixos-containers
Delegate=yes should be set for container scopes where a systemd instance
inside the container shall manage the hierarchies below its own cgroup
and have access to all controllers.
This is equivalent to enabling all accounting options on the systemd
process inside the system container. This means that systemd inside
the container is responsible for managing Cgroup resources for
unit files that enable accounting options inside. Without this
option, units that make use of cgroup features within system
containers might misbehave
See original commit: https://github.com/NixOS/systemd/commit/a931ad47a8
from the manpage:
Turns on delegation of further resource control partitioning to
processes of the unit. Units where this is enabled may create and
manage their own private subhierarchy of control groups below the
control group of the unit itself. For unprivileged services (i.e.
those using the User= setting) the unit's control group will be made
accessible to the relevant user. When enabled the service manager
will refrain from manipulating control groups or moving processes
below the unit's control group, so that a clear concept of ownership
is established: the control group tree above the unit's control
group (i.e. towards the root control group) is owned and managed by
the service manager of the host, while the control group tree below
the unit's control group is owned and managed by the unit itself.
Takes either a boolean argument or a list of control group
controller names. If true, delegation is turned on, and all
supported controllers are enabled for the unit, making them
available to the unit's processes for management. If false,
delegation is turned off entirely (and no additional controllers are
enabled). If set to a list of controllers, delegation is turned on,
and the specified controllers are enabled for the unit. Note that
additional controllers than the ones specified might be made
available as well, depending on configuration of the containing
slice unit or other units contained in it. Note that assigning the
empty string will enable delegation, but reset the list of
controllers, all assignments prior to this will have no effect.
Defaults to false.
Note that controller delegation to less privileged code is only safe
on the unified control group hierarchy. Accordingly, access to the
specified controllers will not be granted to unprivileged services
on the legacy hierarchy, even when requested.
The following controller names may be specified: cpu, cpuacct, io,
blkio, memory, devices, pids. Not all of these controllers are
available on all kernels however, and some are specific to the
unified hierarchy while others are specific to the legacy hierarchy.
Also note that the kernel might support further controllers, which
aren't covered here yet as delegation is either not supported at all
for them or not defined cleanly.
2018-10-21 09:40:20 +00:00
|
|
|
Slice = "machine.slice";
|
|
|
|
Delegate = true;
|
|
|
|
|
2021-04-25 17:36:51 +00:00
|
|
|
# We rely on systemd-nspawn turning a SIGTERM to itself into a shutdown
|
|
|
|
# signal (SIGRTMIN+3) for the inner container.
|
2016-09-25 15:33:01 +00:00
|
|
|
KillMode = "mixed";
|
2021-04-25 17:36:51 +00:00
|
|
|
KillSignal = "TERM";
|
2016-09-25 15:33:01 +00:00
|
|
|
|
|
|
|
DevicePolicy = "closed";
|
|
|
|
DeviceAllow = map (d: "${d.node} ${d.modifier}") cfg.allowedDevices;
|
|
|
|
};
|
|
|
|
|
2021-02-26 16:14:08 +00:00
|
|
|
kernelVersion = config.boot.kernelPackages.kernel.version;
|
2014-04-15 10:58:28 +00:00
|
|
|
|
2018-07-20 20:56:59 +00:00
|
|
|
bindMountOpts = { name, ... }: {
|
2016-01-30 22:00:39 +00:00
|
|
|
|
2015-05-26 11:56:42 +00:00
|
|
|
options = {
|
|
|
|
mountPoint = mkOption {
|
|
|
|
example = "/mnt/usb";
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Mount point on the container file system.";
|
2015-05-26 11:56:42 +00:00
|
|
|
};
|
|
|
|
hostPath = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "/home/alice";
|
2015-09-28 05:48:16 +00:00
|
|
|
type = types.nullOr types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Location of the host path to be mounted.";
|
2015-05-26 11:56:42 +00:00
|
|
|
};
|
|
|
|
isReadOnly = mkOption {
|
2015-05-26 13:41:31 +00:00
|
|
|
default = true;
|
2015-05-26 11:56:42 +00:00
|
|
|
type = types.bool;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Determine whether the mounted path will be accessed in read-only mode.";
|
2015-05-26 11:56:42 +00:00
|
|
|
};
|
|
|
|
};
|
2016-01-30 22:00:39 +00:00
|
|
|
|
2015-05-26 11:56:42 +00:00
|
|
|
config = {
|
|
|
|
mountPoint = mkDefault name;
|
|
|
|
};
|
2016-01-30 22:00:39 +00:00
|
|
|
|
2015-05-26 11:56:42 +00:00
|
|
|
};
|
2016-01-30 22:00:39 +00:00
|
|
|
|
2018-07-20 20:56:59 +00:00
|
|
|
allowedDeviceOpts = { ... }: {
|
2016-09-25 15:33:01 +00:00
|
|
|
options = {
|
|
|
|
node = mkOption {
|
|
|
|
example = "/dev/net/tun";
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Path to device node";
|
2016-09-25 15:33:01 +00:00
|
|
|
};
|
|
|
|
modifier = mkOption {
|
|
|
|
example = "rw";
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2016-09-25 15:33:01 +00:00
|
|
|
Device node access modifier. Takes a combination
|
2022-07-28 21:19:15 +00:00
|
|
|
`r` (read), `w` (write), and
|
|
|
|
`m` (mknod). See the
|
|
|
|
`systemd.resource-control(5)` man page for more
|
2016-09-25 15:33:01 +00:00
|
|
|
information.'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-05-26 11:56:42 +00:00
|
|
|
mkBindFlag = d:
|
|
|
|
let flagPrefix = if d.isReadOnly then " --bind-ro=" else " --bind=";
|
|
|
|
mountstr = if d.hostPath != null then "${d.hostPath}:${d.mountPoint}" else "${d.mountPoint}";
|
|
|
|
in flagPrefix + mountstr ;
|
|
|
|
|
|
|
|
mkBindFlags = bs: concatMapStrings mkBindFlag (lib.attrValues bs);
|
2015-05-25 19:09:53 +00:00
|
|
|
|
2016-05-06 22:04:28 +00:00
|
|
|
networkOptions = {
|
|
|
|
hostBridge = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.nullOr types.str;
|
2016-05-06 22:04:28 +00:00
|
|
|
default = null;
|
|
|
|
example = "br0";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2016-05-06 22:04:28 +00:00
|
|
|
Put the host-side of the veth-pair into the named bridge.
|
|
|
|
Only one of hostAddress* or hostBridge can be given.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-12-04 04:57:24 +00:00
|
|
|
forwardPorts = mkOption {
|
|
|
|
type = types.listOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
protocol = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "tcp";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "The protocol specifier for port forwarding between host and container";
|
2016-12-04 04:57:24 +00:00
|
|
|
};
|
|
|
|
hostPort = mkOption {
|
|
|
|
type = types.int;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Source port of the external interface on host";
|
2016-12-04 04:57:24 +00:00
|
|
|
};
|
|
|
|
containerPort = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Target port of container";
|
2016-12-04 04:57:24 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
default = [];
|
|
|
|
example = [ { protocol = "tcp"; hostPort = 8080; containerPort = 80; } ];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2016-12-18 01:48:29 +00:00
|
|
|
List of forwarded ports from host to container. Each forwarded port
|
|
|
|
is specified by protocol, hostPort and containerPort. By default,
|
|
|
|
protocol is tcp and hostPort and containerPort are assumed to be
|
2018-10-29 11:26:18 +00:00
|
|
|
the same if containerPort is not explicitly given.
|
2016-12-02 21:49:38 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-05-06 22:04:28 +00:00
|
|
|
hostAddress = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "10.231.136.1";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2016-05-06 22:04:28 +00:00
|
|
|
The IPv4 address assigned to the host interface.
|
|
|
|
(Not used when hostBridge is set.)
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
hostAddress6 = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.nullOr types.str;
|
2016-05-06 22:04:28 +00:00
|
|
|
default = null;
|
|
|
|
example = "fc00::1";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2016-05-06 22:04:28 +00:00
|
|
|
The IPv6 address assigned to the host interface.
|
|
|
|
(Not used when hostBridge is set.)
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
localAddress = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "10.231.136.2";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2016-05-06 22:04:28 +00:00
|
|
|
The IPv4 address assigned to the interface in the container.
|
|
|
|
If a hostBridge is used, this should be given with netmask to access
|
|
|
|
the whole network. Otherwise the default netmask is /32 and routing is
|
|
|
|
set up from localAddress to hostAddress and back.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
localAddress6 = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.nullOr types.str;
|
2016-05-06 22:04:28 +00:00
|
|
|
default = null;
|
|
|
|
example = "fc00::2";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2016-05-06 22:04:28 +00:00
|
|
|
The IPv6 address assigned to the interface in the container.
|
|
|
|
If a hostBridge is used, this should be given with netmask to access
|
|
|
|
the whole network. Otherwise the default netmask is /128 and routing is
|
|
|
|
set up from localAddress6 to hostAddress6 and back.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-09-22 10:58:39 +00:00
|
|
|
dummyConfig =
|
|
|
|
{
|
|
|
|
extraVeths = {};
|
2016-09-25 15:33:01 +00:00
|
|
|
additionalCapabilities = [];
|
2019-08-18 19:37:38 +00:00
|
|
|
ephemeral = false;
|
2021-05-08 15:14:50 +00:00
|
|
|
timeoutStartSec = "1min";
|
2016-09-25 15:33:01 +00:00
|
|
|
allowedDevices = [];
|
2016-09-22 10:58:39 +00:00
|
|
|
hostAddress = null;
|
|
|
|
hostAddress6 = null;
|
|
|
|
localAddress = null;
|
|
|
|
localAddress6 = null;
|
2016-11-22 01:11:33 +00:00
|
|
|
tmpfs = null;
|
2016-09-22 10:58:39 +00:00
|
|
|
};
|
|
|
|
|
2014-03-18 09:49:25 +00:00
|
|
|
in
|
|
|
|
|
2013-11-27 15:54:20 +00:00
|
|
|
{
|
|
|
|
options = {
|
|
|
|
|
|
|
|
boot.isContainer = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2013-11-27 15:54:20 +00:00
|
|
|
Whether this NixOS machine is a lightweight container running
|
2021-02-24 13:00:06 +00:00
|
|
|
in another NixOS system.
|
2013-11-27 15:54:20 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-01-24 22:06:00 +00:00
|
|
|
boot.enableContainers = mkOption {
|
|
|
|
type = types.bool;
|
2021-02-24 13:00:06 +00:00
|
|
|
default = true;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-22 03:30:48 +00:00
|
|
|
Whether to enable support for NixOS containers. Defaults to true
|
2021-02-24 13:00:06 +00:00
|
|
|
(at no cost if containers are not actually used).
|
2015-01-24 22:06:00 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-03-18 17:18:35 +00:00
|
|
|
containers = mkOption {
|
2013-11-27 15:54:20 +00:00
|
|
|
type = types.attrsOf (types.submodule (
|
|
|
|
{ config, options, name, ... }:
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
config = mkOption {
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2013-11-27 15:54:20 +00:00
|
|
|
A specification of the desired configuration of this
|
|
|
|
container, as a NixOS module.
|
|
|
|
'';
|
2020-12-15 19:24:59 +00:00
|
|
|
type = lib.mkOptionType {
|
2016-08-29 16:25:50 +00:00
|
|
|
name = "Toplevel NixOS config";
|
2020-12-15 19:25:00 +00:00
|
|
|
merge = loc: defs: (import "${toString config.nixpkgs}/nixos/lib/eval-config.nix" {
|
2016-08-29 16:25:50 +00:00
|
|
|
modules =
|
2019-06-04 23:55:30 +00:00
|
|
|
let
|
2022-10-18 16:24:24 +00:00
|
|
|
extraConfig = { options, ... }: {
|
2019-06-04 23:55:30 +00:00
|
|
|
_file = "module at ${__curPos.file}:${toString __curPos.line}";
|
|
|
|
config = {
|
2022-10-18 16:24:24 +00:00
|
|
|
nixpkgs = if options.nixpkgs?hostPlatform && host.options.nixpkgs.hostPlatform.isDefined
|
|
|
|
then { inherit (host.config.nixpkgs) hostPlatform; }
|
|
|
|
else { inherit (host.config.nixpkgs) localSystem; }
|
|
|
|
;
|
2019-06-04 23:55:30 +00:00
|
|
|
boot.isContainer = true;
|
|
|
|
networking.hostName = mkDefault name;
|
|
|
|
networking.useDHCP = false;
|
|
|
|
assertions = [
|
|
|
|
{
|
2021-02-26 16:14:08 +00:00
|
|
|
assertion =
|
|
|
|
(builtins.compareVersions kernelVersion "5.8" <= 0)
|
|
|
|
-> config.privateNetwork
|
|
|
|
-> stringLength name <= 11;
|
2019-06-04 23:55:30 +00:00
|
|
|
message = ''
|
|
|
|
Container name `${name}` is too long: When `privateNetwork` is enabled, container names can
|
|
|
|
not be longer than 11 characters, because the container's interface name is derived from it.
|
2021-02-26 16:14:08 +00:00
|
|
|
You should either make the container name shorter or upgrade to a more recent kernel that
|
|
|
|
supports interface altnames (i.e. at least Linux 5.8 - please see https://github.com/NixOS/nixpkgs/issues/38509
|
|
|
|
for details).
|
2019-06-04 23:55:30 +00:00
|
|
|
'';
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
2016-08-29 16:25:50 +00:00
|
|
|
};
|
|
|
|
in [ extraConfig ] ++ (map (x: x.value) defs);
|
|
|
|
prefix = [ "containers" name ];
|
2023-02-19 20:25:04 +00:00
|
|
|
inherit (config) specialArgs;
|
2023-05-12 12:56:23 +00:00
|
|
|
|
|
|
|
# The system is inherited from the host above.
|
|
|
|
# Set it to null, to remove the "legacy" entrypoint's non-hermetic default.
|
|
|
|
system = null;
|
2016-08-29 16:25:50 +00:00
|
|
|
}).config;
|
|
|
|
};
|
2013-11-27 15:54:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
path = mkOption {
|
|
|
|
type = types.path;
|
2021-03-29 01:40:44 +00:00
|
|
|
example = "/nix/var/nix/profiles/per-container/webserver";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2013-11-27 15:54:20 +00:00
|
|
|
As an alternative to specifying
|
2022-07-28 21:19:15 +00:00
|
|
|
{option}`config`, you can specify the path to
|
2013-11-27 15:54:20 +00:00
|
|
|
the evaluated NixOS system configuration, typically a
|
|
|
|
symlink to a system profile.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-09-25 15:33:01 +00:00
|
|
|
additionalCapabilities = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "CAP_NET_ADMIN" "CAP_MKNOD" ];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2016-09-25 15:33:01 +00:00
|
|
|
Grant additional capabilities to the container. See the
|
|
|
|
capabilities(7) and systemd-nspawn(1) man pages for more
|
|
|
|
information.
|
|
|
|
'';
|
|
|
|
};
|
2019-08-18 19:37:38 +00:00
|
|
|
|
2020-12-15 19:25:00 +00:00
|
|
|
nixpkgs = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = pkgs.path;
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = literalExpression "pkgs.path";
|
2022-08-15 05:16:25 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-12-15 19:25:00 +00:00
|
|
|
A path to the nixpkgs that provide the modules, pkgs and lib for evaluating the container.
|
|
|
|
|
2022-08-15 05:16:25 +00:00
|
|
|
To only change the `pkgs` argument used inside the container modules,
|
|
|
|
set the `nixpkgs.*` options in the container {option}`config`.
|
|
|
|
Setting `config.nixpkgs.pkgs = pkgs` speeds up the container evaluation
|
|
|
|
by reusing the system pkgs, but the `nixpkgs.config` option in the
|
2020-12-15 19:25:00 +00:00
|
|
|
container config is ignored in this case.
|
2020-04-19 13:41:18 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-02-19 20:25:04 +00:00
|
|
|
specialArgs = mkOption {
|
|
|
|
type = types.attrsOf types.unspecified;
|
|
|
|
default = {};
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
A set of special arguments to be passed to NixOS modules.
|
|
|
|
This will be merged into the `specialArgs` used to evaluate
|
|
|
|
the NixOS configurations.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-08-18 19:37:38 +00:00
|
|
|
ephemeral = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-28 19:18:44 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-08-18 19:37:38 +00:00
|
|
|
Runs container in ephemeral mode with the empty root filesystem at boot.
|
|
|
|
This way container will be bootstrapped from scratch on each boot
|
|
|
|
and will be cleaned up on shutdown leaving no traces behind.
|
|
|
|
Useful for completely stateless, reproducible containers.
|
|
|
|
|
|
|
|
Note that this option might require to do some adjustments to the container configuration,
|
|
|
|
e.g. you might want to set
|
2022-08-28 19:18:44 +00:00
|
|
|
{var}`systemd.network.networks.$interface.dhcpV4Config.ClientIdentifier` to "mac"
|
|
|
|
if you use {var}`macvlans` option.
|
2019-08-18 19:37:38 +00:00
|
|
|
This way dhcp client identifier will be stable between the container restarts.
|
|
|
|
|
|
|
|
Note that the container journal will not be linked to the host if this option is enabled.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-09-25 15:33:01 +00:00
|
|
|
enableTun = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2016-09-25 15:33:01 +00:00
|
|
|
Allows the container to create and setup tunnel interfaces
|
2022-07-28 21:19:15 +00:00
|
|
|
by granting the `NET_ADMIN` capability and
|
|
|
|
enabling access to `/dev/net/tun`.
|
2016-09-25 15:33:01 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-03-18 09:49:25 +00:00
|
|
|
privateNetwork = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-03 20:46:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2014-03-18 09:49:25 +00:00
|
|
|
Whether to give the container its own private virtual
|
|
|
|
Ethernet interface. The interface is called
|
2022-08-03 20:46:41 +00:00
|
|
|
`eth0`, and is hooked up to the interface
|
|
|
|
`ve-«container-name»`
|
2014-03-18 09:49:25 +00:00
|
|
|
on the host. If this option is not set, then the
|
|
|
|
container shares the network interfaces of the host,
|
|
|
|
and can bind to any port on any interface.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-08-22 10:01:09 +00:00
|
|
|
interfaces = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.listOf types.str;
|
2015-08-26 19:11:12 +00:00
|
|
|
default = [];
|
2015-08-22 10:01:09 +00:00
|
|
|
example = [ "eth1" "eth2" ];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2015-08-22 10:01:09 +00:00
|
|
|
The list of interfaces to be moved into the container.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-12-05 23:11:49 +00:00
|
|
|
macvlans = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "eth1" "eth2" ];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2016-12-05 23:11:49 +00:00
|
|
|
The list of host interfaces from which macvlans will be
|
|
|
|
created. For each interface specified, a macvlan interface
|
|
|
|
will be created and moved to the container.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-05-06 22:04:28 +00:00
|
|
|
extraVeths = mkOption {
|
2016-10-09 14:02:14 +00:00
|
|
|
type = with types; attrsOf (submodule { options = networkOptions; });
|
2016-05-06 22:04:28 +00:00
|
|
|
default = {};
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-22 03:30:48 +00:00
|
|
|
Extra veth-pairs to be created for the container.
|
2016-05-06 22:04:28 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-01-12 17:12:33 +00:00
|
|
|
autoStart = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2017-11-01 08:25:26 +00:00
|
|
|
Whether the container is automatically started at boot-time.
|
2015-01-12 17:12:33 +00:00
|
|
|
'';
|
|
|
|
};
|
2015-05-24 16:31:59 +00:00
|
|
|
|
2020-11-22 07:23:53 +00:00
|
|
|
timeoutStartSec = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "1min";
|
2022-08-05 17:39:00 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-11-22 07:23:53 +00:00
|
|
|
Time for the container to start. In case of a timeout,
|
|
|
|
the container processes get killed.
|
2022-08-05 17:39:00 +00:00
|
|
|
See {manpage}`systemd.time(7)`
|
2020-11-22 07:23:53 +00:00
|
|
|
for more information about the format.
|
|
|
|
'';
|
|
|
|
};
|
2019-07-31 14:19:18 +00:00
|
|
|
|
2015-05-26 11:56:42 +00:00
|
|
|
bindMounts = mkOption {
|
2020-08-22 23:28:45 +00:00
|
|
|
type = with types; attrsOf (submodule bindMountOpts);
|
2015-05-26 11:56:42 +00:00
|
|
|
default = {};
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2020-04-02 05:39:04 +00:00
|
|
|
{ "/home" = { hostPath = "/home/alice";
|
|
|
|
isReadOnly = false; };
|
|
|
|
}
|
|
|
|
'';
|
2016-01-30 22:00:39 +00:00
|
|
|
|
2015-05-24 16:31:59 +00:00
|
|
|
description =
|
2022-07-28 21:19:15 +00:00
|
|
|
lib.mdDoc ''
|
2015-05-25 19:09:53 +00:00
|
|
|
An extra list of directories that is bound to the container.
|
2015-05-24 16:31:59 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-09-25 15:33:01 +00:00
|
|
|
allowedDevices = mkOption {
|
2016-10-02 05:07:00 +00:00
|
|
|
type = with types; listOf (submodule allowedDeviceOpts);
|
2016-09-25 15:33:01 +00:00
|
|
|
default = [];
|
|
|
|
example = [ { node = "/dev/net/tun"; modifier = "rw"; } ];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2016-09-25 15:33:01 +00:00
|
|
|
A list of device nodes to which the containers has access to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-11-22 01:11:33 +00:00
|
|
|
tmpfs = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "/var" ];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2016-11-22 01:11:33 +00:00
|
|
|
Mounts a set of tmpfs file systems into the container.
|
|
|
|
Multiple paths can be specified.
|
|
|
|
Valid items must conform to the --tmpfs argument
|
|
|
|
of systemd-nspawn. See systemd-nspawn(1) for details.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-02-25 13:22:23 +00:00
|
|
|
extraFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "--drop-capability=CAP_SYS_CHROOT" ];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-02-25 13:22:23 +00:00
|
|
|
Extra flags passed to the systemd-nspawn command.
|
|
|
|
See systemd-nspawn(1) for details.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-12-15 19:25:00 +00:00
|
|
|
# Removed option. See `checkAssertion` below for the accompanying error message.
|
|
|
|
pkgs = mkOption { visible = false; };
|
2016-05-06 22:04:28 +00:00
|
|
|
} // networkOptions;
|
2013-11-27 15:54:20 +00:00
|
|
|
|
2020-12-15 19:25:00 +00:00
|
|
|
config = let
|
|
|
|
# Throw an error when removed option `pkgs` is used.
|
|
|
|
# Because this is a submodule we cannot use `mkRemovedOptionModule` or option `assertions`.
|
|
|
|
optionPath = "containers.${name}.pkgs";
|
|
|
|
files = showFiles options.pkgs.files;
|
|
|
|
checkAssertion = if options.pkgs.isDefined then throw ''
|
|
|
|
The option definition `${optionPath}' in ${files} no longer has any effect; please remove it.
|
|
|
|
|
|
|
|
Alternatively, you can use the following options:
|
|
|
|
- containers.${name}.nixpkgs
|
|
|
|
This sets the nixpkgs (and thereby the modules, pkgs and lib) that
|
|
|
|
are used for evaluating the container.
|
|
|
|
|
|
|
|
- containers.${name}.config.nixpkgs.pkgs
|
|
|
|
This only sets the `pkgs` argument used inside the container modules.
|
|
|
|
''
|
|
|
|
else null;
|
|
|
|
in {
|
|
|
|
path = builtins.seq checkAssertion
|
|
|
|
mkIf options.config.isDefined config.config.system.build.toplevel;
|
|
|
|
};
|
2013-11-27 15:54:20 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
default = {};
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression
|
2013-11-27 15:54:20 +00:00
|
|
|
''
|
|
|
|
{ webserver =
|
2014-03-19 18:55:05 +00:00
|
|
|
{ path = "/nix/var/nix/profiles/webserver";
|
2013-11-27 15:54:20 +00:00
|
|
|
};
|
|
|
|
database =
|
2014-03-19 18:55:05 +00:00
|
|
|
{ config =
|
2013-11-27 15:54:20 +00:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
{ services.postgresql.enable = true;
|
2022-04-03 15:46:15 +00:00
|
|
|
services.postgresql.package = pkgs.postgresql_14;
|
2018-04-06 01:19:06 +00:00
|
|
|
|
2021-12-03 00:00:48 +00:00
|
|
|
system.stateVersion = "21.05";
|
2013-11-27 15:54:20 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
2022-08-03 20:46:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2013-11-27 15:54:20 +00:00
|
|
|
A set of NixOS system configurations to be run as lightweight
|
|
|
|
containers. Each container appears as a service
|
2022-08-03 20:46:41 +00:00
|
|
|
`container-«name»`
|
2013-11-27 15:54:20 +00:00
|
|
|
on the host system, allowing it to be started and stopped via
|
2022-08-03 20:46:41 +00:00
|
|
|
{command}`systemctl`.
|
2013-11-27 15:54:20 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-04-02 15:03:30 +00:00
|
|
|
config = mkIf (config.boot.enableContainers) (let
|
2013-11-27 15:54:20 +00:00
|
|
|
|
2016-04-02 15:03:30 +00:00
|
|
|
unit = {
|
|
|
|
description = "Container '%i'";
|
2013-11-27 15:54:20 +00:00
|
|
|
|
2020-05-08 14:05:29 +00:00
|
|
|
unitConfig.RequiresMountsFor = "${stateDirectory}/%i";
|
2014-03-18 09:49:25 +00:00
|
|
|
|
2021-03-14 16:05:16 +00:00
|
|
|
path = [ pkgs.iproute2 ];
|
2013-11-27 15:54:20 +00:00
|
|
|
|
2019-08-21 06:41:00 +00:00
|
|
|
environment = {
|
2020-05-08 14:05:29 +00:00
|
|
|
root = "${stateDirectory}/%i";
|
2019-08-21 06:41:00 +00:00
|
|
|
INSTANCE = "%i";
|
|
|
|
};
|
2014-04-01 14:02:53 +00:00
|
|
|
|
2016-09-22 10:58:39 +00:00
|
|
|
preStart = preStartScript dummyConfig;
|
2014-08-12 00:33:30 +00:00
|
|
|
|
2016-09-22 10:58:39 +00:00
|
|
|
script = startScript dummyConfig;
|
2014-03-19 18:55:05 +00:00
|
|
|
|
2016-09-22 10:58:39 +00:00
|
|
|
postStart = postStartScript dummyConfig;
|
2014-03-17 14:03:29 +00:00
|
|
|
|
2016-04-02 15:03:30 +00:00
|
|
|
restartIfChanged = false;
|
2016-01-30 22:00:39 +00:00
|
|
|
|
2016-09-25 15:33:01 +00:00
|
|
|
serviceConfig = serviceDirectives dummyConfig;
|
2016-04-02 15:03:30 +00:00
|
|
|
};
|
|
|
|
in {
|
2022-08-04 19:48:27 +00:00
|
|
|
warnings =
|
|
|
|
(optional (config.virtualisation.containers.enable && versionOlder config.system.stateVersion "22.05") ''
|
|
|
|
Enabling both boot.enableContainers & virtualisation.containers on system.stateVersion < 22.05 is unsupported.
|
|
|
|
'');
|
|
|
|
|
2019-08-13 21:52:01 +00:00
|
|
|
systemd.targets.multi-user.wants = [ "machines.target" ];
|
nixos/containers: Introduce several tweaks to systemd-nspawn from upstream systemd
* Lets container@.service be activated by machines.target instead of
multi-user.target
According to the systemd manpages, all containers that are registered
by machinectl, should be inside machines.target for easy stopping
and starting container units altogether
* make sure container@.service and container.slice instances are
actually located in machine.slice
https://plus.google.com/112206451048767236518/posts/SYAueyXHeEX
See original commit: https://github.com/NixOS/systemd/commit/45d383a3b8
* Enable Cgroup delegation for nixos-containers
Delegate=yes should be set for container scopes where a systemd instance
inside the container shall manage the hierarchies below its own cgroup
and have access to all controllers.
This is equivalent to enabling all accounting options on the systemd
process inside the system container. This means that systemd inside
the container is responsible for managing Cgroup resources for
unit files that enable accounting options inside. Without this
option, units that make use of cgroup features within system
containers might misbehave
See original commit: https://github.com/NixOS/systemd/commit/a931ad47a8
from the manpage:
Turns on delegation of further resource control partitioning to
processes of the unit. Units where this is enabled may create and
manage their own private subhierarchy of control groups below the
control group of the unit itself. For unprivileged services (i.e.
those using the User= setting) the unit's control group will be made
accessible to the relevant user. When enabled the service manager
will refrain from manipulating control groups or moving processes
below the unit's control group, so that a clear concept of ownership
is established: the control group tree above the unit's control
group (i.e. towards the root control group) is owned and managed by
the service manager of the host, while the control group tree below
the unit's control group is owned and managed by the unit itself.
Takes either a boolean argument or a list of control group
controller names. If true, delegation is turned on, and all
supported controllers are enabled for the unit, making them
available to the unit's processes for management. If false,
delegation is turned off entirely (and no additional controllers are
enabled). If set to a list of controllers, delegation is turned on,
and the specified controllers are enabled for the unit. Note that
additional controllers than the ones specified might be made
available as well, depending on configuration of the containing
slice unit or other units contained in it. Note that assigning the
empty string will enable delegation, but reset the list of
controllers, all assignments prior to this will have no effect.
Defaults to false.
Note that controller delegation to less privileged code is only safe
on the unified control group hierarchy. Accordingly, access to the
specified controllers will not be granted to unprivileged services
on the legacy hierarchy, even when requested.
The following controller names may be specified: cpu, cpuacct, io,
blkio, memory, devices, pids. Not all of these controllers are
available on all kernels however, and some are specific to the
unified hierarchy while others are specific to the legacy hierarchy.
Also note that the kernel might support further controllers, which
aren't covered here yet as delegation is either not supported at all
for them or not defined cleanly.
2018-10-21 09:40:20 +00:00
|
|
|
|
2016-04-02 15:03:30 +00:00
|
|
|
systemd.services = listToAttrs (filter (x: x.value != null) (
|
|
|
|
# The generic container template used by imperative containers
|
|
|
|
[{ name = "container@"; value = unit; }]
|
|
|
|
# declarative containers
|
2016-09-25 15:33:01 +00:00
|
|
|
++ (mapAttrsToList (name: cfg: nameValuePair "container@${name}" (let
|
2019-06-06 12:43:46 +00:00
|
|
|
containerConfig = cfg // (
|
2016-09-25 15:33:01 +00:00
|
|
|
if cfg.enableTun then
|
|
|
|
{
|
|
|
|
allowedDevices = cfg.allowedDevices
|
|
|
|
++ [ { node = "/dev/net/tun"; modifier = "rw"; } ];
|
|
|
|
additionalCapabilities = cfg.additionalCapabilities
|
|
|
|
++ [ "CAP_NET_ADMIN" ];
|
|
|
|
}
|
|
|
|
else {});
|
|
|
|
in
|
2019-08-21 06:41:00 +00:00
|
|
|
recursiveUpdate unit {
|
2019-06-06 12:43:46 +00:00
|
|
|
preStart = preStartScript containerConfig;
|
|
|
|
script = startScript containerConfig;
|
|
|
|
postStart = postStartScript containerConfig;
|
|
|
|
serviceConfig = serviceDirectives containerConfig;
|
2020-05-08 14:05:29 +00:00
|
|
|
unitConfig.RequiresMountsFor = lib.optional (!containerConfig.ephemeral) "${stateDirectory}/%i";
|
|
|
|
environment.root = if containerConfig.ephemeral then "/run/nixos-containers/%i" else "${stateDirectory}/%i";
|
2016-09-25 15:33:01 +00:00
|
|
|
} // (
|
2019-06-06 12:43:46 +00:00
|
|
|
if containerConfig.autoStart then
|
2016-09-25 15:33:01 +00:00
|
|
|
{
|
nixos/containers: Introduce several tweaks to systemd-nspawn from upstream systemd
* Lets container@.service be activated by machines.target instead of
multi-user.target
According to the systemd manpages, all containers that are registered
by machinectl, should be inside machines.target for easy stopping
and starting container units altogether
* make sure container@.service and container.slice instances are
actually located in machine.slice
https://plus.google.com/112206451048767236518/posts/SYAueyXHeEX
See original commit: https://github.com/NixOS/systemd/commit/45d383a3b8
* Enable Cgroup delegation for nixos-containers
Delegate=yes should be set for container scopes where a systemd instance
inside the container shall manage the hierarchies below its own cgroup
and have access to all controllers.
This is equivalent to enabling all accounting options on the systemd
process inside the system container. This means that systemd inside
the container is responsible for managing Cgroup resources for
unit files that enable accounting options inside. Without this
option, units that make use of cgroup features within system
containers might misbehave
See original commit: https://github.com/NixOS/systemd/commit/a931ad47a8
from the manpage:
Turns on delegation of further resource control partitioning to
processes of the unit. Units where this is enabled may create and
manage their own private subhierarchy of control groups below the
control group of the unit itself. For unprivileged services (i.e.
those using the User= setting) the unit's control group will be made
accessible to the relevant user. When enabled the service manager
will refrain from manipulating control groups or moving processes
below the unit's control group, so that a clear concept of ownership
is established: the control group tree above the unit's control
group (i.e. towards the root control group) is owned and managed by
the service manager of the host, while the control group tree below
the unit's control group is owned and managed by the unit itself.
Takes either a boolean argument or a list of control group
controller names. If true, delegation is turned on, and all
supported controllers are enabled for the unit, making them
available to the unit's processes for management. If false,
delegation is turned off entirely (and no additional controllers are
enabled). If set to a list of controllers, delegation is turned on,
and the specified controllers are enabled for the unit. Note that
additional controllers than the ones specified might be made
available as well, depending on configuration of the containing
slice unit or other units contained in it. Note that assigning the
empty string will enable delegation, but reset the list of
controllers, all assignments prior to this will have no effect.
Defaults to false.
Note that controller delegation to less privileged code is only safe
on the unified control group hierarchy. Accordingly, access to the
specified controllers will not be granted to unprivileged services
on the legacy hierarchy, even when requested.
The following controller names may be specified: cpu, cpuacct, io,
blkio, memory, devices, pids. Not all of these controllers are
available on all kernels however, and some are specific to the
unified hierarchy while others are specific to the legacy hierarchy.
Also note that the kernel might support further controllers, which
aren't covered here yet as delegation is either not supported at all
for them or not defined cleanly.
2018-10-21 09:40:20 +00:00
|
|
|
wantedBy = [ "machines.target" ];
|
2016-09-25 15:33:01 +00:00
|
|
|
wants = [ "network.target" ];
|
|
|
|
after = [ "network.target" ];
|
2019-06-06 14:19:07 +00:00
|
|
|
restartTriggers = [
|
|
|
|
containerConfig.path
|
2020-05-08 14:05:29 +00:00
|
|
|
config.environment.etc."${configurationDirectoryName}/${name}.conf".source
|
2019-06-06 14:19:07 +00:00
|
|
|
];
|
|
|
|
restartIfChanged = true;
|
2016-09-25 15:33:01 +00:00
|
|
|
}
|
|
|
|
else {})
|
2016-04-02 15:03:30 +00:00
|
|
|
)) config.containers)
|
|
|
|
));
|
2014-03-17 14:03:29 +00:00
|
|
|
|
2020-05-08 14:05:29 +00:00
|
|
|
# Generate a configuration file in /etc/nixos-containers for each
|
2014-03-19 18:55:05 +00:00
|
|
|
# container so that container@.target can get the container
|
|
|
|
# configuration.
|
2016-12-04 04:57:24 +00:00
|
|
|
environment.etc =
|
2018-10-29 11:26:18 +00:00
|
|
|
let mkPortStr = p: p.protocol + ":" + (toString p.hostPort) + ":" + (if p.containerPort == null then toString p.hostPort else toString p.containerPort);
|
2020-05-08 14:05:29 +00:00
|
|
|
in mapAttrs' (name: cfg: nameValuePair "${configurationDirectoryName}/${name}.conf"
|
2014-03-19 18:55:05 +00:00
|
|
|
{ text =
|
|
|
|
''
|
|
|
|
SYSTEM_PATH=${cfg.path}
|
|
|
|
${optionalString cfg.privateNetwork ''
|
|
|
|
PRIVATE_NETWORK=1
|
2016-01-30 22:00:39 +00:00
|
|
|
${optionalString (cfg.hostBridge != null) ''
|
|
|
|
HOST_BRIDGE=${cfg.hostBridge}
|
|
|
|
''}
|
2016-12-04 04:57:24 +00:00
|
|
|
${optionalString (length cfg.forwardPorts > 0) ''
|
|
|
|
HOST_PORT=${concatStringsSep "," (map mkPortStr cfg.forwardPorts)}
|
2016-12-02 21:49:38 +00:00
|
|
|
''}
|
2014-03-19 18:55:05 +00:00
|
|
|
${optionalString (cfg.hostAddress != null) ''
|
|
|
|
HOST_ADDRESS=${cfg.hostAddress}
|
|
|
|
''}
|
2016-01-30 22:00:39 +00:00
|
|
|
${optionalString (cfg.hostAddress6 != null) ''
|
|
|
|
HOST_ADDRESS6=${cfg.hostAddress6}
|
|
|
|
''}
|
2014-03-19 18:55:05 +00:00
|
|
|
${optionalString (cfg.localAddress != null) ''
|
|
|
|
LOCAL_ADDRESS=${cfg.localAddress}
|
|
|
|
''}
|
2016-01-30 22:00:39 +00:00
|
|
|
${optionalString (cfg.localAddress6 != null) ''
|
|
|
|
LOCAL_ADDRESS6=${cfg.localAddress6}
|
|
|
|
''}
|
2014-03-19 18:55:05 +00:00
|
|
|
''}
|
2016-05-06 22:04:28 +00:00
|
|
|
INTERFACES="${toString cfg.interfaces}"
|
2016-12-05 23:11:49 +00:00
|
|
|
MACVLANS="${toString cfg.macvlans}"
|
2016-05-06 22:04:28 +00:00
|
|
|
${optionalString cfg.autoStart ''
|
|
|
|
AUTO_START=1
|
|
|
|
''}
|
2018-02-25 13:22:23 +00:00
|
|
|
EXTRA_NSPAWN_FLAGS="${mkBindFlags cfg.bindMounts +
|
|
|
|
optionalString (cfg.extraFlags != [])
|
|
|
|
(" " + concatStringsSep " " cfg.extraFlags)}"
|
2014-03-19 18:55:05 +00:00
|
|
|
'';
|
2014-03-18 17:18:35 +00:00
|
|
|
}) config.containers;
|
2013-11-27 15:54:20 +00:00
|
|
|
|
2014-03-18 09:49:25 +00:00
|
|
|
# Generate /etc/hosts entries for the containers.
|
|
|
|
networking.extraHosts = concatStrings (mapAttrsToList (name: cfg: optionalString (cfg.localAddress != null)
|
|
|
|
''
|
2016-12-06 22:32:17 +00:00
|
|
|
${head (splitString "/" cfg.localAddress)} ${name}.containers
|
2014-03-18 17:18:35 +00:00
|
|
|
'') config.containers);
|
2014-03-18 09:49:25 +00:00
|
|
|
|
2016-12-02 11:51:00 +00:00
|
|
|
networking.dhcpcd.denyInterfaces = [ "ve-*" "vb-*" ];
|
2014-05-07 15:00:46 +00:00
|
|
|
|
2017-11-21 07:41:41 +00:00
|
|
|
services.udev.extraRules = optionalString config.networking.networkmanager.enable ''
|
|
|
|
# Don't manage interfaces created by nixos-container.
|
2017-12-12 17:35:06 +00:00
|
|
|
ENV{INTERFACE}=="v[eb]-*", ENV{NM_UNMANAGED}="1"
|
2017-11-21 07:41:41 +00:00
|
|
|
'';
|
|
|
|
|
2020-05-08 14:05:29 +00:00
|
|
|
environment.systemPackages = [
|
2022-08-04 11:49:00 +00:00
|
|
|
nixos-container
|
2020-05-08 14:05:29 +00:00
|
|
|
];
|
2019-10-03 07:16:53 +00:00
|
|
|
|
|
|
|
boot.kernelModules = [
|
|
|
|
"bridge"
|
|
|
|
"macvlan"
|
|
|
|
"tap"
|
|
|
|
"tun"
|
|
|
|
];
|
2016-04-02 15:03:30 +00:00
|
|
|
});
|
2014-02-24 17:05:26 +00:00
|
|
|
}
|