Add sandboxed-workspace module for isolated dev environments
Provides isolated development environments using either VMs (microvm.nix)
or containers (systemd-nspawn) with a unified configuration interface.
Features:
- Unified options with required type field ("vm" or "container")
- Shared base configuration for networking, SSH, users, packages
- Automatic SSH host key generation and persistence
- Shell aliases for workspace management (start/stop/status/ssh)
- Automatic /etc/hosts entries for workspace hostnames
- restartIfChanged support for both VMs and containers
- Passwordless doas in workspaces
Container backend:
- Uses hostBridge for proper bridge networking with /24 subnet
- systemd-networkd for IP configuration
- systemd-resolved for DNS
VM backend:
- TAP interface with deterministic MAC addresses
- virtiofs shares for workspace directories
- vsock CID generation
This commit is contained in:
114
common/sandboxed-workspace/base.nix
Normal file
114
common/sandboxed-workspace/base.nix
Normal file
@@ -0,0 +1,114 @@
|
||||
{ hostConfig, workspaceName, ip, networkInterface }:
|
||||
|
||||
# Base configuration shared by all sandboxed workspaces (VMs and containers)
|
||||
# This provides common settings for networking, SSH, users, and packages
|
||||
#
|
||||
# Parameters:
|
||||
# hostConfig - The host's NixOS config (for inputs, ssh keys, etc.)
|
||||
# workspaceName - Name of the workspace (used as hostname)
|
||||
# ip - Static IP address for the workspace
|
||||
# networkInterface - Match config for systemd-networkd (e.g., { Type = "ether"; } or { Name = "host0"; })
|
||||
|
||||
{ config, lib, pkgs, ... }:
|
||||
|
||||
{
|
||||
imports = [
|
||||
../shell.nix
|
||||
hostConfig.inputs.home-manager.nixosModules.home-manager
|
||||
hostConfig.inputs.nix-index-database.nixosModules.default
|
||||
];
|
||||
|
||||
# Basic system configuration
|
||||
system.stateVersion = "25.11";
|
||||
|
||||
# Set hostname to match the workspace name
|
||||
networking.hostName = workspaceName;
|
||||
|
||||
# Networking with systemd-networkd
|
||||
networking.useNetworkd = true;
|
||||
systemd.network.enable = true;
|
||||
|
||||
# Enable resolved to populate /etc/resolv.conf from networkd's DNS settings
|
||||
services.resolved.enable = true;
|
||||
|
||||
# Basic networking configuration
|
||||
networking.useDHCP = false;
|
||||
|
||||
# Static IP configuration
|
||||
# Uses the host as DNS server (host forwards to upstream DNS)
|
||||
systemd.network.networks."20-workspace" = {
|
||||
matchConfig = networkInterface;
|
||||
networkConfig = {
|
||||
Address = "${ip}/24";
|
||||
Gateway = hostConfig.networking.sandbox.hostAddress;
|
||||
DNS = [ hostConfig.networking.sandbox.hostAddress ];
|
||||
};
|
||||
};
|
||||
|
||||
# Disable firewall inside workspaces (we're behind NAT)
|
||||
networking.firewall.enable = false;
|
||||
|
||||
# Enable SSH for access
|
||||
services.openssh = {
|
||||
enable = true;
|
||||
settings = {
|
||||
PasswordAuthentication = false;
|
||||
PermitRootLogin = "prohibit-password";
|
||||
};
|
||||
};
|
||||
|
||||
# Use persistent SSH host keys from shared directory
|
||||
services.openssh.hostKeys = lib.mkForce [
|
||||
{
|
||||
path = "/etc/ssh-host-keys/ssh_host_ed25519_key";
|
||||
type = "ed25519";
|
||||
}
|
||||
];
|
||||
|
||||
# Basic system packages
|
||||
environment.systemPackages = with pkgs; [
|
||||
kakoune
|
||||
vim
|
||||
git
|
||||
htop
|
||||
wget
|
||||
curl
|
||||
tmux
|
||||
dnsutils
|
||||
];
|
||||
|
||||
# User configuration
|
||||
users.mutableUsers = false;
|
||||
users.users.googlebot = {
|
||||
isNormalUser = true;
|
||||
extraGroups = [ "wheel" ];
|
||||
shell = pkgs.fish;
|
||||
openssh.authorizedKeys.keys = hostConfig.machines.ssh.userKeys;
|
||||
};
|
||||
|
||||
security.doas.enable = true;
|
||||
security.sudo.enable = false;
|
||||
security.doas.extraRules = [
|
||||
{ groups = [ "wheel" ]; noPass = true; }
|
||||
];
|
||||
|
||||
# Minimal locale settings
|
||||
i18n.defaultLocale = "en_US.UTF-8";
|
||||
time.timeZone = "America/Los_Angeles";
|
||||
|
||||
# Enable flakes
|
||||
nix.settings.experimental-features = [ "nix-command" "flakes" ];
|
||||
|
||||
# Make nixpkgs available in NIX_PATH and registry (like the NixOS ISO)
|
||||
# This allows `nix-shell -p`, `nix repl '<nixpkgs>'`, etc. to work
|
||||
nix.nixPath = [ "nixpkgs=${hostConfig.inputs.nixpkgs}" ];
|
||||
nix.registry.nixpkgs.flake = hostConfig.inputs.nixpkgs;
|
||||
|
||||
# Enable fish shell
|
||||
programs.fish.enable = true;
|
||||
|
||||
# Home Manager configuration
|
||||
home-manager.useGlobalPkgs = true;
|
||||
home-manager.useUserPackages = true;
|
||||
home-manager.users.googlebot = import ./home.nix;
|
||||
}
|
||||
72
common/sandboxed-workspace/container.nix
Normal file
72
common/sandboxed-workspace/container.nix
Normal file
@@ -0,0 +1,72 @@
|
||||
{ config, lib, ... }:
|
||||
|
||||
# Container-specific configuration for sandboxed workspaces using systemd-nspawn
|
||||
# This module is imported by default.nix for workspaces with type = "container"
|
||||
|
||||
with lib;
|
||||
|
||||
let
|
||||
cfg = config.sandboxed-workspace;
|
||||
hostConfig = config;
|
||||
|
||||
# Filter for container-type workspaces only
|
||||
containerWorkspaces = filterAttrs (n: ws: ws.type == "container") cfg.workspaces;
|
||||
in
|
||||
{
|
||||
config = mkIf (cfg.enable && containerWorkspaces != { }) {
|
||||
# NixOS container module only sets restartIfChanged when autoStart=true
|
||||
# Work around this by setting it directly on the systemd service
|
||||
systemd.services = mapAttrs'
|
||||
(name: ws: nameValuePair "container@${name}" {
|
||||
restartIfChanged = lib.mkForce true;
|
||||
restartTriggers = [
|
||||
config.containers.${name}.path
|
||||
config.environment.etc."nixos-containers/${name}.conf".source
|
||||
];
|
||||
})
|
||||
containerWorkspaces;
|
||||
|
||||
# Convert container workspace configs to NixOS containers format
|
||||
containers = mapAttrs
|
||||
(name: ws: {
|
||||
autoStart = ws.autoStart;
|
||||
privateNetwork = true;
|
||||
ephemeral = true;
|
||||
restartIfChanged = true;
|
||||
|
||||
# Attach container's veth to the sandbox bridge
|
||||
# This creates the veth pair and attaches host side to the bridge
|
||||
hostBridge = config.networking.sandbox.bridgeName;
|
||||
|
||||
bindMounts = {
|
||||
"/home/googlebot/workspace" = {
|
||||
hostPath = "/home/googlebot/sandboxed/${name}/workspace";
|
||||
isReadOnly = false;
|
||||
};
|
||||
"/etc/ssh-host-keys" = {
|
||||
hostPath = "/home/googlebot/sandboxed/${name}/ssh-host-keys";
|
||||
isReadOnly = false;
|
||||
};
|
||||
"/home/googlebot/claude-config" = {
|
||||
hostPath = "/home/googlebot/sandboxed/${name}/claude-config";
|
||||
isReadOnly = false;
|
||||
};
|
||||
};
|
||||
|
||||
config = { config, lib, pkgs, ... }: {
|
||||
imports = [
|
||||
(import ./base.nix {
|
||||
inherit hostConfig;
|
||||
workspaceName = name;
|
||||
ip = ws.ip;
|
||||
networkInterface = { Name = "eth0"; };
|
||||
})
|
||||
(import ws.config)
|
||||
];
|
||||
|
||||
networking.useHostResolvConf = false;
|
||||
};
|
||||
})
|
||||
containerWorkspaces;
|
||||
};
|
||||
}
|
||||
157
common/sandboxed-workspace/default.nix
Normal file
157
common/sandboxed-workspace/default.nix
Normal file
@@ -0,0 +1,157 @@
|
||||
{ config, lib, pkgs, ... }:
|
||||
|
||||
# Unified sandboxed workspace module supporting both VMs and containers
|
||||
# This module provides isolated development environments with shared configuration
|
||||
|
||||
with lib;
|
||||
|
||||
let
|
||||
cfg = config.sandboxed-workspace;
|
||||
in
|
||||
{
|
||||
imports = [
|
||||
./vm.nix
|
||||
./container.nix
|
||||
];
|
||||
|
||||
options.sandboxed-workspace = {
|
||||
enable = mkEnableOption "sandboxed workspace management";
|
||||
|
||||
workspaces = mkOption {
|
||||
type = types.attrsOf (types.submodule {
|
||||
options = {
|
||||
type = mkOption {
|
||||
type = types.enum [ "vm" "container" ];
|
||||
description = ''
|
||||
Backend type for this workspace:
|
||||
- "vm": microVM with cloud-hypervisor (more isolation, uses virtiofs)
|
||||
- "container": systemd-nspawn container (less overhead, uses bind mounts)
|
||||
'';
|
||||
};
|
||||
|
||||
config = mkOption {
|
||||
type = types.path;
|
||||
description = "Path to the workspace configuration file";
|
||||
};
|
||||
|
||||
ip = mkOption {
|
||||
type = types.str;
|
||||
example = "192.168.83.10";
|
||||
description = ''
|
||||
Static IP address for this workspace on the microvm bridge network.
|
||||
Configures the workspace's network interface and adds an entry to /etc/hosts
|
||||
on the host so the workspace can be accessed by name (e.g., ssh workspace-example).
|
||||
Must be in the 192.168.83.0/24 subnet (or whatever networking.sandbox.subnet is).
|
||||
'';
|
||||
};
|
||||
|
||||
hostKey = mkOption {
|
||||
type = types.nullOr types.str;
|
||||
default = null;
|
||||
example = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA...";
|
||||
description = ''
|
||||
SSH host public key for this workspace. If set, adds to programs.ssh.knownHosts
|
||||
so the host automatically trusts the workspace without prompting.
|
||||
Get the key from: ~/sandboxed/<name>/ssh-host-keys/ssh_host_ed25519_key.pub
|
||||
'';
|
||||
};
|
||||
|
||||
autoStart = mkOption {
|
||||
type = types.bool;
|
||||
default = false;
|
||||
description = "Whether to automatically start this workspace on boot";
|
||||
};
|
||||
|
||||
cid = mkOption {
|
||||
type = types.nullOr types.int;
|
||||
default = null;
|
||||
description = ''
|
||||
vsock Context Identifier for this workspace (VM-only, ignored for containers).
|
||||
If null, auto-generated from workspace name.
|
||||
Must be unique per host. Valid range: 3 to 4294967294.
|
||||
See: https://man7.org/linux/man-pages/man7/vsock.7.html
|
||||
'';
|
||||
};
|
||||
};
|
||||
});
|
||||
default = { };
|
||||
description = "Sandboxed workspace configurations";
|
||||
};
|
||||
};
|
||||
|
||||
config = mkIf cfg.enable {
|
||||
# Automatically enable sandbox networking when workspaces are defined
|
||||
networking.sandbox.enable = mkIf (cfg.workspaces != { }) true;
|
||||
|
||||
# Add workspace hostnames to /etc/hosts so they can be accessed by name
|
||||
networking.hosts = lib.mkMerge (lib.mapAttrsToList
|
||||
(name: ws: {
|
||||
${ws.ip} = [ "workspace-${name}" ];
|
||||
})
|
||||
cfg.workspaces);
|
||||
|
||||
# Add workspace SSH host keys to known_hosts so host trusts workspaces without prompting
|
||||
programs.ssh.knownHosts = lib.mkMerge (lib.mapAttrsToList
|
||||
(name: ws:
|
||||
lib.optionalAttrs (ws.hostKey != null) {
|
||||
"workspace-${name}" = {
|
||||
publicKey = ws.hostKey;
|
||||
extraHostNames = [ ws.ip ];
|
||||
};
|
||||
})
|
||||
cfg.workspaces);
|
||||
|
||||
# Shell aliases for workspace management
|
||||
# Service names differ by type: microvm@<name> for VMs, container@<name> for containers
|
||||
environment.shellAliases = lib.mkMerge (lib.mapAttrsToList
|
||||
(name: ws:
|
||||
let
|
||||
serviceName = if ws.type == "vm" then "microvm@${name}" else "container@${name}";
|
||||
in
|
||||
{
|
||||
"workspace_${name}" = "ssh googlebot@workspace-${name}";
|
||||
"workspace_${name}_start" = "doas systemctl start ${serviceName}";
|
||||
"workspace_${name}_stop" = "doas systemctl stop ${serviceName}";
|
||||
"workspace_${name}_restart" = "doas systemctl restart ${serviceName}";
|
||||
"workspace_${name}_status" = "doas systemctl status ${serviceName}";
|
||||
})
|
||||
cfg.workspaces);
|
||||
|
||||
# Automatically generate SSH host keys and directories for all workspaces
|
||||
systemd.services = lib.mapAttrs'
|
||||
(name: ws:
|
||||
let
|
||||
serviceName = if ws.type == "vm" then "microvm@${name}" else "container@${name}";
|
||||
in
|
||||
lib.nameValuePair "workspace-${name}-setup" {
|
||||
description = "Setup directories and SSH keys for workspace ${name}";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
before = [ "${serviceName}.service" ];
|
||||
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
};
|
||||
|
||||
script = ''
|
||||
# Create directories if they don't exist
|
||||
mkdir -p /home/googlebot/sandboxed/${name}/workspace
|
||||
mkdir -p /home/googlebot/sandboxed/${name}/ssh-host-keys
|
||||
mkdir -p /home/googlebot/sandboxed/${name}/claude-config
|
||||
|
||||
# Fix ownership
|
||||
chown -R googlebot:users /home/googlebot/sandboxed/${name}
|
||||
|
||||
# Generate SSH host key if it doesn't exist
|
||||
if [ ! -f /home/googlebot/sandboxed/${name}/ssh-host-keys/ssh_host_ed25519_key ]; then
|
||||
${pkgs.openssh}/bin/ssh-keygen -t ed25519 -N "" \
|
||||
-f /home/googlebot/sandboxed/${name}/ssh-host-keys/ssh_host_ed25519_key
|
||||
chown googlebot:users /home/googlebot/sandboxed/${name}/ssh-host-keys/ssh_host_ed25519_key*
|
||||
echo "Generated SSH host key for workspace ${name}"
|
||||
fi
|
||||
'';
|
||||
}
|
||||
)
|
||||
cfg.workspaces;
|
||||
};
|
||||
}
|
||||
50
common/sandboxed-workspace/home.nix
Normal file
50
common/sandboxed-workspace/home.nix
Normal file
@@ -0,0 +1,50 @@
|
||||
{ config, lib, pkgs, ... }:
|
||||
|
||||
# Home Manager configuration for sandboxed workspace user environment
|
||||
# This sets up the shell and tools inside VMs and containers
|
||||
|
||||
{
|
||||
home.username = "googlebot";
|
||||
home.homeDirectory = "/home/googlebot";
|
||||
home.stateVersion = "24.11";
|
||||
|
||||
programs.home-manager.enable = true;
|
||||
|
||||
# Shell configuration
|
||||
programs.fish.enable = true;
|
||||
programs.starship.enable = true;
|
||||
programs.starship.enableFishIntegration = true;
|
||||
programs.starship.settings.container.disabled = true;
|
||||
|
||||
# Basic command-line tools
|
||||
programs.btop.enable = true;
|
||||
programs.ripgrep.enable = true;
|
||||
programs.eza.enable = true;
|
||||
|
||||
# Git configuration
|
||||
programs.git = {
|
||||
enable = true;
|
||||
settings = {
|
||||
user.name = lib.mkDefault "googlebot";
|
||||
user.email = lib.mkDefault "zuckerberg@neet.dev";
|
||||
};
|
||||
};
|
||||
|
||||
# Shell aliases
|
||||
home.shellAliases = {
|
||||
ls = "eza";
|
||||
la = "eza -la";
|
||||
ll = "eza -l";
|
||||
};
|
||||
|
||||
# Environment variables for Claude Code
|
||||
home.sessionVariables = {
|
||||
# Isolate Claude config to a specific directory on the host
|
||||
CLAUDE_CONFIG_DIR = "/home/googlebot/claude-config";
|
||||
};
|
||||
|
||||
# Additional packages for development
|
||||
home.packages = with pkgs; [
|
||||
# Add packages as needed per workspace
|
||||
];
|
||||
}
|
||||
138
common/sandboxed-workspace/vm.nix
Normal file
138
common/sandboxed-workspace/vm.nix
Normal file
@@ -0,0 +1,138 @@
|
||||
{ config, lib, ... }:
|
||||
|
||||
# VM-specific configuration for sandboxed workspaces using microvm.nix
|
||||
# This module is imported by default.nix for workspaces with type = "vm"
|
||||
|
||||
with lib;
|
||||
|
||||
let
|
||||
cfg = config.sandboxed-workspace;
|
||||
hostConfig = config;
|
||||
|
||||
# Generate a deterministic vsock CID from workspace name.
|
||||
#
|
||||
# vsock (virtual sockets) enables host-VM communication without networking.
|
||||
# cloud-hypervisor uses vsock for systemd-notify integration: when a VM finishes
|
||||
# booting, systemd sends READY=1 to the host via vsock, allowing the host's
|
||||
# microvm@ service to accurately track VM boot status instead of guessing.
|
||||
#
|
||||
# Each VM needs a unique CID (Context Identifier). Reserved CIDs per vsock(7):
|
||||
# - VMADDR_CID_HYPERVISOR (0): reserved for hypervisor
|
||||
# - VMADDR_CID_LOCAL (1): loopback address
|
||||
# - VMADDR_CID_HOST (2): host address
|
||||
# See: https://man7.org/linux/man-pages/man7/vsock.7.html
|
||||
# https://docs.kernel.org/virt/kvm/vsock.html
|
||||
#
|
||||
# We auto-generate from SHA256 hash to ensure uniqueness without manual assignment.
|
||||
# Range: 100 - 16777315 (offset avoids reserved CIDs and leaves 3-99 for manual use)
|
||||
nameToCid = name:
|
||||
let
|
||||
hash = builtins.hashString "sha256" name;
|
||||
hexPart = builtins.substring 0 6 hash;
|
||||
in
|
||||
100 + (builtins.foldl'
|
||||
(acc: c: acc * 16 + (
|
||||
if c == "a" then 10
|
||||
else if c == "b" then 11
|
||||
else if c == "c" then 12
|
||||
else if c == "d" then 13
|
||||
else if c == "e" then 14
|
||||
else if c == "f" then 15
|
||||
else lib.strings.toInt c
|
||||
)) 0
|
||||
(lib.stringToCharacters hexPart));
|
||||
|
||||
# Filter for VM-type workspaces only
|
||||
vmWorkspaces = filterAttrs (n: ws: ws.type == "vm") cfg.workspaces;
|
||||
|
||||
# Generate VM configuration for a workspace
|
||||
mkVmConfig = name: ws: {
|
||||
config = import ws.config;
|
||||
specialArgs = { inputs = hostConfig.inputs; };
|
||||
extraModules = [
|
||||
(import ./base.nix {
|
||||
inherit hostConfig;
|
||||
workspaceName = name;
|
||||
ip = ws.ip;
|
||||
networkInterface = { Type = "ether"; };
|
||||
})
|
||||
{
|
||||
# MicroVM specific configuration
|
||||
microvm = {
|
||||
# Use cloud-hypervisor for better performance
|
||||
hypervisor = lib.mkDefault "cloud-hypervisor";
|
||||
|
||||
# Resource allocation
|
||||
vcpu = 8;
|
||||
mem = 4096; # 4GB RAM
|
||||
|
||||
# Disk for writable overlay
|
||||
volumes = [{
|
||||
image = "overlay.img";
|
||||
mountPoint = "/nix/.rw-store";
|
||||
size = 8192; # 8GB
|
||||
}];
|
||||
|
||||
# Shared directories with host using virtiofs
|
||||
shares = [
|
||||
{
|
||||
# Share the host's /nix/store for accessing packages
|
||||
proto = "virtiofs";
|
||||
tag = "ro-store";
|
||||
source = "/nix/store";
|
||||
mountPoint = "/nix/.ro-store";
|
||||
}
|
||||
{
|
||||
proto = "virtiofs";
|
||||
tag = "workspace";
|
||||
source = "/home/googlebot/sandboxed/${name}/workspace";
|
||||
mountPoint = "/home/googlebot/workspace";
|
||||
}
|
||||
{
|
||||
proto = "virtiofs";
|
||||
tag = "ssh-host-keys";
|
||||
source = "/home/googlebot/sandboxed/${name}/ssh-host-keys";
|
||||
mountPoint = "/etc/ssh-host-keys";
|
||||
}
|
||||
{
|
||||
proto = "virtiofs";
|
||||
tag = "claude-config";
|
||||
source = "/home/googlebot/sandboxed/${name}/claude-config";
|
||||
mountPoint = "/home/googlebot/claude-config";
|
||||
}
|
||||
];
|
||||
|
||||
# Writeable overlay for /nix/store
|
||||
writableStoreOverlay = "/nix/.rw-store";
|
||||
|
||||
# TAP interface for bridged networking
|
||||
# The interface name "vm-*" matches the pattern in common/network/microvm.nix
|
||||
# which automatically attaches it to the microbr bridge
|
||||
interfaces = [{
|
||||
type = "tap";
|
||||
id = "vm-${name}";
|
||||
# Generate a deterministic MAC from workspace name (02: prefix = locally administered)
|
||||
mac =
|
||||
let
|
||||
hash = builtins.hashString "sha256" name;
|
||||
in
|
||||
"02:${builtins.substring 0 2 hash}:${builtins.substring 2 2 hash}:${builtins.substring 4 2 hash}:${builtins.substring 6 2 hash}:${builtins.substring 8 2 hash}";
|
||||
}];
|
||||
|
||||
# Enable vsock for systemd-notify integration
|
||||
vsock.cid =
|
||||
if ws.cid != null
|
||||
then ws.cid
|
||||
else nameToCid name;
|
||||
};
|
||||
}
|
||||
];
|
||||
autostart = ws.autoStart;
|
||||
};
|
||||
in
|
||||
{
|
||||
config = mkIf (cfg.enable && vmWorkspaces != { }) {
|
||||
# Convert VM workspace configs to microvm.nix format
|
||||
microvm.vms = mapAttrs mkVmConfig vmWorkspaces;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user