2013-11-27 15:54:20 +00:00
|
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
with pkgs.lib;
|
|
|
|
|
|
2014-03-18 09:49:25 +00:00
|
|
|
|
let
|
|
|
|
|
|
|
|
|
|
runInNetns = pkgs.stdenv.mkDerivation {
|
|
|
|
|
name = "run-in-netns";
|
|
|
|
|
unpackPhase = "true";
|
|
|
|
|
buildPhase = ''
|
|
|
|
|
mkdir -p $out/bin
|
|
|
|
|
gcc ${./run-in-netns.c} -o $out/bin/run-in-netns
|
|
|
|
|
'';
|
|
|
|
|
installPhase = "true";
|
|
|
|
|
};
|
|
|
|
|
|
2014-03-19 09:15:57 +00:00
|
|
|
|
nixos-container = pkgs.substituteAll {
|
|
|
|
|
name = "nixos-container";
|
|
|
|
|
dir = "bin";
|
|
|
|
|
isExecutable = true;
|
|
|
|
|
src = ./nixos-container.sh;
|
|
|
|
|
inherit (pkgs) bash socat;
|
|
|
|
|
};
|
2014-03-18 10:36:03 +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;
|
|
|
|
|
description = ''
|
|
|
|
|
Whether this NixOS machine is a lightweight container running
|
|
|
|
|
in another NixOS system.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
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 {
|
|
|
|
|
description = ''
|
|
|
|
|
A specification of the desired configuration of this
|
|
|
|
|
container, as a NixOS module.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
path = mkOption {
|
|
|
|
|
type = types.path;
|
|
|
|
|
example = "/nix/var/nix/profiles/containers/webserver";
|
|
|
|
|
description = ''
|
|
|
|
|
As an alternative to specifying
|
|
|
|
|
<option>config</option>, you can specify the path to
|
|
|
|
|
the evaluated NixOS system configuration, typically a
|
|
|
|
|
symlink to a system profile.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2014-03-18 09:49:25 +00:00
|
|
|
|
privateNetwork = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
|
|
|
|
description = ''
|
|
|
|
|
Whether to give the container its own private virtual
|
|
|
|
|
Ethernet interface. The interface is called
|
|
|
|
|
<literal>eth0</literal>, and is hooked up to the interface
|
|
|
|
|
<literal>c-<replaceable>container-name</replaceable></literal>
|
|
|
|
|
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.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
hostAddress = mkOption {
|
|
|
|
|
type = types.nullOr types.string;
|
|
|
|
|
default = null;
|
|
|
|
|
example = "10.231.136.1";
|
|
|
|
|
description = ''
|
|
|
|
|
The IPv4 address assigned to the host interface.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
localAddress = mkOption {
|
|
|
|
|
type = types.nullOr types.string;
|
|
|
|
|
default = null;
|
|
|
|
|
example = "10.231.136.2";
|
|
|
|
|
description = ''
|
|
|
|
|
The IPv4 address assigned to <literal>eth0</literal>
|
|
|
|
|
in the container.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2013-11-27 15:54:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = mkMerge
|
2014-03-19 18:55:05 +00:00
|
|
|
|
[ (mkIf options.config.isDefined {
|
2013-11-27 15:54:20 +00:00
|
|
|
|
path = (import ../../lib/eval-config.nix {
|
|
|
|
|
modules =
|
|
|
|
|
let extraConfig =
|
|
|
|
|
{ boot.isContainer = true;
|
2014-02-24 17:05:26 +00:00
|
|
|
|
security.initialRootPassword = mkDefault "!";
|
2013-11-27 15:54:20 +00:00
|
|
|
|
networking.hostName = mkDefault name;
|
2014-03-18 10:39:51 +00:00
|
|
|
|
networking.useDHCP = false;
|
2014-03-17 13:04:39 +00:00
|
|
|
|
imports = [ ./container-login.nix ];
|
2013-11-27 15:54:20 +00:00
|
|
|
|
};
|
|
|
|
|
in [ extraConfig config.config ];
|
2014-03-18 17:18:35 +00:00
|
|
|
|
prefix = [ "containers" name ];
|
2013-11-27 15:54:20 +00:00
|
|
|
|
}).config.system.build.toplevel;
|
|
|
|
|
})
|
|
|
|
|
];
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
default = {};
|
|
|
|
|
example = literalExample
|
|
|
|
|
''
|
|
|
|
|
{ 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;
|
|
|
|
|
services.postgresql.package = pkgs.postgresql92;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
'';
|
|
|
|
|
description = ''
|
|
|
|
|
A set of NixOS system configurations to be run as lightweight
|
|
|
|
|
containers. Each container appears as a service
|
|
|
|
|
<literal>container-<replaceable>name</replaceable></literal>
|
|
|
|
|
on the host system, allowing it to be started and stopped via
|
|
|
|
|
<command>systemctl</command> .
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
|
2014-03-19 18:55:05 +00:00
|
|
|
|
systemd.services."container@" =
|
2014-03-20 14:09:38 +00:00
|
|
|
|
{ description = "Container '%i'";
|
2013-11-27 15:54:20 +00:00
|
|
|
|
|
2014-03-20 14:09:38 +00:00
|
|
|
|
unitConfig.RequiresMountsFor = [ "/var/lib/containers/%i" ];
|
2014-03-18 09:49:25 +00:00
|
|
|
|
|
|
|
|
|
path = [ pkgs.iproute ];
|
2013-11-27 15:54:20 +00:00
|
|
|
|
|
2014-03-20 14:09:38 +00:00
|
|
|
|
environment.INSTANCE = "%i";
|
2014-03-19 18:55:05 +00:00
|
|
|
|
|
|
|
|
|
script =
|
2013-11-27 15:54:20 +00:00
|
|
|
|
''
|
2014-03-19 18:55:05 +00:00
|
|
|
|
root="/var/lib/containers/$INSTANCE"
|
|
|
|
|
mkdir -p -m 0755 "$root/etc"
|
|
|
|
|
if ! [ -e "$root/etc/os-release" ]; then
|
|
|
|
|
touch "$root/etc/os-release"
|
2013-11-27 15:54:20 +00:00
|
|
|
|
fi
|
2014-03-17 14:23:20 +00:00
|
|
|
|
|
|
|
|
|
mkdir -p -m 0755 \
|
2014-03-19 18:55:05 +00:00
|
|
|
|
"/nix/var/nix/profiles/per-container/$INSTANCE" \
|
|
|
|
|
"/nix/var/nix/gcroots/per-container/$INSTANCE"
|
2014-03-18 09:49:25 +00:00
|
|
|
|
|
2014-03-19 18:55:05 +00:00
|
|
|
|
SYSTEM_PATH=/nix/var/nix/profiles/system
|
|
|
|
|
if [ -f "/etc/containers/$INSTANCE.conf" ]; then
|
|
|
|
|
. "/etc/containers/$INSTANCE.conf"
|
|
|
|
|
fi
|
2014-03-18 10:04:54 +00:00
|
|
|
|
|
2014-03-18 09:49:25 +00:00
|
|
|
|
# Cleanup from last time.
|
2014-03-19 18:55:05 +00:00
|
|
|
|
ifaceHost=c-$INSTANCE
|
|
|
|
|
ifaceCont=ctmp-$INSTANCE
|
|
|
|
|
ns=net-$INSTANCE
|
|
|
|
|
ip netns del $ns 2> /dev/null || true
|
|
|
|
|
ip link del $ifaceHost 2> /dev/null || true
|
|
|
|
|
ip link del $ifaceCont 2> /dev/null || true
|
|
|
|
|
|
|
|
|
|
if [ "$PRIVATE_NETWORK" = 1 ]; then
|
|
|
|
|
# Create a pair of virtual ethernet devices. On the host,
|
|
|
|
|
# we get ‘c-<container-name’, and on the guest, we get
|
|
|
|
|
# ‘eth0’.
|
|
|
|
|
ip link add $ifaceHost type veth peer name $ifaceCont
|
|
|
|
|
ip netns add $ns
|
|
|
|
|
ip link set $ifaceCont netns $ns
|
|
|
|
|
ip netns exec $ns ip link set $ifaceCont name eth0
|
|
|
|
|
ip netns exec $ns ip link set dev eth0 up
|
|
|
|
|
ip link set dev $ifaceHost up
|
|
|
|
|
if [ -n "$HOST_ADDRESS" ]; then
|
|
|
|
|
ip addr add $HOST_ADDRESS dev $ifaceHost
|
|
|
|
|
ip netns exec $ns ip route add $HOST_ADDRESS dev eth0
|
|
|
|
|
ip netns exec $ns ip route add default via $HOST_ADDRESS
|
|
|
|
|
fi
|
|
|
|
|
if [ -n "$LOCAL_ADDRESS" ]; then
|
|
|
|
|
ip netns exec $ns ip addr add $LOCAL_ADDRESS dev eth0
|
|
|
|
|
ip route add $LOCAL_ADDRESS dev $ifaceHost
|
|
|
|
|
fi
|
|
|
|
|
runInNetNs="${runInNetns}/bin/run-in-netns $ns"
|
|
|
|
|
extraFlags="--capability=CAP_NET_ADMIN"
|
|
|
|
|
fi
|
2013-11-27 15:54:20 +00:00
|
|
|
|
|
2014-03-19 18:55:05 +00:00
|
|
|
|
exec $runInNetNs ${config.systemd.package}/bin/systemd-nspawn \
|
|
|
|
|
-M "$INSTANCE" -D "/var/lib/containers/$INSTANCE" $extraFlags \
|
|
|
|
|
--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" \
|
|
|
|
|
"$SYSTEM_PATH/init"
|
|
|
|
|
'';
|
2013-11-27 15:54:20 +00:00
|
|
|
|
|
|
|
|
|
preStop =
|
|
|
|
|
''
|
2014-03-19 18:55:05 +00:00
|
|
|
|
pid="$(cat /sys/fs/cgroup/systemd/machine/$INSTANCE.nspawn/system/tasks 2> /dev/null)"
|
2013-11-27 15:54:20 +00:00
|
|
|
|
if [ -n "$pid" ]; then
|
|
|
|
|
# Send the RTMIN+3 signal, which causes the container
|
|
|
|
|
# systemd to start halt.target.
|
|
|
|
|
echo "killing container systemd, PID = $pid"
|
|
|
|
|
kill -RTMIN+3 $pid
|
|
|
|
|
# Wait for the container to exit. We can't let systemd
|
|
|
|
|
# do this because it will send a signal to the entire
|
|
|
|
|
# cgroup.
|
|
|
|
|
for ((n = 0; n < 180; n++)); do
|
|
|
|
|
if ! kill -0 $pid 2> /dev/null; then break; fi
|
|
|
|
|
sleep 1
|
|
|
|
|
done
|
|
|
|
|
fi
|
|
|
|
|
'';
|
2014-03-17 14:03:29 +00:00
|
|
|
|
|
2014-03-19 18:55:05 +00:00
|
|
|
|
restartIfChanged = false;
|
|
|
|
|
#reloadIfChanged = true; # FIXME
|
2014-03-17 14:03:29 +00:00
|
|
|
|
|
2014-03-19 18:55:05 +00:00
|
|
|
|
serviceConfig.ExecReload = pkgs.writeScript "reload-container"
|
|
|
|
|
''
|
|
|
|
|
#! ${pkgs.stdenv.shell} -e
|
|
|
|
|
SYSTEM_PATH=/nix/var/nix/profiles/system
|
|
|
|
|
if [ -f "/etc/containers/$INSTANCE.conf" ]; then
|
|
|
|
|
. "/etc/containers/$INSTANCE.conf"
|
|
|
|
|
fi
|
|
|
|
|
echo $SYSTEM_PATH/bin/switch-to-configuration test | \
|
|
|
|
|
${pkgs.socat}/bin/socat unix:/var/lib/containers/$INSTANCE/var/lib/root-shell.socket -
|
|
|
|
|
'';
|
|
|
|
|
};
|
2014-03-17 14:03:29 +00:00
|
|
|
|
|
2014-03-19 18:55:05 +00:00
|
|
|
|
# Generate a configuration file in /etc/containers for each
|
|
|
|
|
# container so that container@.target can get the container
|
|
|
|
|
# configuration.
|
|
|
|
|
environment.etc = mapAttrs' (name: cfg: nameValuePair "containers/${name}.conf"
|
|
|
|
|
{ text =
|
|
|
|
|
''
|
|
|
|
|
SYSTEM_PATH=${cfg.path}
|
|
|
|
|
${optionalString cfg.privateNetwork ''
|
|
|
|
|
PRIVATE_NETWORK=1
|
|
|
|
|
${optionalString (cfg.hostAddress != null) ''
|
|
|
|
|
HOST_ADDRESS=${cfg.hostAddress}
|
|
|
|
|
''}
|
|
|
|
|
${optionalString (cfg.localAddress != null) ''
|
|
|
|
|
LOCAL_ADDRESS=${cfg.localAddress}
|
|
|
|
|
''}
|
|
|
|
|
''}
|
|
|
|
|
'';
|
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)
|
|
|
|
|
''
|
|
|
|
|
${cfg.localAddress} ${name}.containers
|
2014-03-18 17:18:35 +00:00
|
|
|
|
'') config.containers);
|
2014-03-18 09:49:25 +00:00
|
|
|
|
|
2014-03-31 18:16:52 +01:00
|
|
|
|
environment.systemPackages = [ nixos-container ];
|
2014-03-19 18:55:05 +00:00
|
|
|
|
|
2013-11-27 15:54:20 +00:00
|
|
|
|
};
|
2014-02-24 17:05:26 +00:00
|
|
|
|
}
|