nixpkgs/nixos/modules/profiles/docker-container.nix
Thomas Tuegel 757c7f3773
docker-container: Remove /etc symlink
The system output usually contains a symlink from /etc to the static
configuration for the benefit of the stage-1 script in the initrd. The stage-2
script is usually started in the real root without such a symlink. In a
container, there is no stage-1 and the system output is used directly as a real
root. If the symlink is present, setup-etc.pl will create a symlink cycle and
the system cannot boot. There is no reason for the /etc link to exist in a
container because setup-etc.pl will create the necessary files. The container
module will now remove the /etc symlink and create an empty directory. The empty
/etc is for container managers to populate it with site-specific settings; for
example, to set the hostname. This is required to boot NixOS in an LXC container
on another host.

See also: #9735
2019-11-27 15:51:19 -06:00

61 lines
1.5 KiB
Nix

{ config, lib, pkgs, ... }:
with lib;
let inherit (pkgs) writeScript; in
let
pkgs2storeContents = l : map (x: { object = x; symlink = "none"; }) l;
in {
# Docker image config.
imports = [
../installer/cd-dvd/channel.nix
./minimal.nix
./clone-config.nix
];
# Create the tarball
system.build.tarball = pkgs.callPackage ../../lib/make-system-tarball.nix {
contents = [
{
source = "${config.system.build.toplevel}/.";
target = "./";
}
];
extraArgs = "--owner=0";
# Add init script to image
storeContents = pkgs2storeContents [
config.system.build.toplevel
pkgs.stdenv
];
# Some container managers like lxc need these
extraCommands =
let script = writeScript "extra-commands.sh" ''
rm etc
mkdir -p proc sys dev etc
'';
in script;
};
boot.isContainer = true;
boot.postBootCommands =
''
# After booting, register the contents of the Nix store in the Nix
# database.
if [ -f /nix-path-registration ]; then
${config.nix.package.out}/bin/nix-store --load-db < /nix-path-registration &&
rm /nix-path-registration
fi
# nixos-rebuild also requires a "system" profile
${config.nix.package.out}/bin/nix-env -p /nix/var/nix/profiles/system --set /run/current-system
'';
# Install new init script
system.activationScripts.installInitScript = ''
ln -fs $systemConfig/init /init
'';
}