Files
claude-vm/default.nix
T
2026-03-10 17:21:34 +00:00

111 lines
2.8 KiB
Nix

let
sources = import ./npins;
system = builtins.currentSystem;
pkgs = import sources.nixpkgs {
inherit system;
config.allowUnfree = true;
};
claudeCode = pkgs.callPackage "${sources.claude-code-nix}/package.nix" { };
nixosCfg = import "${sources.nixpkgs}/nixos/lib/eval-config.nix" {
inherit system;
# specialArgs = {
# inherit sources;
# };
modules = [
{ nixpkgs.pkgs = pkgs; }
(
{ modulesPath, ... }:
{
imports = [ "${modulesPath}/virtualisation/qemu-vm.nix" ];
# virtualisation
virtualisation.graphics = false;
virtualisation.memorySize = 4096;
virtualisation.cores = 4;
virtualisation.sharedDirectories = {
workspace = {
source = ''"$WORKSPACE_DIR"'';
target = "/workspace";
securityModel = "none";
};
config = {
source = ''"$CLAUDE_VM_CONFIG_DIR"'';
target = "/mtn/claude-vm-config";
securityModel = "none";
};
};
# boot / console
boot.kernelParams = [ "console=ttyS0" ];
boot.loader.grub.enable = false;
boot.initrd.kernelModules = [
"9p"
"9pnet_virtio"
];
# user
users.users.claude = {
isNormalUser = true;
home = "/home/claude";
extraGroups = [ "wheel" ];
};
services.getty.autologinUser = "claude";
# packages
environment.systemPackages = [
claudeCode
pkgs.git
pkgs.curl
pkgs.vim
];
# login shell launches claude
programs.bash.interactiveShellInit = ''
# Only run for the auto-login claude user
[ "$(whoami)" = "claude" ] || return
args=()
if [ -f /mnt/claude-vm-config/claude-args ]; then
while IFS= read -r line; do
[ -n "$line" ] && args+=("$line")
done < /mnt/claude-vm-config/claude-args
fi
cd /workspace 2>/dev/null || true
exec claude "''${args[@]}"
'';
# misc
networking.hostName = "claude-vm";
system.stateVersion = "25.05";
}
)
];
};
vmBuild = nixosCfg.config.system.build.vm;
in
{
claude-vm = pkgs.writeShellScriptBin "claude-vm" ''
CONFIG_DIR=$(mktemp -d)
trap "rm -rf '$CONFIG_DIR'" EXIT
# Write all CLI args to a file, one per line
if [ $# -gt 0 ]; then
printf '%s\n' "$@" > "$CONFIG_DIR/claude-args"
else
touch "$CONFIG_DIR/claude-args"
fi
export CLAUDE_VM_CONFIG_DIR="$CONFIG_DIR"
export WORKSPACE_DIR="$(pwd)"
${vmBuild}/bin/run-claude-vm-vm
'';
}