NixOS - Add Virtual Machines UI to Cockpit


With Cockpit Virtual Machines you can create, run, and manage virtual machines in your browser. Cockpit runs within NixOS and I thought it would be interesting to test the Virtual Machines UI of Cockpit. I would like to replace Proxmox with NixOS and then it would be useful to be able to create Virtual Machines. Ultimately, I stopped testing and made the choice to investigate Quickemu (with Quickgui) further.

Please note, the below is not a working setup, I am just sharing my findings and how far I have come. To get it working you will have to test further yourself. If you got it working (creating a VM), I’d love to hear about it in the comments!

Configuration

Cockpit machines is unfortunately not available as a package. However there was an outdated package within the Nix User Repository (NUR), so I decided to add a custom package declaratively.

First create a new directory, for example:

sudo mkdir -p  /etc/nixos/packages/cockpit

Create default.nix:

cd /etc/nixos/packages/cockpit
sudo nano default.nix

And add the following:

{ pkgs, ... }:

{
  virtual-machines = pkgs.callPackage ./virtual-machines.nix { };
  # podman-containers = pkgs.callPackage ./podman-containers.nix { };
}

You can add podman-containers.nix in the same way, but we will ignore that for now. Save the changes to default.nix.

Create virtual-machines.nix:

sudo nano virtual-machines.nix

And add the following (based on this script):

{ lib, stdenv, fetchzip, gettext }:

stdenv.mkDerivation rec {
  pname = "cockpit-machines";
  version = "302";

  src = fetchzip {
    url = "https://github.com/cockpit-project/cockpit-machines/releases/download/${version}/cockpit-machines-${version}.tar.xz";
    sha256 = "sha256-3dfB9RzFzN578djOSdANVcb0AZ0vpSq6lIG7uMwzAVU=";
  };

  nativeBuildInputs = [
    gettext
  ];

  makeFlags = [ "DESTDIR=$(out)" "PREFIX=" ];

  postPatch = ''
    substituteInPlace Makefile \
      --replace /usr/share $out/share
    touch pkg/lib/cockpit.js
    touch pkg/lib/cockpit-po-plugin.js
    touch dist/manifest.json
  '';

  postFixup = ''
    gunzip $out/share/cockpit/machines/index.js.gz
    sed -i "s#/usr/bin/python3#/usr/bin/env python3#ig" $out/share/cockpit/machines/index.js
    sed -i "s#/usr/bin/pwscore#/usr/bin/env pwscore#ig" $out/share/cockpit/machines/index.js
    gzip -9 $out/share/cockpit/machines/index.js
  '';

  dontBuild = true;

  meta = with lib; {
    description = "Cockpit UI for virtual machines";
    license = licenses.lgpl21;
    homepage = "https://github.com/cockpit-project/cockpit-machines";
    platforms = platforms.linux;
    maintainers = with maintainers; [ ];
  };
}

Adjust the version if necessary.

If it is not easy to determine the shaS256 of the tar.gz file, then switch to the new NixOS configuration with the wrong value, the new sha256 will then be mentioned

Save the changes to virtual-machines.nix.

Now open configuration.nix:

sudo nano /etc/nixos/configuration.nix

Adjust the configuration like this:

{ config, pkgs, lib, ... }:

let
  cockpit-apps = pkgs.callPackage packages/cockpit/default.nix { inherit pkgs; };
in
{
  imports =
  [ # Include the results of the hardware scan.
    ./hardware-configuration.nix
  ];

  environment.systemPackages = with pkgs; [
     cockpit
     # cockpit-apps.podman-containers
     cockpit-apps.virtual-machines
     libvirt # needed for virtual-machines
     virt-manager # needed for virtual-machines
  ];

# Add the rest of the configuration here

}

For a newer version of Cockpit (or Libvirt/Virt-manager) you can use the package from the unstable channel as described here.

According to some sources it may be necessary to add libvirtd or qemu-libvirtd to the user’s extraGroups, but I have not tested that further

Save the changes to configuration.nix.

Now you can switch to the new configuration:

sudo nix-collect-garbage # optional: clean up
sudo nixos-rebuild switch

You can view my complete configuration.nix here.


Read other notes

Comments

    No comments found for this note.

    Join the discussion for this note on this ticket. Comments appear on this page instantly.

    Tags


    Notes mentioning this note

    • NixOS - Cockpit Setup
      Cockpit is a modern web-based graphical interface for servers. You can use it to administer servers and it has a...

    Notes Graph