Skip to content

feat: mem and vcpu configurable#7

Merged
systemstart merged 1 commit into
mainfrom
feat/configurable-mem-vcpu
Jun 4, 2026
Merged

feat: mem and vcpu configurable#7
systemstart merged 1 commit into
mainfrom
feat/configurable-mem-vcpu

Conversation

@systemstart

@systemstart systemstart commented Jun 4, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • New Features
    • VM resources are now fully configurable at runtime without requiring rebuilds. Memory, vCPU count, and root disk size can be dynamically adjusted using VM_MEM and VM_VCPU environment variables, providing greater flexibility for different deployment scenarios and varying workload requirements. Default configuration: 8192 MB memory and 4 vCPUs.

@coderabbitai

coderabbitai Bot commented Jun 4, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

VM memory and vCPU sizing becomes configurable via NixOS module options with environment variable overrides. The module exposes claude-vm.agent.mem and claude-vm.agent.vcpu options, the runner script template reads VM_MEM and VM_VCPU for runtime overrides, and the flake wires configured values through to the generated runner.

Changes

VM Sizing Configuration

Layer / File(s) Summary
Module agent sizing options
modules/base.nix
Options claude-vm.agent.mem and claude-vm.agent.vcpu are added with defaults (8192 MB, 4 vCPUs) and descriptions indicating environment variable overrides; MicroVM config updated to use the new options instead of hardcoded values.
Runner script parameters for runtime sizing
flake.nix
mkRunnerScript function signature extended with defaultMem and defaultVcpu parameters; generated script defines VM_MEM and VM_VCPU sourcing from environment variables with defaults; sed replacements added to inject these into QEMU runner -m, -smp, and disk size= arguments.
Package construction integration
flake.nix
Package building extracts mem and vcpu from NixOS configuration and passes them to mkRunnerScript as defaultMem and defaultVcpu.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

A rabbit hops through memory lanes,
Where configs bloom and vCPUs dance,
Environment winds carry override refrains,
Sizing now flows at runtime's glance! 🐰✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: mem and vcpu configurable' directly and clearly summarizes the main change—making memory and vCPU settings configurable across the codebase.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/configurable-mem-vcpu

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@flake.nix`:
- Around line 35-36: Validate VM_MEM and VM_VCPU at runtime before passing them
to QEMU: check that VM_MEM and VM_VCPU (used in flake.nix where
VM_MEM="''${VM_MEM:-${toString defaultMem}}", VM_VCPU="''${VM_VCPU:-${toString
defaultVcpu}}") are positive integers (numeric, >0) and fall back to the default
values (defaultMem/defaultVcpu) or fail fast with a clear error if they are
invalid; implement the check using a Nix expression (e.g., builtins.match or
parseInt + comparisons) and replace the direct interpolations with the
validated/normalized variables so non-numeric, zero or negative inputs are
rejected or replaced before being handed to QEMU.

In `@modules/base.nix`:
- Around line 25-34: The mem and vcpu options currently accept any integer
(including <=0); add an option-level validation on both the mem and vcpu
lib.mkOption blocks to reject non-positive values (e.g. validation = v: if v > 0
then true else "mem must be a positive integer (MB)" and similarly for vcpu:
"vcpu must be a positive integer"); update the error strings to clearly name the
option being validated so configuration-time errors are helpful.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 518ba4c0-ac77-4cd5-801a-46a837fa7bed

📥 Commits

Reviewing files that changed from the base of the PR and between 9a47e30 and 693e433.

📒 Files selected for processing (2)
  • flake.nix
  • modules/base.nix

Comment thread flake.nix
Comment on lines +35 to +36
VM_MEM="''${VM_MEM:-${toString defaultMem}}"
VM_VCPU="''${VM_VCPU:-${toString defaultVcpu}}"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Add runtime validation for VM_MEM and VM_VCPU.

The script reads VM_MEM and VM_VCPU from the environment without validation. Invalid values (non-numeric, negative, zero) will be passed to QEMU, potentially causing confusing errors.

🛡️ Proposed validation
 VM_MEM="''${VM_MEM:-${toString defaultMem}}"
 VM_VCPU="''${VM_VCPU:-${toString defaultVcpu}}"
+
+# Validate VM_MEM is a positive integer
+if ! [[ "$VM_MEM" =~ ^[0-9]+$ ]] || [ "$VM_MEM" -le 0 ]; then
+  echo "error: VM_MEM must be a positive integer (got: $VM_MEM)" >&2
+  exit 1
+fi
+
+# Validate VM_VCPU is a positive integer
+if ! [[ "$VM_VCPU" =~ ^[0-9]+$ ]] || [ "$VM_VCPU" -le 0 ]; then
+  echo "error: VM_VCPU must be a positive integer (got: $VM_VCPU)" >&2
+  exit 1
+fi
+
 RUNTIME="''${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@flake.nix` around lines 35 - 36, Validate VM_MEM and VM_VCPU at runtime
