Lightweight Linux VMs on NixOS

栏目: IT技术 · 发布时间: 4年前

内容简介:Often I find myself needing aEnterOn NixOS, you can install LXD by adding

Often I find myself needing a pristine Linux system for testing some program that is expected to work on a user’s machine with an environment that is possibly quite different to mine. I could spin up a virtual machine, but that is too heavyweight. Alternatively, I could use Docker, but a Docker container is conceptually more of a process and less of a system.

Enter LXD , which advertises itself as offering a “user experience similar to virtual machines but using Linux containers instead.” Or, as u/Floppie7th puts it, “LXD makes ‘pet’ containers. Basically, VMs without the virtual hardware and extra kernel.” In other words, LXD allows us to spin up lightweight (Linux) VMs on a Linux machine, where one cares more about the separation of userland than hardware or kernel.

  • Running a Ubuntu container
    • Entering the container
  • Running a NixOS container

Installing lxd

On NixOS, you can install LXD by adding virtualisation.lxd.enable = true; to your configuration.nix. You might also want to add yourself to the lxd user group so as to not have to use sudo when running the lxc command.

Running a Ubuntu container

Let us run a bare Ubuntu container to get started:

lxc launch ubuntu:18.04 pristine -c security.nesting=true

(Note that security.nesting flag is being enabled so that we may be able to install Nix later; you may leave it disabled if you would not be using Nix).

We named the container “pristine”, and you can check its status in lxc list —it should be in the RUNNING state.

Entering the container

This will drop us in the root shell:

lxc exec pristine -- /bin/bash

However, usually, it is better to create a user account (with sudo access) first, and then use it:

lxc exec pristine -- adduser --shell /bin/bash --ingroup sudo srid

Then you may directly log in as that user as follows:

lxc exec pristine -- su - srid -c 'tmux new-session -A -s main'

Note that we use tmux so that programs requiring tty will work correctly.

Installing Nix

Assuming you have enabled the security.nesting flag on the container, you should now be able to install Nix by following the official instructions .

I use Nix to develop and install my programs, so the above is all I need to do in order to begin testing them on a pristine Linux machine without much fanfare.

Running a NixOS container

The official image server for LXD does not support NixOS. However, we can build our own using nixos-generators . You will need a configuration.nix (see mine ) to build a NixOS image:

nix-shell -p nixos-generators --run \ 
  'lxc image import --alias nixos $(nixos-generate -f lxc-metadata | xargs -r cat | awk \'{print $3}\') $(nixos-generate -c configuration.nix -f lxc | xargs -r cat | awk \'{print $3}\')'

Then launch a container:

lxc launch nixos childnixos

Access its root shell:

lxc exec childnixos -- /run/current-system/sw/bin/bash

If the configuration.nix also declares a user account, you can instead directly log in as that user:

lxc exec childnixos -- /run/current-system/sw/bin/su - srid  \
  -c 'tmux new-session -A -s main'

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java Script深度剖析

Java Script深度剖析

卢云鹏、沈维伦、Don Gosselin、李筱青 / 卢云鹏、沈维伦、李筱青 / 北京大学出版社 / 2004-10-1 / 49.0

本书适合于大中专院计算机相关专业作为教材,也是JavaScript初学者以及JavaScript爱好者的理想参考用书。书中详细介绍了基本的JavaScript程序设计原理以及实现它们的语法,内容包括JavaScript简介,变理、函数、对角和事件,数据类型、运算符,结构化逻辑控制结构和语句,窗口和框架,表单,动态HTML和动画,cookie和安全性,服务器端 JavaScript,数据库连接,使用......一起来看看 《Java Script深度剖析》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码