From 996cb7c373dcb3ead4de59815b19b9d64d0186f1 Mon Sep 17 00:00:00 2001 From: Lan Tian Date: Sat, 14 Sep 2024 23:00:29 -0700 Subject: [PATCH] Add option to declaratively configure visible bridges --- README.md | 10 +++++++++- modules/proxmox-ve/bridges.nix | 26 ++++++++++++++++++++++++++ modules/proxmox-ve/default.nix | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 modules/proxmox-ve/bridges.nix diff --git a/README.md b/README.md index 6fda726..4679714 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ Below is a fragment of a NixOS configuration that enables Proxmox VE. To get internet in your VMs, you need to add a network device to the VM, connected to a bridge. To get this working, follow this 2 steps: -1. Create the bridge in `System->Network->Create->Linux Bridge`. This operation has no effect on your system and is just a quirk for Proxmox to know the existence of your bridge. +1. Set the list of bridges in `services.proxmox-ve.bridges` option. This is the list of bridges that will be visible in Proxmox web interface. Note that this option doesn't affect your OS level network config in any way. 2. Configure your networking through NixOS configuration so that the bridge you created in the Proxmox web interface actually exists! ### Example NixOS networking configurations @@ -104,6 +104,10 @@ Any kind of advanced networking configuration is possible through the usual NixO #### With `systemd-networkd` ```nix +# Make vmbr0 bridge visible in Proxmox web interface +services.proxmox-ve.bridges = [ "vmbr0" ]; + +# Actually set up the vmbr0 bridge systemd.network.networks."10-lan" = { matchConfig.Name = [ "ens18" ]; networkConfig = { @@ -131,6 +135,10 @@ systemd.network.networks."10-lan-bridge" = { ### With scripted networking ```nix +# Make vmbr0 bridge visible in Proxmox web interface +services.proxmox-ve.bridges = [ "vmbr0" ]; + +# Actually set up the vmbr0 bridge networking.bridges.vmbr0.interfaces = [ "ens18" ]; networking.interfaces.vmbr0.useDHCP = lib.mkDefault true; ``` diff --git a/modules/proxmox-ve/bridges.nix b/modules/proxmox-ve/bridges.nix new file mode 100644 index 0000000..ffc34dd --- /dev/null +++ b/modules/proxmox-ve/bridges.nix @@ -0,0 +1,26 @@ +{ + config, + lib, + ... +}: +let + cfg = config.services.proxmox-ve.bridges; +in +{ + options.services.proxmox-ve.bridges = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = [ ]; + description = "List of Linux or OVS bridges visible in Proxmox web interface. This option has no effect on OS level network config."; + }; + + config = lib.mkIf (builtins.length cfg > 0) { + environment.etc."network/interfaces" = { + mode = "0644"; + text = lib.concatMapStringsSep "\n" (br: '' + auto ${br} + iface ${br} inet static + bridge_ports none + '') cfg; + }; + }; +} diff --git a/modules/proxmox-ve/default.nix b/modules/proxmox-ve/default.nix index 368293f..db70880 100644 --- a/modules/proxmox-ve/default.nix +++ b/modules/proxmox-ve/default.nix @@ -17,6 +17,7 @@ in ]; imports = [ + ./bridges.nix ./cluster.nix # ./firewall.nix # ./ha-manager.nix