Skip to content

Instantly share code, notes, and snippets.

@andi242
Last active October 25, 2025 07:29
Show Gist options
  • Select an option

  • Save andi242/85349311e4ededea899f0b6635bceb48 to your computer and use it in GitHub Desktop.

Select an option

Save andi242/85349311e4ededea899f0b6635bceb48 to your computer and use it in GitHub Desktop.
basic nix vm

basic nix vm

put both files in a dir.

├── config.nix
└── flake.nix
  • build or rebuild the vm: nixos-rebuild build-vm --flake .#nixos
    • (might need nix.settings.experimental-features = [ "nix-command" "flakes" ]; in system config
      or append --extra-experimental-features nix-command --extra-experimental-features flakes to the command)
  • run the vm: ./result/bin/run-nixos-vm
  • creates: result -> /nix/store/hash-vm and nixos.qcow2
  • reset vm: rm result nixos.qcow2
    • nixos.qcow2 stores all the user settings/files.
{ config, lib, pkgs, ... }:
{
boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;
boot.kernelPackages = pkgs.linuxPackages_6_12;
virtualisation.vmVariant = {
virtualisation = {
qemu.options = [ "-device virtio-vga -audio model=hda,driver=pipewire" ];
memorySize = 6000;
cores = 6;
diskSize = 20000;
forwardPorts = [
{
from = "host";
host.port = 2221;
guest.port = 22;
}
];
};
};
networking.hostName = "nixos";
networking.networkmanager.enable = true;
time.timeZone = "Europe/Berlin";
environment.systemPackages = with pkgs; [
ghostty
fastfetch
firefox
];
services.displayManager.autoLogin.user = "user"; #auto login the user
services.displayManager.gdm.enable = true;
services.displayManager.gdm.autoSuspend = false;
services.desktopManager.gnome.enable = true;
users.users.user = {
isNormalUser = true;
password = "12345"; # password for VM testing only
extraGroups = [ "wheel" ];
};
security.sudo.wheelNeedsPassword = false; # no password auth when sudo
nix.settings.experimental-features = [ "nix-command" "flakes" ];
system.stateVersion = "25.05";
}
{
description = "NixOS basic VM";
inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs, home-manager, ... }: {
nixosConfigurations.nixos = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./config.nix
];
};
};
}
@andi242
Copy link
Author

andi242 commented Oct 25, 2025

virtualisation.forwardPorts = [
  {
    from = "host";
    host.port = 2221;
    guest.port = 22;
  }
];

that should add ssh port forwarding to the VM. ssh localhost:2221

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment