Commit Graph

13 Commits

Author SHA1 Message Date
figsoda
08e0e1b667 nixos/nspawn-container: remove outdated comment and flush 2026-07-29 19:23:25 -04:00
figsoda
503d46170d nixos/nspawn-container: fix build 2026-07-25 15:41:51 -04:00
zowoq
814dbea27f nixos/nspawn-container: hash name, remove assertion
the code comment is copied from the commit that added the assertion

af0238c70e
2026-07-07 10:59:58 +10:00
zowoq
f5c8639dd5 nixos/nspawn-container: use ty instead of mypy
the test driver package is also using ty
85d58871ae
2026-05-29 12:25:45 +10:00
zowoq
5eba89ff43 nixos/nspawn-container: add "usr/bin"
same change was done for nixos/nixos-containers in b9f2fa1c7d
2026-05-22 11:27:39 +10:00
Jacek Galowicz
948c5d0da2 nixos-test-driver: fix vlan/bridge cleanup 2026-05-03 17:45:52 +02:00
Kierán Meinhardt
d1386a426b nixos/virtualisation: provide actionable advice on VLAN TAP failure 2026-03-04 08:40:12 +01:00
Kierán Meinhardt
0702405810 nixos/test-driver: implement container↔VM networking 2026-01-28 11:28:33 +01:00
Kierán Meinhardt
a88839a901 nixos/nspawn-container: add support for shared directory
Co-authored-by: Jeremy Fleischman <jeremyfleischman@gmail.com>
2026-01-27 16:58:48 +01:00
Kierán Meinhardt
1e3c78f75d nixos/nspawn-container: run-nspawn as cli application 2026-01-19 10:36:38 +01:00
Kierán Meinhardt
7ba279d21e nixos/nspawn-container: rename initArgs to cmdline 2026-01-19 10:36:38 +01:00
Kierán Meinhardt
40f2d0b242 nixos/nspawn-container: fix typo in comment 2026-01-19 10:36:37 +01:00
Jeremy Fleischman
4bd5482aa6 nixos/nspawn-container: init a new nspawn-container profile
This shares a lot in common with the
<nixos/modules/virtualisation/nixos-containers.nix> infrastructure, but
is designed to behave like our `qemu-vm.nix` profile (provides a lot of
the same `virtualisation.*` options, produces a simple script you can
run).

This lays the groundwork to be able to rework the nixos test
infrastructure to allow for containers as well as qemu nodes. That work
isn't quite done yet, but if you want more context, you can see the
followup work in <https://github.com/applicative-systems/nixpkgs/compare/nspawn-container-profile...applicative-systems:nixpkgs:nixos-test-containers>.

Credit due to the [Clan.lol](https://clan.lol/) team for first
implementing this. I'm just cleaning it up and making it play nicely
with upstream.

To try it out, create a `demo.nix`:

```nix
let
  pkgs = import ./. { };
  mkContainer =
    {
      nodeNumber,
      vlans,
    }:
    pkgs.nixos (
      {
        config,
        modulesPath,
        pkgs,
        lib,
        ...
      }:
      let
        interfaces = lib.attrValues config.virtualisation.allInterfaces;

        # Automatically assign IP addresses to requested interfaces.
        assignIPs = lib.filter (i: i.assignIP) interfaces;
        ipInterfaces = lib.forEach assignIPs (
          i:
          lib.nameValuePair i.name {
            ipv4.addresses = [
              {
                address = "192.168.${toString i.vlan}.${toString nodeNumber}";
                prefixLength = 24;
              }
            ];
          }
        );
      in
      {
        imports = [ "${modulesPath}/virtualisation/nspawn-container" ];
        users.users.root.password = "";

        networking.hostName = "c${toString nodeNumber}";
        virtualisation.vlans = vlans;
        networking.interfaces = lib.listToAttrs ipInterfaces;

        environment.systemPackages = [ pkgs.neovim ];
        system.stateVersion = lib.trivial.release;
      }
    );
in
{
  container1 = mkContainer {
    nodeNumber = 1;
    vlans = [ 1 ];
  };
  container2 = mkContainer {
    nodeNumber = 2;
    vlans = [ 2 ];
  };
  container12 = mkContainer {
    nodeNumber = 12;
    vlans = [
      1
      2
    ];
  };
}
```

Build and run the machines in separate terminals (unfortunately,
`systemd-nspawn` requires `sudo`):

```console
$ sudo $(nix-build ./demo.nix -A container1.config.system.build.nspawn)/bin/run-c1-nspawn
$ sudo $(nix-build ./demo.nix -A container2.config.system.build.nspawn)/bin/run-c2-nspawn
$ sudo $(nix-build ./demo.nix -A container12.config.system.build.nspawn)/bin/run-c12-nspawn
```

You can log into this machines as `root`, and verify they can ping each
other:

`c1` can ping `c12`:

```
[root@c1:~]# ping 192.168.1.12 -c 1
PING 192.168.1.12 (192.168.1.12) 56(84) bytes of data.
64 bytes from 192.168.1.12: icmp_seq=1 ttl=64 time=0.164 ms
...
```

So can `c2`:

```
[root@c2:~]# ping 192.168.2.12
PING 192.168.2.12 (192.168.2.12) 56(84) bytes of data.
64 bytes from 192.168.2.12: icmp_seq=1 ttl=64 time=0.127 ms
```
2026-01-19 10:36:37 +01:00