diff options
| author | Jeremy Fleischman <jeremyfleischman@gmail.com> | 2025-12-05 17:04:01 -0800 |
|---|---|---|
| committer | KierĂ¡n Meinhardt <kieran.meinhardt@applicative.systems> | 2026-01-19 10:36:37 +0100 |
| commit | 4bd5482aa60b025be8d0fc56764bc1672fd1274e (patch) | |
| tree | 6954542179b75c18127b6af2c41047ad6f42a911 /pkgs/development/python-modules/robotframework-pythonlibcore/git@git.tavy.me:nixos | |
| parent | 9a9938e5feabe43f89dea169d33bd8c0011442da (diff) | |
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
```
Diffstat (limited to 'pkgs/development/python-modules/robotframework-pythonlibcore/git@git.tavy.me:nixos')
0 files changed, 0 insertions, 0 deletions
