2022-11-27 15:38:34 +00:00
|
|
|
# Test whether we can run a NixOS container inside a Nix build using systemd-nspawn.
|
2023-01-20 14:32:31 +00:00
|
|
|
{ lib, nixpkgs, ... }:
|
2022-11-27 15:38:34 +00:00
|
|
|
|
2023-01-20 14:32:31 +00:00
|
|
|
{
|
2022-11-27 15:38:34 +00:00
|
|
|
name = "containers";
|
|
|
|
|
|
|
|
nodes =
|
|
|
|
{
|
|
|
|
host =
|
|
|
|
{ config, lib, pkgs, nodes, ... }:
|
|
|
|
{ virtualisation.writableStore = true;
|
|
|
|
virtualisation.diskSize = 2048;
|
|
|
|
virtualisation.additionalPaths =
|
2023-01-08 13:38:34 +00:00
|
|
|
[ pkgs.stdenvNoCC
|
2022-11-27 15:38:34 +00:00
|
|
|
(import ./systemd-nspawn.nix { inherit nixpkgs; }).toplevel
|
|
|
|
];
|
|
|
|
virtualisation.memorySize = 4096;
|
2022-12-24 09:14:09 +00:00
|
|
|
nix.settings.substituters = lib.mkForce [ ];
|
2022-11-27 15:38:34 +00:00
|
|
|
nix.extraOptions =
|
|
|
|
''
|
2022-11-28 20:54:02 +00:00
|
|
|
extra-experimental-features = nix-command auto-allocate-uids cgroups
|
2022-11-27 15:38:34 +00:00
|
|
|
extra-system-features = uid-range
|
|
|
|
'';
|
|
|
|
nix.nixPath = [ "nixpkgs=${nixpkgs}" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testScript = { nodes }: ''
|
|
|
|
start_all()
|
|
|
|
|
|
|
|
host.succeed("nix --version >&2")
|
|
|
|
|
|
|
|
# Test that 'id' gives the expected result in various configurations.
|
|
|
|
|
|
|
|
# Existing UIDs, sandbox.
|
2023-01-08 13:38:34 +00:00
|
|
|
host.succeed("nix build -v --no-auto-allocate-uids --sandbox -L --offline --impure --file ${./id-test.nix} --argstr name id-test-1")
|
2022-11-27 15:38:34 +00:00
|
|
|
host.succeed("[[ $(cat ./result) = 'uid=1000(nixbld) gid=100(nixbld) groups=100(nixbld)' ]]")
|
|
|
|
|
|
|
|
# Existing UIDs, no sandbox.
|
2023-01-08 13:38:34 +00:00
|
|
|
host.succeed("nix build -v --no-auto-allocate-uids --no-sandbox -L --offline --impure --file ${./id-test.nix} --argstr name id-test-2")
|
2022-11-27 15:38:34 +00:00
|
|
|
host.succeed("[[ $(cat ./result) = 'uid=30001(nixbld1) gid=30000(nixbld) groups=30000(nixbld)' ]]")
|
|
|
|
|
|
|
|
# Auto-allocated UIDs, sandbox.
|
2023-01-08 13:38:34 +00:00
|
|
|
host.succeed("nix build -v --auto-allocate-uids --sandbox -L --offline --impure --file ${./id-test.nix} --argstr name id-test-3")
|
2022-11-27 15:38:34 +00:00
|
|
|
host.succeed("[[ $(cat ./result) = 'uid=1000(nixbld) gid=100(nixbld) groups=100(nixbld)' ]]")
|
|
|
|
|
|
|
|
# Auto-allocated UIDs, no sandbox.
|
2023-01-08 13:38:34 +00:00
|
|
|
host.succeed("nix build -v --auto-allocate-uids --no-sandbox -L --offline --impure --file ${./id-test.nix} --argstr name id-test-4")
|
2022-11-27 15:38:34 +00:00
|
|
|
host.succeed("[[ $(cat ./result) = 'uid=872415232 gid=30000(nixbld) groups=30000(nixbld)' ]]")
|
|
|
|
|
|
|
|
# Auto-allocated UIDs, UID range, sandbox.
|
2023-01-08 13:38:34 +00:00
|
|
|
host.succeed("nix build -v --auto-allocate-uids --sandbox -L --offline --impure --file ${./id-test.nix} --argstr name id-test-5 --arg uidRange true")
|
2022-11-27 15:38:34 +00:00
|
|
|
host.succeed("[[ $(cat ./result) = 'uid=0(root) gid=0(root) groups=0(root)' ]]")
|
|
|
|
|
|
|
|
# Auto-allocated UIDs, UID range, no sandbox.
|
2023-01-08 13:38:34 +00:00
|
|
|
host.fail("nix build -v --auto-allocate-uids --no-sandbox -L --offline --impure --file ${./id-test.nix} --argstr name id-test-6 --arg uidRange true")
|
2022-11-27 15:38:34 +00:00
|
|
|
|
|
|
|
# Run systemd-nspawn in a Nix build.
|
2023-09-19 15:03:21 +00:00
|
|
|
#host.succeed("nix build -v --auto-allocate-uids --sandbox -L --offline --impure --file ${./systemd-nspawn.nix} --argstr nixpkgs ${nixpkgs}")
|
|
|
|
#host.succeed("[[ $(cat ./result/msg) = 'Hello World' ]]")
|
2022-11-27 15:38:34 +00:00
|
|
|
'';
|
|
|
|
|
2023-01-20 14:32:31 +00:00
|
|
|
}
|