Skip to content

Instantly share code, notes, and snippets.

@v1k0d3n
Forked from tarnacious/build-qcow2.nix
Created October 9, 2024 00:48
Show Gist options
  • Select an option

  • Save v1k0d3n/49d3ec2cf43f9827e7dfb86cee0341ba to your computer and use it in GitHub Desktop.

Select an option

Save v1k0d3n/49d3ec2cf43f9827e7dfb86cee0341ba to your computer and use it in GitHub Desktop.

Revisions

  1. @tarnacious tarnacious revised this gist Apr 20, 2022. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions machine-config.nix
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,21 @@
    { pkgs, lib, ... }: [3/1275]
    with lib;
    {
    { pkgs, lib, ... }:

    with lib;

    {
    imports = [
    <nixpkgs/nixos/modules/profiles/qemu-guest.nix>
    ];
    config = {
    fileSystems."/" = {
    ];

    config = {
    fileSystems."/" = {
    device = "/dev/disk/by-label/nixos";
    fsType = "ext4";
    autoResize = true;
    autoResize = true;
    };

    boot.growPartition = true;
    boot.kernelParams = [ "console=ttyS0" ];
    boot.kernelParams = [ "console=ttyS0" ];
    boot.loader.grub.device = "/dev/vda";
    boot.loader.timeout = 0;

  2. @tarnacious tarnacious revised this gist Dec 3, 2019. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,15 @@ Should create a `results` directory that symlinks to a qcow2 image in the store.

    I basically copied this from the [openstack image in nixpkgs][1] because I don't know a better way.

    Update:

    Thanks to @d-goldin I've just learnt I can start a VM using just:

    ```
    nix-build '<nixpkgs/nixos>' -A vm -I nixos-config=./machine-config.nix build-vm
    ./result/bin/run-nixos-vm
    ```

    Which is pretty neat, and I guess pretty common.

    [1]: https://github.com/NixOS/nixpkgs/blob/9e8140864bf629ea2d781a863abfedd6cab4229f/nixos/maintainers/scripts/openstack/openstack-image.nix
  3. @tarnacious tarnacious revised this gist Dec 3, 2019. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,11 @@
    Build a bare bones bootable nixos qcow2 image suitable for running with libvirt/qemu/kvm.
    Build a bare bones bootable nixos qcow2 image suitable for running with libvirt/qemu/kvm.

    ```
    nix-build '<nixpkgs/nixos>' -A config.system.build.qcow2 --arg configuration "{ imports = [ ./build-qcow2.nix ]; }"
    ```

    Should create a `results` directory that symlinks to a qcow2 image in the store.
    Should create a `results` directory that symlinks to a qcow2 image in the store.

    I basically copied this from the [openstack image in nixpkgs][1] because I don't know a better way.

    [1]: https://github.com/NixOS/nixpkgs/blob/9e8140864bf629ea2d781a863abfedd6cab4229f/nixos/maintainers/scripts/openstack/openstack-image.nix
  4. @tarnacious tarnacious created this gist Dec 3, 2019.
    24 changes: 24 additions & 0 deletions build-qcow2.nix
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    { config, lib, pkgs, ... }:

    with lib;

    {
    imports =
    [
    <nixpkgs/nixos/modules/installer/cd-dvd/channel.nix>
    ./machine-config.nix
    ];

    system.build.qcow2 = import <nixpkgs/nixos/lib/make-disk-image.nix> {
    inherit lib config;
    pkgs = import <nixpkgs> { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package
    diskSize = 8192;
    format = "qcow2";
    configFile = pkgs.writeText "configuration.nix"
    ''
    {
    imports = [ <./machine-config.nix> ];
    }
    '';
    };
    }
    25 changes: 25 additions & 0 deletions machine-config.nix
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    { pkgs, lib, ... }: [3/1275]

    with lib;

    {
    imports = [
    <nixpkgs/nixos/modules/profiles/qemu-guest.nix>
    ];

    config = {
    fileSystems."/" = {
    device = "/dev/disk/by-label/nixos";
    fsType = "ext4";
    autoResize = true;
    };

    boot.growPartition = true;
    boot.kernelParams = [ "console=ttyS0" ];
    boot.loader.grub.device = "/dev/vda";
    boot.loader.timeout = 0;

    users.extraUsers.root.password = "";

    };
    }
    7 changes: 7 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    Build a bare bones bootable nixos qcow2 image suitable for running with libvirt/qemu/kvm.

    ```
    nix-build '<nixpkgs/nixos>' -A config.system.build.qcow2 --arg configuration "{ imports = [ ./build-qcow2.nix ]; }"
    ```

    Should create a `results` directory that symlinks to a qcow2 image in the store.