diff --git a/docs/src/content/docs/ghaf/dev/guides/creating-vms.mdx b/docs/src/content/docs/ghaf/dev/guides/creating-vms.mdx index 6d6a9b6228..951b5139bc 100644 --- a/docs/src/content/docs/ghaf/dev/guides/creating-vms.mdx +++ b/docs/src/content/docs/ghaf/dev/guides/creating-vms.mdx @@ -220,7 +220,7 @@ Application VMs use a template pattern for multiple instances. ### Using mkAppVm -The `mkAppVm` function creates application VMs. All values (name, ramMb, borderColor, applications, vtpm) are defined in the `mkAppVm` call and stored in `evaluatedConfig.config.ghaf.appvm.vmDef`. Host-level options automatically read from there. +The `mkAppVm` function creates application VMs. All values (name, mem, borderColor, applications, vtpm) are defined in the `mkAppVm` call and stored in `evaluatedConfig.config.ghaf.appvm.vmDef`. Host-level options automatically read from there. ```nix { config, lib, ... }: @@ -277,7 +277,7 @@ Use the `extensions` option to add modules to an existing app VM without modifyi | `usbPassthrough` | list | USB passthrough rules (host-side) | | `bootPriority` | enum | Boot priority: "low", "medium", "high" | -Values like `name`, `ramMb`, `borderColor`, `applications`, and `vtpm` are all derived from `evaluatedConfig.config.ghaf.appvm.vmDef` and should be set in the `mkAppVm` call, not at the host level. +Values like `name`, `mem`, `borderColor`, `applications`, and `vtpm` are all derived from `evaluatedConfig.config.ghaf.appvm.vmDef` and should be set in the `mkAppVm` call, not at the host level. ### Application Definition diff --git a/docs/src/content/docs/ghaf/dev/ref/creating_appvm.mdx b/docs/src/content/docs/ghaf/dev/ref/creating_appvm.mdx index 4bd495c2ec..588e2eeec3 100644 --- a/docs/src/content/docs/ghaf/dev/ref/creating_appvm.mdx +++ b/docs/src/content/docs/ghaf/dev/ref/creating_appvm.mdx @@ -20,8 +20,8 @@ To create an App VM, do the following: | name | str | yes | This name is postfixed with `-vm` and will be shown in microvm list. The name, for example, `chromium-vm` will be also the VM hostname. The length of the name must be 8 characters or less. | “chromium” | | packages | list of types.package | no | Packages to include in a VM. It is possible to make it empty or add several packages. | [chromium top] | | macAddress | str | yes | Needed for network configuration. | "02:00:00:03:03:05" | - | ramMb | int, [1, …, host memory] | no | Memory in MB. | 3072 | - | cores | int, [1, …, host cores] | no | Virtual CPU cores. | + | mem | int, [1, …, host memory] | no | Memory in MB. | 3072 | + | vcpu | int, [1, …, host cores] | no | Virtual CPU cores. | 2. Create a new option for your VM in [modules/reference/appvms/default.nix](https://github.com/tiiuae/ghaf/blob/main/modules/reference/appvms/default.nix). For example: diff --git a/modules/microvm/appvm.nix b/modules/microvm/appvm.nix index d725ba7900..9ccd94b89a 100644 --- a/modules/microvm/appvm.nix +++ b/modules/microvm/appvm.nix @@ -12,7 +12,7 @@ # The VM-side configuration is in appvm-base.nix, created via mkAppVm in profiles. # # Extension Pattern: -# ALL values (name, ramMb, borderColor, applications, etc.) should be defined ONLY +# ALL values (name, mem, borderColor, applications, etc.) should be defined ONLY # in the mkAppVm call. Host-level options automatically read from # evaluatedConfig.config.ghaf.appvm.vmDef. This eliminates duplication. # @@ -59,7 +59,7 @@ let evaluatedConfig = finalEvaluatedConfig; # Derive values from vmDef - the attrset key is used as fallback for name name = vmDef.name or attrName; - ramMb = vmDef.ramMb or 4096; + mem = vmDef.mem or 4096; balloonRatio = vmDef.balloonRatio or 2; borderColor = vmDef.borderColor or null; applications = vmDef.applications or [ ]; @@ -139,7 +139,7 @@ in description = '' Read-only attrset of enabled VMs with all values derived from evaluatedConfig. Use this instead of accessing vms directly when you need derived values - like vtpm, applications, ramMb, etc. + like vtpm, applications, mem, etc. ''; }; @@ -148,7 +148,7 @@ in App VM configurations. Each VM must have evaluatedConfig set via mkAppVm. Extension Pattern: - - ALL values (name, ramMb, borderColor, applications, vtpm, etc.) + - ALL values (name, mem, borderColor, applications, vtpm, etc.) are derived from evaluatedConfig.config.ghaf.appvm.vmDef - You only need to set 'enable' and 'evaluatedConfig' here - Use 'extensions' to add modules from external features (e.g., ghaf-intro) diff --git a/modules/microvm/host/mem-manager.nix b/modules/microvm/host/mem-manager.nix index 9262909408..0b2234796c 100644 --- a/modules/microvm/host/mem-manager.nix +++ b/modules/microvm/host/mem-manager.nix @@ -30,7 +30,7 @@ in let vmConfig = lib.ghaf.vm.getConfig config.microvm.vms.${name}; microvmConfig = vmConfig.microvm; - # Use enabledVms which has derived ramMb from evaluatedConfig + # Use enabledVms which has derived mem from evaluatedConfig vmBaseName = lib.removeSuffix "-vm" name; appvmConfig = config.ghaf.virtualization.microvm.appvm.enabledVms.${vmBaseName} or null; in @@ -43,7 +43,7 @@ in Type = "simple"; WorkingDirectory = "${config.microvm.stateDir}/${name}"; ExecStart = "${pkgs.ghaf-mem-manager}/bin/ghaf-mem-manager -s ${name}.sock -m ${ - toString (appvmConfig.ramMb * 1024 * 1024) + toString (appvmConfig.mem * 1024 * 1024) } -M ${toString (microvmConfig.mem * 1024 * 1024)}"; }; }; diff --git a/modules/microvm/sysvms/appvm-base.nix b/modules/microvm/sysvms/appvm-base.nix index 7bddd8a0d4..58e5e97e3c 100644 --- a/modules/microvm/sysvms/appvm-base.nix +++ b/modules/microvm/sysvms/appvm-base.nix @@ -9,7 +9,7 @@ # Unlike singleton VMs (gui-vm, net-vm, etc.), App VMs are instantiated multiple times. # Each instance is configured via hostConfig.appvm which contains: # - name: VM name (e.g., "chromium", "comms") -# - ramMb, cores: Resource allocation +# - mem, vcpu: Resource allocation # - applications: List of apps with name, command, packages, etc. # - packages: Additional packages for the VM # - vtpm, waypipe, ghafAudio: Feature flags @@ -305,10 +305,10 @@ in microvm = { optimize.enable = false; # Sensible defaults based on vm definition - can be further overridden via vmConfig - mem = lib.mkDefault ((vm.ramMb or 4096) * ((vm.balloonRatio or 2) + 1)); + mem = lib.mkDefault ((vm.mem or 4096) * ((vm.balloonRatio or 2) + 1)); balloon = (vm.balloonRatio or 2) > 0; deflateOnOOM = false; - vcpu = lib.mkDefault (vm.cores or 4); + vcpu = lib.mkDefault (vm.vcpu or 4); hypervisor = "qemu"; shares = [ diff --git a/modules/microvm/vm-config.nix b/modules/microvm/vm-config.nix index 45e695c3c2..7fd94c7cff 100644 --- a/modules/microvm/vm-config.nix +++ b/modules/microvm/vm-config.nix @@ -82,21 +82,31 @@ let }; }; - # App VM configuration submodule (uses ramMb/cores for consistency with appvm definitions) + # App VM configuration submodule (uses mem/vcpu for consistency with system VM definitions) appVmConfigType = types.submodule { options = { - ramMb = mkOption { + mem = mkOption { type = types.nullOr types.int; default = null; description = "App VM memory allocation in MB."; }; - cores = mkOption { + vcpu = mkOption { type = types.nullOr types.int; default = null; description = "App VM vCPU count."; }; + balloonRatio = mkOption { + type = types.nullOr types.int; + default = null; + description = '' + Memory balloon ratio. The VM is allocated mem * (balloonRatio + 1) + bytes of memory, with ballooning enabled when balloonRatio > 0. + If null, uses the default from the VM definition (typically 2). + ''; + }; + extraModules = mkOption { type = types.listOf types.unspecified; default = [ ]; @@ -147,8 +157,8 @@ in ''; example = literalExpression '' { - chromium = { ramMb = 8192; extraModules = [ ./chrome.nix ]; }; - comms = { ramMb = 4096; }; + chromium = { mem = 8192; extraModules = [ ./chrome.nix ]; }; + comms = { mem = 4096; }; } ''; }; diff --git a/modules/profiles/laptop-x86.nix b/modules/profiles/laptop-x86.nix index 13ffa120d0..8d8de8fd94 100644 --- a/modules/profiles/laptop-x86.nix +++ b/modules/profiles/laptop-x86.nix @@ -213,12 +213,13 @@ in mkAppVm = vmDef: let - # Apply vmConfig.appvms overrides (ramMb, cores) + # Apply vmConfig.appvms overrides (mem, vcpu) vmCfg = config.ghaf.virtualization.vmConfig.appvms.${vmDef.name} or { }; effectiveDef = vmDef - // lib.optionalAttrs ((vmCfg.ramMb or null) != null) { inherit (vmCfg) ramMb; } - // lib.optionalAttrs ((vmCfg.cores or null) != null) { inherit (vmCfg) cores; }; + // lib.optionalAttrs ((vmCfg.mem or null) != null) { inherit (vmCfg) mem; } + // lib.optionalAttrs ((vmCfg.vcpu or null) != null) { inherit (vmCfg) vcpu; } + // lib.optionalAttrs ((vmCfg.balloonRatio or null) != null) { inherit (vmCfg) balloonRatio; }; in lib.nixosSystem { modules = [ diff --git a/modules/reference/appvms/business.nix b/modules/reference/appvms/business.nix index 39a898312b..f018cea123 100644 --- a/modules/reference/appvms/business.nix +++ b/modules/reference/appvms/business.nix @@ -133,7 +133,7 @@ in # (reference appvms use laptop-x86.mkAppVm which doesn't exist on other profiles like Orin) config = lib.mkIf (cfg.enable && config.ghaf.profiles.laptop-x86.enable or false) { # DRY: Only enable, evaluatedConfig, and usbPassthrough at host level. - # All values (name, ramMb, borderColor, applications, vtpm) are derived from vmDef. + # All values (name, mem, borderColor, applications, vtpm) are derived from vmDef. ghaf.virtualization.microvm.appvm.vms.business = { enable = lib.mkDefault true; @@ -149,8 +149,8 @@ in evaluatedConfig = config.ghaf.profiles.laptop-x86.mkAppVm { name = "business"; packages = optionals config.ghaf.profiles.debug.enable [ pkgs.tcpdump ]; - ramMb = 6144; - cores = 4; + mem = 6144; + vcpu = 4; borderColor = "#218838"; ghafAudio.enable = lib.mkDefault true; vtpm = { diff --git a/modules/reference/appvms/chromium.nix b/modules/reference/appvms/chromium.nix index 68bf5dbed0..773dc30faf 100644 --- a/modules/reference/appvms/chromium.nix +++ b/modules/reference/appvms/chromium.nix @@ -23,15 +23,15 @@ in # (reference appvms use laptop-x86.mkAppVm which doesn't exist on other profiles like Orin) config = lib.mkIf (cfg.enable && config.ghaf.profiles.laptop-x86.enable or false) { # DRY: Only enable and evaluatedConfig at host level. - # All values (name, ramMb, borderColor, applications, vtpm) are derived from vmDef. + # All values (name, mem, borderColor, applications, vtpm) are derived from vmDef. ghaf.virtualization.microvm.appvm.vms.chromium = { enable = lib.mkDefault false; evaluatedConfig = config.ghaf.profiles.laptop-x86.mkAppVm { name = "chromium"; packages = lib.optional config.ghaf.development.debug.tools.enable pkgs.alsa-utils; - ramMb = 6144; - cores = 4; + mem = 6144; + vcpu = 4; borderColor = "#9C0000"; ghafAudio.enable = lib.mkDefault true; vtpm = { diff --git a/modules/reference/appvms/comms.nix b/modules/reference/appvms/comms.nix index 83a66bb387..9c377a8693 100644 --- a/modules/reference/appvms/comms.nix +++ b/modules/reference/appvms/comms.nix @@ -23,7 +23,7 @@ in # (reference appvms use laptop-x86.mkAppVm which doesn't exist on other profiles like Orin) config = lib.mkIf (cfg.enable && config.ghaf.profiles.laptop-x86.enable or false) { # DRY: Only enable and evaluatedConfig at host level. - # All values (name, ramMb, borderColor, applications, vtpm) are derived from vmDef. + # All values (name, mem, borderColor, applications, vtpm) are derived from vmDef. ghaf.virtualization.microvm.appvm.vms.comms = { enable = lib.mkDefault true; @@ -34,8 +34,8 @@ in pkgs.gpsd ] ++ lib.optionals config.ghaf.profiles.debug.enable [ pkgs.tcpdump ]; - ramMb = 4096; - cores = 4; + mem = 4096; + vcpu = 4; borderColor = "#337aff"; ghafAudio.enable = lib.mkDefault true; vtpm = { diff --git a/modules/reference/appvms/flatpak.nix b/modules/reference/appvms/flatpak.nix index 2f5b359806..311021e0ca 100644 --- a/modules/reference/appvms/flatpak.nix +++ b/modules/reference/appvms/flatpak.nix @@ -137,14 +137,14 @@ in # (reference appvms use laptop-x86.mkAppVm which doesn't exist on other profiles like Orin) config = lib.mkIf (cfg.enable && config.ghaf.profiles.laptop-x86.enable or false) { # DRY: Only enable and evaluatedConfig at host level. - # All values (name, ramMb, borderColor, applications, vtpm) are derived from vmDef. + # All values (name, mem, borderColor, applications, vtpm) are derived from vmDef. ghaf.virtualization.microvm.appvm.vms.flatpak = { enable = lib.mkDefault true; evaluatedConfig = config.ghaf.profiles.laptop-x86.mkAppVm { name = "flatpak"; - ramMb = 6144; - cores = 4; + mem = 6144; + vcpu = 4; bootPriority = "low"; borderColor = "#FFA500"; ghafAudio.enable = lib.mkDefault true; diff --git a/modules/reference/appvms/gala.nix b/modules/reference/appvms/gala.nix index a27d497a8d..de2f83eb5c 100644 --- a/modules/reference/appvms/gala.nix +++ b/modules/reference/appvms/gala.nix @@ -23,14 +23,14 @@ in # (reference appvms use laptop-x86.mkAppVm which doesn't exist on other profiles like Orin) config = lib.mkIf (cfg.enable && config.ghaf.profiles.laptop-x86.enable or false) { # DRY: Only enable and evaluatedConfig at host level. - # All values (name, ramMb, borderColor, applications, vtpm) are derived from vmDef. + # All values (name, mem, borderColor, applications, vtpm) are derived from vmDef. ghaf.virtualization.microvm.appvm.vms.gala = { enable = lib.mkDefault true; evaluatedConfig = config.ghaf.profiles.laptop-x86.mkAppVm { name = "gala"; - ramMb = 1536; - cores = 2; + mem = 1536; + vcpu = 2; bootPriority = "low"; borderColor = "#027d7b"; vtpm = { diff --git a/modules/reference/appvms/google-chrome.nix b/modules/reference/appvms/google-chrome.nix index 6a81004ad2..0368b27909 100644 --- a/modules/reference/appvms/google-chrome.nix +++ b/modules/reference/appvms/google-chrome.nix @@ -53,7 +53,7 @@ in # (reference appvms use laptop-x86.mkAppVm which doesn't exist on other profiles like Orin) config = lib.mkIf (cfg.enable && config.ghaf.profiles.laptop-x86.enable or false) { # DRY: Only enable, evaluatedConfig, and usbPassthrough at host level. - # All values (name, ramMb, borderColor, applications, vtpm) are derived from vmDef. + # All values (name, mem, borderColor, applications, vtpm) are derived from vmDef. ghaf.virtualization.microvm.appvm.vms.chrome = { enable = lib.mkDefault true; @@ -78,8 +78,8 @@ in evaluatedConfig = config.ghaf.profiles.laptop-x86.mkAppVm { name = "chrome"; packages = lib.optional config.ghaf.development.debug.tools.enable pkgs.alsa-utils; - ramMb = 6144; - cores = 4; + mem = 6144; + vcpu = 4; borderColor = "#9C0000"; ghafAudio.enable = lib.mkDefault true; vtpm = { diff --git a/modules/reference/appvms/zathura.nix b/modules/reference/appvms/zathura.nix index 26093a9c17..6a36b3ffa5 100644 --- a/modules/reference/appvms/zathura.nix +++ b/modules/reference/appvms/zathura.nix @@ -23,14 +23,14 @@ in # (reference appvms use laptop-x86.mkAppVm which doesn't exist on other profiles like Orin) config = lib.mkIf (cfg.enable && config.ghaf.profiles.laptop-x86.enable or false) { # DRY: Only enable and evaluatedConfig at host level. - # All values (name, ramMb, borderColor, applications, vtpm) are derived from vmDef. + # All values (name, mem, borderColor, applications, vtpm) are derived from vmDef. ghaf.virtualization.microvm.appvm.vms.zathura = { enable = lib.mkDefault true; evaluatedConfig = config.ghaf.profiles.laptop-x86.mkAppVm { name = "zathura"; - ramMb = 512; - cores = 1; + mem = 512; + vcpu = 1; bootPriority = "low"; borderColor = "#122263"; vtpm = { diff --git a/targets/laptop/flake-module.nix b/targets/laptop/flake-module.nix index 20e6941b6c..6c1d9b6a97 100644 --- a/targets/laptop/flake-module.nix +++ b/targets/laptop/flake-module.nix @@ -104,7 +104,7 @@ let }; vmConfig = { guivm.mem = 6144; - appvms.flatpak.ramMb = 5120; + appvms.flatpak.mem = 5120; }; }) @@ -148,7 +148,7 @@ let }; vmConfig = { guivm.mem = 6144; - appvms.flatpak.ramMb = 5120; + appvms.flatpak.mem = 5120; }; }) @@ -390,7 +390,7 @@ let }; vmConfig = { guivm.mem = 6144; - appvms.flatpak.ramMb = 5120; + appvms.flatpak.mem = 5120; }; }) @@ -420,7 +420,7 @@ let }; vmConfig = { guivm.mem = 6144; - appvms.flatpak.ramMb = 5120; + appvms.flatpak.mem = 5120; }; }) diff --git a/targets/vm/flake-module.nix b/targets/vm/flake-module.nix index 661f89b84e..bd444780b5 100644 --- a/targets/vm/flake-module.nix +++ b/targets/vm/flake-module.nix @@ -136,8 +136,8 @@ let # Create evaluatedConfig with waypipe disabled (no guivm) evaluatedConfig = vmProfile.mkAppVm { name = "zathura"; - ramMb = 512; - cores = 1; + mem = 512; + vcpu = 1; borderColor = "#122263"; # Dark blue — security context indicator waypipe.enable = false; # No guivm, so no waypipe applications = [