feat: mem and vcpu configurable#7
Conversation
📝 WalkthroughWalkthroughVM memory and vCPU sizing becomes configurable via NixOS module options with environment variable overrides. The module exposes ChangesVM Sizing Configuration
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
flake.nixmodules/base.nix
| VM_MEM="''${VM_MEM:-${toString defaultMem}}" | ||
| VM_VCPU="''${VM_VCPU:-${toString defaultVcpu}}" |
There was a problem hiding this comment.
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.
| 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."; | ||
| }; |
There was a problem hiding this comment.
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.
| 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.
Summary by CodeRabbit
VM_MEMandVM_VCPUenvironment variables, providing greater flexibility for different deployment scenarios and varying workload requirements. Default configuration: 8192 MB memory and 4 vCPUs.