1 import ./make-test-python.nix ({ pkgs, lib, ... }: {
2 name = "containers-imperative";
4 maintainers = with lib.maintainers; [ aristid aszlig eelco kampfschlaefer ];
8 { config, pkgs, lib, ... }:
9 { imports = [ ../modules/installer/cd-dvd/channel.nix ];
11 # XXX: Sandbox setup fails while trying to hardlink files from the host's
12 # store file system into the prepared chroot directory.
13 nix.settings.sandbox = false;
14 nix.settings.substituters = []; # don't try to access cache.nixos.org
16 virtualisation.memorySize = 2048;
17 virtualisation.writableStore = true;
18 # Make sure we always have all the required dependencies for creating a
19 # container available within the VM, because we don't have network access.
20 virtualisation.additionalPaths = let
21 emptyContainer = import ../lib/eval-config.nix {
22 modules = lib.singleton {
23 nixpkgs = { inherit (config.nixpkgs) localSystem; };
25 containers.foo.config = {};
28 # The system is inherited from the host above.
29 # Set it to null, to remove the "legacy" entrypoint's non-hermetic default.
33 stdenv stdenvNoCC emptyContainer.config.containers.foo.path
34 libxslt desktop-file-utils texinfo docbook5 libxml2
35 docbook_xsl_ns xorg.lndir documentation-highlighter
40 tmpfilesContainerConfig = pkgs.writeText "container-config-tmpfiles" ''
42 systemd.tmpfiles.rules = [ "d /foo - - - - -" ];
43 systemd.services.foo = {
44 serviceConfig.Type = "oneshot";
45 script = "ls -al /foo";
46 wantedBy = [ "multi-user.target" ];
50 brokenCfg = pkgs.writeText "broken.nix" ''
54 message = "I never evaluate";
60 with subtest("Make sure we have a NixOS tree (required by ‘nixos-container create’)"):
61 machine.succeed("PAGER=cat nix-env -qa -A nixos.hello >&2")
65 with subtest("Create some containers imperatively"):
66 id1 = machine.succeed("nixos-container create foo --ensure-unique-name").rstrip()
67 machine.log(f"created container {id1}")
69 id2 = machine.succeed("nixos-container create foo --ensure-unique-name").rstrip()
70 machine.log(f"created container {id2}")
74 with subtest(f"Put the root of {id2} into a bind mount"):
76 f"mv /var/lib/nixos-containers/{id2} /id2-bindmount",
77 f"mount --bind /id2-bindmount /var/lib/nixos-containers/{id1}",
80 ip1 = machine.succeed(f"nixos-container show-ip {id1}").rstrip()
81 ip2 = machine.succeed(f"nixos-container show-ip {id2}").rstrip()
85 "Create a directory and a file we can later check if it still exists "
86 + "after destruction of the container"
88 machine.succeed("mkdir /nested-bindmount")
89 machine.succeed("echo important data > /nested-bindmount/dummy")
92 "Create a directory with a dummy file and bind-mount it into both containers."
95 important_path = f"/var/lib/nixos-containers/{id}/very/important/data"
97 f"mkdir -p {important_path}",
98 f"mount --bind /nested-bindmount {important_path}",
101 with subtest("Start one of them"):
102 machine.succeed(f"nixos-container start {id1}")
104 with subtest("Execute commands via the root shell"):
105 assert "Linux" in machine.succeed(f"nixos-container run {id1} -- uname")
107 with subtest("Execute a nix command via the root shell. (regression test for #40355)"):
109 f"nixos-container run {id1} -- nix-instantiate -E "
110 + '\'derivation { name = "empty"; builder = "false"; system = "false"; }\' '
113 with subtest("Stop and start (regression test for #4989)"):
114 machine.succeed(f"nixos-container stop {id1}")
115 machine.succeed(f"nixos-container start {id1}")
117 # clear serial backlog for next tests
118 machine.succeed("logger eat console backlog 3ea46eb2-7f82-4f70-b810-3f00e3dd4c4d")
119 machine.wait_for_console_text(
120 "eat console backlog 3ea46eb2-7f82-4f70-b810-3f00e3dd4c4d"
123 with subtest("Stop a container early"):
124 machine.succeed(f"nixos-container stop {id1}")
125 machine.succeed(f"nixos-container start {id1} >&2 &")
126 machine.wait_for_console_text("Stage 2")
127 machine.succeed(f"nixos-container stop {id1}")
128 machine.wait_for_console_text(f"Container {id1} exited successfully")
129 machine.succeed(f"nixos-container start {id1}")
131 with subtest("Stop a container without machined (regression test for #109695)"):
132 machine.systemctl("stop systemd-machined")
133 machine.succeed(f"nixos-container stop {id1}")
134 machine.wait_for_console_text(f"Container {id1} has been shut down")
135 machine.succeed(f"nixos-container start {id1}")
137 with subtest("tmpfiles are present"):
138 machine.log("creating container tmpfiles")
140 "nixos-container create tmpfiles --config-file ${tmpfilesContainerConfig}"
142 machine.log("created, starting…")
143 machine.succeed("nixos-container start tmpfiles")
144 machine.log("done starting, investigating…")
146 "echo $(nixos-container run tmpfiles -- systemctl is-active foo.service) | grep -q active;"
148 machine.succeed("nixos-container destroy tmpfiles")
150 with subtest("Execute commands via the root shell"):
151 assert "Linux" in machine.succeed(f"nixos-container run {id1} -- uname")
153 with subtest("Destroy the containers"):
155 machine.succeed(f"nixos-container destroy {id}")
157 with subtest("Check whether destruction of any container has killed important data"):
158 machine.succeed("grep -qF 'important data' /nested-bindmount/dummy")
160 with subtest("Ensure that the container path is gone"):
161 print(machine.succeed("ls -lsa /var/lib/nixos-containers"))
162 machine.succeed(f"test ! -e /var/lib/nixos-containers/{id1}")
164 with subtest("Ensure that a failed container creation doesn'leave any state"):
166 "nixos-container create b0rk --config-file ${brokenCfg}"
168 machine.succeed("test ! -e /var/lib/nixos-containers/b0rk")