Skip to content

How to add Cockpit UI for virtual machines to NixOS

Introduction

Cockpit is a cutting-edge web-based graphical interface for server management. It allows you to administer servers effortlessly and includes a built-in terminal.

Personally, I utilize the NixOS package to monitor system logs, network settings, services, system resources, and CPU load and all without needing to touch the command line.

With the Cockpit UI for virtual machines, you can create, run, and manage virtual machines directly in your browser. Since Cockpit runs within NixOS, I thought it would be interesting to test its virtual machines UI. My aim was to replace Proxmox with NixOS, and then it would be useful to be able to create virtual machines. However, I eventually paused my testing and decided to further explore Quickemu along with Quickgui.

How To

Unfortunately, Cockpit machines is 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.

  1. First create the packages/cockpit directory

    Run the following command:

    # Open your terminal application
    sudo mkdir -p /etc/nixos/packages/cockpit
  2. Create /etc/nixos/packages/cockpit/default.nix

    /etc/nixos/packages/cockpit/default.nix
    # To edit use your text editor application, for example Nano
    { 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.

  3. Create /etc/nixos/packages/cockpit/virtual-machines.nix

    /etc/nixos/packages/cockpit/virtual-machines.nix
    # To edit use your text editor application, for example Nano
    { 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; [ ];
    };
    }

    The above is inspired by this script. Adjust the version if necessary.

  4. Add the cockpit-apps to configuration.nix

    /etc/nixos/configuration.nix
    # To edit use your text editor application, for example Nano
    { 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
    }

    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.

  5. Switch NixOS configuration

    Now you can switch to the new NixOS configuration. Run the following command:

    # Open your terminal application
    sudo nix-collect-garbage # Optional: clean up
    sudo nixos-rebuild switch
  6. Check the results

    Now you can browse to Cockpit and the virtual machines UI by opening a web browser and going to: http://localhost:9090. Replace localhost with the relevant IP address or FQDN if needed, and adjust the port if you changed it earlier.

Comments

    No comments found for this note.

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

    Copyright 2021- Fiction Becomes Fact