Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NixOS within NixOS?

I'm starting to play around with NixOS deployments. To that end, I have a repo with some packages defined, and a configuration.nix for the server.

It seems like I should then be able to test this configuration locally (I'm also running NixOS). I imagine it's a bad idea to change my global configuration.nix to point to the deployment server's configuration.nix (who knows what that will break); but is there a safe and convenient way to "try out" the server locally - i.e. build it and either boot into it or, better, start it as a separate process?

I can see docker being one way, of course; maybe there's nothing else. But I have this vague sense Nix could be capable of doing it alone.

like image 867
user2141650 Avatar asked Dec 03 '18 19:12

user2141650


People also ask

What is special about NixOS?

NixOS is a Linux distribution built on top of the Nix package manager. It uses declarative configuration and allows reliable system upgrades. Several official package "channels" are offered, including the current Stable release and the Unstable release which follows the latest development.

How many packages are in NixOS?

According to Repology, as of July 2021 it contains more than 70,000 packages, and is the most up-to-date package repository.

What is NixOS used for?

NixOS is a GNU/Linux distribution that uses Nix as both a package manager and a configuration manager. Every single configuration file is described as a derivation, and your system is a derivation that depends on all the applications and config files you've specified.

What makes NixOS different?

NixOS uses Nix not just for package management but also to build all other static parts of the system. This extension follows naturally: while Nix derivations typically build whole packages, they can just as easily build any file or directory tree so long as it can be built and used in a pure way.


2 Answers

There is a fairly standard way of doing this that is built into the default system.

Namely nixos-rebuild build-vm. This will take your current configuration file (by default /etc/nixos/configuration.nix, build it and create a script allowing you to boot the configuration into a virtualmachine.

once the script has finished, it will leave a symlink in the current directory. You can then boot by running ./result/bin/run-$HOSTNAME-vm which will start a boot of your virtualmachine for you to play around with.

TLDR;

  1. nixos-rebuild build-vm
  2. ./result/bin/run-$HOSTNAME-vm
like image 107
hamhut1066 Avatar answered Oct 15 '22 11:10

hamhut1066


nixos-rebuild build-vm is the easiest way to do this, however; you could also import the configuration into a NixOS container (see Chapter 47. Container Management in the NixOS manual and the nixos-container command).

This would be done with something like:

containers.mydeploy = {
  privateNetwork = true;
  config = import ../mydeploy-configuration.nix;
};

Note that you would not want to specify the network configuration in mydeploy-configuration.nix if it's static as that could cause conflicts with the network subnet created for the container.

like image 31
Samuel Leathers Avatar answered Oct 15 '22 11:10

Samuel Leathers