Skip to content

Instantly share code, notes, and snippets.

@FlakM
Last active October 28, 2025 15:21
Show Gist options
  • Save FlakM/0535b8aa7efec56906c5ab5e32580adf to your computer and use it in GitHub Desktop.
Save FlakM/0535b8aa7efec56906c5ab5e32580adf to your computer and use it in GitHub Desktop.

Revisions

  1. FlakM revised this gist Apr 8, 2024. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion nix vm using flake.md
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,8 @@ Did you know that it is rather easy to setup a VM to test your NixOs configurati
    { config, lib, pkgs, ... }: {
    # customize kernel version
    boot.kernelPackages = pkgs.linuxPackages_5_15;
    users.groups.admin = {};
    users.users = {
    admin = {
    isNormalUser = true;
  2. FlakM renamed this gist May 22, 2023. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions nix_vm.md → nix vm using flake.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    # Setting up qemu VM using nix flakes

    Did you know that it is rather easy to setup a VM to test your NixOs configuration?

    ## Create simple flake:

    ```nix
  3. FlakM revised this gist May 22, 2023. 1 changed file with 12 additions and 9 deletions.
    21 changes: 12 additions & 9 deletions nix_vm.md
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,13 @@
    ## Create simple flake:

    ```bash
    cat <<EOF > flake.nix
    ```nix
    # flake.nix
    {
    inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    outputs = { self, nixpkgs, ... }@attrs:
    outputs = { self, nixpkgs, ... }:
    let
    system = "x86_64-linux";
    pkgs = import nixpkgs { inherit system; };
    in
    {
    # test is a hostname for our machine
    @@ -20,13 +19,12 @@ cat <<EOF > flake.nix
    };
    };
    }
    EOF
    ```

    ## Add your configuration

    ```bash
    cat <<EOF > configuration.nix
    ```nix
    # configuration.nix
    { config, lib, pkgs, ... }: {
    # customize kernel version
    boot.kernelPackages = pkgs.linuxPackages_5_15;
    @@ -61,7 +59,6 @@ cat <<EOF > configuration.nix
    system.stateVersion = "23.05";
    }
    EOF
    ```

    ## Run a VM
    @@ -70,8 +67,14 @@ EOF
    git init # skip this step if you are inside already tracked repository
    git add . # flakes requires at least tracking the files
    nixos-rebuild build-vm --flake .#test
    # expose port 22 from guest
    export QEMU_NET_OPTS="hostfwd=tcp::2221-:22"
    result/bin/run-nixos-vm
    ```

    ## Profit!
    ## Profit!

    ```bash
    # ssh onto the machine
    ssh -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no admin@localhost -p 2221
    ```
  4. FlakM created this gist May 22, 2023.
    77 changes: 77 additions & 0 deletions nix_vm.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    ## Create simple flake:

    ```bash
    cat <<EOF > flake.nix
    {
    inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    outputs = { self, nixpkgs, ... }@attrs:
    let
    system = "x86_64-linux";
    pkgs = import nixpkgs { inherit system; };
    in
    {
    # test is a hostname for our machine
    nixosConfigurations.test = nixpkgs.lib.nixosSystem {
    inherit system;
    modules = [
    ./configuration.nix
    ];
    };
    };
    }
    EOF
    ```

    ## Add your configuration

    ```bash
    cat <<EOF > configuration.nix
    { config, lib, pkgs, ... }: {
    # customize kernel version
    boot.kernelPackages = pkgs.linuxPackages_5_15;
    users.users = {
    admin = {
    isNormalUser = true;
    extraGroups = [ "wheel" ];
    password = "admin";
    group = "admin";
    };
    };
    virtualisation.vmVariant = {
    # following configuration is added only when building VM with build-vm
    virtualisation = {
    memorySize = 2048; # Use 2048MiB memory.
    cores = 3;
    graphics = false;
    };
    };
    services.openssh = {
    enable = true;
    settings.PasswordAuthentication = true;
    };
    networking.firewall.allowedTCPPorts = [ 22 ];
    environment.systemPackages = with pkgs; [
    htop
    ];
    system.stateVersion = "23.05";
    }
    EOF
    ```

    ## Run a VM

    ```bash
    git init # skip this step if you are inside already tracked repository
    git add . # flakes requires at least tracking the files
    nixos-rebuild build-vm --flake .#test
    export QEMU_NET_OPTS="hostfwd=tcp::2221-:22"
    result/bin/run-nixos-vm
    ```

    ## Profit!