Lab 10 - Nix

Lab 10 - Nix #

Table of Contents


Overview #

For this lab, we will be installing and configuring a Minecraft server inside of a NixOS VM. All of this configuration can be successfully done without a Minecraft client or knowledge on how to play the game itself.

Getting help #

If you want any help with any part of this lab, join the OCF Discord ( https://ocf.io/discord), matrix ( https://ocf.io/matrix), or IRC ( https://irc.ocf.io) and ask in the #decal-general channel!

The original creator of this lab is ~jaysa, with revisions by ~sbwilliams.

Resources #

stack of books

Nix Expression Syntax #

Two similar tutorials on the Nix language, which are helpful in understanding the syntax of nix expression, such as the configuration.nix file we’ll be writing for this lab.

Program Configuration #

Part 1: Virtual Machine Inside a Virtual Machine #

spinning inception top

1.1: Installing Nix #

Log into your DeCal VM and install the Nix package manager with the following command:

sudo apt install nix

1.2: Creating a NixOS virtual machine #

Now, we’re going to set up the NixOS VM inside of our Debian VM.

Why is it necessary to create a NixOS VM inside of our existing Debian VM? Didn’t we just install Nix?

With apt, we installed only the Nix package manager. However, we get many more options for system customization when we are running the Nix package manager on NixOS. It allows more core features of our system, such as user management and file systems, to be managed declaratively.

The following section is mostly taken from this guide: nix.dev: Nixos Virtual Machines.


  1. Let’s create a default NixOS configuration file for our NixOS virtual machine to use. Here is a minimal
{ config, pkgs, ... }:

{
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  # create a user named "decalstudent"
  # nixos by default will merge this configuration with any users that already exist on the system and only set passwords on user creation. setting users.mutableUsers = false allows for declarative user configuration managed by nixogs
  users.users.decalstudent = {
    isNormalUser = true;
    extraGroups = [ "wheel" ]; # Enable 'sudo' for the user.
    # if it wasn't obvious, this option is an incredibly bad idea outside of testing, as anyone with access to your nixos configuration could log in as you
    # see the hashedPassword and hashedPasswordFile options for (comparatively) safer alternatives
    initialPassword = "meowmeow";
  };

  # packages to be installed in the nixos system, from nixpkgs
  # https://search.nixos.org/packages
  environment.systemPackages = with pkgs; [
    cowsay
    lolcat
  ];

  nixpkgs.config.allowUnfree = true;

  /*# run a minecraft server as a systemd service managed by nixos
  services.minecraft-server = {
    enable = true;
    # declaratively configure the server whitelist and server.properties based on nixos configuration
    declarative = true;

    # configure the server.properties file
    serverProperties = {
      # allow users without microsoft accounts to log in
      online = false;
    };
  };*/

  system.stateVersion = "26.05";
}

Use the following commands to build and run your VM respectively:

# build the vm:
sudo nix-build '<nixpkgs/nixos>' -A vm -I nixpkgs=channel:nixos-26.05 -I nixos-config=./configuration.nix
# run the vm:
sudo QEMU_KERNEL_PARAMS=console=ttyS0 ./result/bin/run-nixos-vm -nographic -m 4096; reset

Now, you should have a working nixos VM that you can log into with the username and password specified in configuration.nix!


Part 2: Installing the Minecraft Server #

Now that we have a NixOS machine to play with, let’s actually install minecraft declaratively within configuration.nix.

2.1: Basic Minecraft Configuration #

There’s a basic configuration for a minecraft server already present in the above configuration, but you’ll need to uncomment it, then rebuild the configuration.

However, your configuration will now fail to build, as you have not accepted Minecraft’s EULA, which is a requirement to run a Minecraft server. See if you can find a way around this using the NixOS services.minecraft-server configuration options. For a good starting point, check https://search.nixos.org/options?channel=26.05&query=services.minecraft-server&type=options.

2.2: Microsoft Sucks #

At this point, you should be able to try joining your minecraft server at the url of your decal vm! Doing so isn’t strictly necessary for this lab, but could be a fun way to see it working. The OCF desktops already have Prism Launcher installed to be able to play minecraft.

NOTE: currently you probably can’t actually join the server due to networking :(

Unfortunately, you do need a Microsoft account and a copy of Minecraft to be able to do so. If you don’t have this and would still like to join the server, talk to me IRL and I might be able to help.

Either way, you wouldn’t be able to connect, as NixOS manages a firewall that by default blocks all incoming connections. You should be able to find a way to fix this within the NixOS configuration options.

2.3 Extra Features #

A couple simple features you should be able to change. You can check them in game, or from the console logs when the server starts up (sudo journalctl -u minecraft-server). You should be able to set all of these using NixOS configuration options.

  1. Set the default gamemode to Creative (Hint: this is normally set through the server.properties file).
  2. Enable the whitelist and add at least one person to it (if you need a random username, you can use jeb_).
  3. Change the version of Minecraft that the server runs (Hint: the server is packaged by Nixpkgs).

Bonus: Backups #

Conclusion #

However, you can always take the configuration.nix file you wrote for this lab to a different computer with Nix installed (no matter the operating system) to build the QEMU VM, start it up, and get the same result!

That’s the beauty of the declarative nature of the Nix programming language, and the operating system and package manager which accompanies it. Nix is now one more tool you can turn to for avoiding repetitive tasks such as taking backups, replacing files, and other tedious systems administration work on the servers you work with.

I hope you enjoyed this lab!! Happy crafting! 💎⛏️