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.
If you want to actually be able to connect to the server: If you’re not using the provided VM make sure your Linux VM has enough RAM to host the server (2-4Gb).
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, and you can contact me through any platform on the card at
jaysa.net if you need help :)
Resources #
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 #
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.
- 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;
users.users.jaysa = {
isNormalUser = true;
extraGroups = [ "wheel" ]; # Enable 'sudo' for the user.
initialPassword = "test";
};
environment.systemPackages = with pkgs; [
cowsay
lolcat
];
system.stateVersion = "24.05";
}
sudo nix-build '<nixpkgs/nixos>' -A vm -I nixpkgs=channel:nixos-24.05 -I nixos-config=./configuration.nix
This command may take awhile to run. Go grab a soda or something.
ls -R ./result
QEMU_KERNEL_PARAMS=console=ttyS0 ./result/bin/run-nixos-vm -nographic; reset
Now, you should have a working nixos VM that you can log into with the username and password specified in configuration.nix!
rm -r result nixos.qcow2
sudo nix-build '<nixpkgs/nixos>' -A vm -I nixpkgs=channel:nixos-24.05 -I nixos-config=./configuration.nix
sudo QEMU_KERNEL_PARAMS=console=ttyS0 ./result/bin/run-nixos-vm -nographic; reset
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 (look at the section where we installed lolcat and cowsay!)
2.1: Basic minecraft configuration #
2.2: Error: Minecraft is NOT GNU+OK!!! #
Unfortunately, Minecraft is not free and open source software, and nix will not evaluate our derivation until we allow unfree software in our system. Figure out how to work around this.
Hint: https://wiki.nixos.org/wiki/Unfree_software
Bonus error: OOM, also known as… out of memory! #
Figure out how to check the logs and see what went wrong. NixOS runs minecraft as a systemd service, so you can check the journal logs just like those of any other process on the machine (see old lab for a refresher).
Hint: think about how much memory we specified in our jvmOpts option. Do we know if our NixOS VM even has the amount of memory we told our minecraft server use? How do we check how much memory the NixOS VM has?
Hint 2: We can see how much memory our VM has by running free -h inside of it. Perhaps we could allocate more memory (say, 8GB) using an additional argument to the QEMU command that starts the VM.
https://wiki.nixos.org/wiki/Swap
Even if you can’t resolve the out of memory error, don’t worry. Just describe some of the steps you took to debug it and methods you tried before moving onto the next section. Don’t feel bad; it’s more likely to be an issue with our hardware than anything you did wrong!
2.3 Set the Whitelist #
Find the NixOS option used to change the server gamemode to creative.
Now, in your server logs, default game type should appear as SURVIVAL instead of CREATIVE Default game type: SURVIVAL [Server thread/INFO]: Default game type: SURVIVAL
One Extra feature! #
Pick an additional feature to add to your server. If you don’t know what to do, here are some ideas (you can confirm each of these by checking the systemd log for minecraft, each should modify the output:
- Change the port which the server listens on.
- Change the version of minecraft that the server runs.
- Set the gamemode to creative.
Bonus: Backups #
Conclusion #
Unfortunately, our network this semester isn’t set up so that you can join your server from your outside Minecraft client and play on it. Also, your virtual machine would require more RAM for the game not to be unbearably laggy (but the machine running all the VMs only has 500GB of RAM… split among all of our students, it isn’t much). 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! 💎⛏️
FAQ: semicolon error…
Gradescope Questions: #
- How did you manage to solve the minecraft server running out of memory? answer: QEMU_KERNEL_PARAMS=console=ttyS0 ./result/bin/run-nixos-vm -nographic -m 8192; reset