Declarative NixOS machines on systemd-nspawn.
{
inputs = {
nspawn-machines = {
url = "github:SwornSystems/nix-nspawn-machines";
};
};
}nixpkgs.lib.nixosSystem {
inherit system;
modules = [
nspawn-machines.nixosModules.default
(
{
lib,
pkgs,
...
}:
{
networking = {
hostName = "hello";
domain = "example";
firewall.allowedTCPPorts = [ 80 ];
};
virtualisation.nspawn-machines.settings = {
Network.VirtualEthernet = true;
};
services.nginx = {
enable = true;
virtualHosts.default.root = pkgs.writeTextDir "index.html" "Hello world!";
};
system.stateVersion = lib.trivial.release;
}
)
];
}> nix build .#nixosConfigurations.hello.config.system.build.nspawn-machine
> sudo ./result/bin/run-hello-nspawn-machine
Running as unit: nspawn-machine-hello.service; invocation ID: ...
> sudo machinectl list
MACHINE CLASS SERVICE OS VERSION ADDRESSES
hello.example container systemd-nspawn nixos ... ...
1 machines listed.
> curl http://hello.example
Hello world!
> sudo journalctl --machine hello.example --unit nginx
... hello systemd[1]: Started Nginx Web Server.
> sudo machinectl stop hello.exampleUpstream nixpkgs already ships nspawn-container support.
It's more focused on usage in NixOS integration tests rather than general use.
It makes certain environmental assumptions that this approach does not.
Our approach aims to be as minimal as possible, just a thin wrapper around systemd.nspawn files.
Many.
These are some I found trying to replace Docker and QEMU usages with nspawn.
When running as root, most things work fine.
Ideally, over time these issues resolve upstream, and rootless becomes usable.
- PATCHED
rootidmapmounts are unwritable when the user's UID and GID differ. mountfsdreuses the UID map as the GID map.- MERGED (
v262)PrivateUsers=pickdefaults tochownownership when set in a settings file, butautoon the CLI. - Rootless
Bind=mounts cannot read the user's own files. Zone=andBridge=require root.nss-mymachinesonly resolves root machines, not rootless ones.mountfsdrequires root to mount/nix/store, even though it's world-readable.
- Firewall stops machines from getting a DHCP lease.
systemdpackage needsvmlinux.hfor rootless machines.nsresourced,mountfsd, and usermachinedunits aren't installed
- Runners need
systemd-container,libnss-mymachines, andnscdto resolve machines.
Licensed under the terms of both the MIT License and the Apache License (Version 2.0).