before passing them to QEMU: check that VM_MEM and VM_VCPU (used in flake.nix
where VM_MEM="''${VM_MEM:-${toString defaultMem}}",
VM_VCPU="''${VM_VCPU:-${toString defaultVcpu}}") are positive integers (numeric,
>0) and fall back to the default values (defaultMem/defaultVcpu) or fail fast
with a clear error if they are invalid; implement the check using a Nix
expression (e.g., builtins.match or parseInt + comparisons) and replace the
direct interpolations with the validated/normalized variables so non-numeric,
zero or negative inputs are rejected or replaced before being handed to QEMU.

Comment thread modules/base.nix
Comment on lines +25 to +34
mem = lib.mkOption {
type = lib.types.int;
default = 8192;
description = "VM memory in MB. Overridable at runtime via VM_MEM env var.";
};
vcpu = lib.mkOption {
type = lib.types.int;
default = 4;
description = "VM vCPU count. Overridable at runtime via VM_VCPU env var.";
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Add validation to ensure positive values.

The mem and vcpu options accept any integer, including negative or zero values. While QEMU would reject these at runtime, adding Nix-level validation provides clearer error messages during configuration.

✅ Proposed validation
     mem = lib.mkOption {
       type = lib.types.int;
       default = 8192;
       description = "VM memory in MB. Overridable at runtime via VM_MEM env var.";
     };
+    # Validate mem is positive
+    assertions = [{
+      assertion = cfg.mem > 0;
+      message = "claude-vm.agent.mem must be greater than 0";
+    }];
+
     vcpu = lib.mkOption {
       type = lib.types.int;
       default = 4;
       description = "VM vCPU count. Overridable at runtime via VM_VCPU env var.";
     };
+    # Validate vcpu is positive  
+    assertions = [{
+      assertion = cfg.vcpu > 0;
+      message = "claude-vm.agent.vcpu must be greater than 0";
+    }];

Alternatively, use a more restrictive type:

     mem = lib.mkOption {
-      type = lib.types.int;
+      type = lib.types.ints.positive;
       default = 8192;
       description = "VM memory in MB. Overridable at runtime via VM_MEM env var.";
     };
     vcpu = lib.mkOption {
-      type = lib.types.int;
+      type = lib.types.ints.positive;
       default = 4;
       description = "VM vCPU count. Overridable at runtime via VM_VCPU env var.";
     };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
mem = lib.mkOption {
type = lib.types.int;
default = 8192;
description = "VM memory in MB. Overridable at runtime via VM_MEM env var.";
};
vcpu = lib.mkOption {
type = lib.types.int;
default = 4;
description = "VM vCPU count. Overridable at runtime via VM_VCPU env var.";
};
mem = lib.mkOption {
type = lib.types.ints.positive;
default = 8192;
description = "VM memory in MB. Overridable at runtime via VM_MEM env var.";
};
vcpu = lib.mkOption {
type = lib.types.ints.positive;
default = 4;
description = "VM vCPU count. Overridable at runtime via VM_VCPU env var.";
};
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@modules/base.nix` around lines 25 - 34, The mem and vcpu options currently
accept any integer (including <=0); add an option-level validation on both the
mem and vcpu lib.mkOption blocks to reject non-positive values (e.g. validation
= v: if v > 0 then true else "mem must be a positive integer (MB)" and similarly
for vcpu: "vcpu must be a positive integer"); update the error strings to
clearly name the option being validated so configuration-time errors are
helpful.

@systemstart systemstart merged commit 18c95e4 into main Jun 4, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant