From def1ff330cf0dbbec2b3e9c93e1e97cd49eac427 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Tue, 6 Jan 2026 15:48:08 +0900 Subject: [PATCH] nix/vm: add nixos-containers and networking --- nix/vm.nix | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 5 deletions(-) diff --git a/nix/vm.nix b/nix/vm.nix index 05ee275..6b38b1e 100644 --- a/nix/vm.nix +++ b/nix/vm.nix @@ -1,22 +1,28 @@ { self, nixpkgs, ... }: -nixpkgs.lib.nixosSystem { +let system = "x86_64-linux"; pkgs = import nixpkgs { - system = "x86_64-linux"; + inherit system; overlays = [ - self.overlays.roowho2 + self.overlays.default ]; }; +in +nixpkgs.lib.nixosSystem { + inherit system pkgs; modules = [ "${nixpkgs}/nixos/modules/virtualisation/qemu-vm.nix" "${nixpkgs}/nixos/tests/common/user-account.nix" self.nixosModules.default - ({ config, pkgs, ... }: { + ({ config, lib, ... }: { system.stateVersion = config.system.nixos.release; virtualisation.graphics = false; + virtualisation.memorySize = 256; + virtualisation.vlans = [ 1 ]; + users.users.alice.extraGroups = [ "wheel" ]; services.getty.autologinUser = "alice"; @@ -27,6 +33,11 @@ nixpkgs.lib.nixosSystem { Try running any of: rwho + ruptime + + To log into other containers, use: + machinectl shell c1 + machinectl shell c2 Password for alice is 'foobar' @@ -43,7 +54,83 @@ nixpkgs.lib.nixosSystem { defaultEditor = true; }; - environment.systemPackages = with pkgs; [ jq roowho2 ]; + security.polkit.enable = true; + + networking = { + useNetworkd = true; + macvlans.mv-eth0-host = { + interface = "eth0"; + mode = "bridge"; + }; + }; + + systemd.network.enable = true; + systemd.network.networks."mv-eth0-host" = { + matchConfig.Name = "mv-eth0-host"; + gateway = [ + "10.0.0.255" + ]; + DHCP = "no"; + address = [ + "10.0.0.128/24" + ]; + }; + + containers = let + commonContainerConfig = n: { + autoStart = true; + macvlans = [ "eth0" ]; + config = { + imports = [ + "${nixpkgs}/nixos/tests/common/user-account.nix" + self.nixosModules.default + ]; + system.stateVersion = config.system.nixos.release; + nixpkgs.pkgs = pkgs; + + networking = { + hostName = "c${toString n}"; + useDHCP = false; + useNetworkd = true; + useHostResolvConf = false; + }; + + systemd.network.enable = true; + systemd.network.networks."mv-eth0" = { + matchConfig.Name = "mv-eth0"; + gateway = [ "10.0.0.255" ]; + address = [ "10.0.0.${toString n}/24" ]; + dhcpV4Config.ClientIdentifier = "mac"; + }; + + users.users.alice.extraGroups = [ "wheel" ]; + + users.motd = '' + ================================= + Welcome to the roowho2 c${toString n} container! + + Try running any of: + rwho + ruptime + + Password for alice is 'foobar' + ================================= + ''; + + services.roowho2 = { + enable = true; + }; + + programs.vim = { + enable = true; + defaultEditor = true; + }; + }; + }; + in { + c1 = commonContainerConfig 1; + c2 = commonContainerConfig 2; + }; }) ]; }