Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/src/content/docs/ghaf/dev/guides/creating-vms.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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, ... }:
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions docs/src/content/docs/ghaf/dev/ref/creating_appvm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
8 changes: 4 additions & 4 deletions modules/microvm/appvm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down Expand Up @@ -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 [ ];
Expand Down Expand Up @@ -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.
'';
};

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions modules/microvm/host/mem-manager.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)}";
};
};
Expand Down
6 changes: 3 additions & 3 deletions modules/microvm/sysvms/appvm-base.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [
Expand Down
20 changes: 15 additions & 5 deletions modules/microvm/vm-config.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [ ];
Expand Down Expand Up @@ -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; };
}
'';
};
Expand Down
7 changes: 4 additions & 3 deletions modules/profiles/laptop-x86.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
6 changes: 3 additions & 3 deletions modules/reference/appvms/business.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 = {
Expand Down
6 changes: 3 additions & 3 deletions modules/reference/appvms/chromium.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
6 changes: 3 additions & 3 deletions modules/reference/appvms/comms.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 = {
Expand Down
6 changes: 3 additions & 3 deletions modules/reference/appvms/flatpak.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions modules/reference/appvms/gala.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
6 changes: 3 additions & 3 deletions modules/reference/appvms/google-chrome.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 = {
Expand Down
6 changes: 3 additions & 3 deletions modules/reference/appvms/zathura.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
8 changes: 4 additions & 4 deletions targets/laptop/flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ let
};
vmConfig = {
guivm.mem = 6144;
appvms.flatpak.ramMb = 5120;
appvms.flatpak.mem = 5120;
};
})

Expand Down Expand Up @@ -148,7 +148,7 @@ let
};
vmConfig = {
guivm.mem = 6144;
appvms.flatpak.ramMb = 5120;
appvms.flatpak.mem = 5120;
};
})

Expand Down Expand Up @@ -390,7 +390,7 @@ let
};
vmConfig = {
guivm.mem = 6144;
appvms.flatpak.ramMb = 5120;
appvms.flatpak.mem = 5120;
};
})

Expand Down Expand Up @@ -420,7 +420,7 @@ let
};
vmConfig = {
guivm.mem = 6144;
appvms.flatpak.ramMb = 5120;
appvms.flatpak.mem = 5120;
};
})

Expand Down
4 changes: 2 additions & 2 deletions targets/vm/flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
Loading