未加星标

Transient Global Environments with NixOS

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
transient global environments - the third path

Posted on October 4, 2016

Tags:nixos

There’s two broad paths to managing contexts and dependencies for projects, which cover most approaches that people use. However, there’s significant downsides to both, and it turns out that using Nix enables a third alternative which is very appealing.

Global Installation

A common, well known approach is to install things globally. This usually means using the system package manager or equivalent (e.g.apt-get, brew). In the simple cases, this works really well - there’s minimal overhead to get started. The well known downside is that when you have multiple projects, they can start to interfere with each other - for example if you work on two codebases that require different versions of gcc to build, you’re pretty much out of luck because the system will only support one global installation of gcc.

With Nix/NixOS, this would mean installing all your dependencies via nix-env -i , or by adding them all to environment.systemPackages .

Local Installation

This is generally the alternative to global installations. Part of the system is sandboxed off using some tool, and installations only exist within the sandbox. Some such tools are virtualenv , docker , stack , nix-shell . Another variant is manually installing things to weird locations - e.g.having one version of gcc in /usr/bin and then putting another one in /opt.

With Nix/NixOS, this would mean using nix-shell to set up isolated environments.

This works pretty well (and this approach is pretty commonly regarded as a best practice), but one pretty big downside is that it generally requires lots of fiddling with tools. For example, if you want to debug your program with gdb, and it’s running inside a docker container, you may have to map PIDs back and forth across the PID namespace. Or, if you want emacs to be able to run your python linter, you may have to set up a bunch of emacs variables so that emacs knows that it must invoke nix-shell /path/to/project.nix --run python , instead of just running python directly.

Transient Global Installation

A key point is that (at least for me), even when I have multiple projects, I’m only actively working on one at any given moment. So, I’m fine with a global install of my current project, as long as it’s trivial to get it out of the way when it’s time for me to switch focus. This way, I can avoid all the complexities of local installations - e.g. python will always just be python , but it will mean something different depending on which project I have open.

As a concrete example, let’s say that I have two projects, A and B.

A depends on (i.e.these need to be installed while I work on project A) python 3.4 and wget.

B depends on python 3.5 and curl

I can’t satisfy this by just installing everything globally - the two versions of python will conflict.

The local installation solution might look like this

$ cat project-a/default.nix let pkgs = import <nixpkgs> { }; in pkgs.stdenv.mkDerivation { name = "env-project-a"; buildInputs = with pkgs; [ python34 wget ]; } $ cat project-b/default.nix let pkgs = import <nixpkgs> { }; in pkgs.stdenv.mkDerivation { name = "env-project-b"; buildInputs = with pkgs; [ python35 curl ]; }

They don’t interfere, but to interact with either project I have to work inside nix-shell project-a/default.nix or nix-shell project-b/default.nix .

The equivalent transient global installation would look like this

$ cat project-a/project.nix let pkgs = import <nixpkgs> { }; in pkgs.buildEnv { name = "env-project-a"; paths = with pkgs; [ python34 wget ]; } $ cat project-b/project.nix let pkgs = import <nixpkgs> { }; in pkgs.buildEnv { name = "env-project-b"; paths = with pkgs; [ python35 curl ]; }

To work on A, I run

$ nix-env -f project-a/project.nix -i

to switch to B, I run

$ nix-env -e env-project-a $ nix-env -f project-b/project.nix -i

I no longer have to be inside a nix-shell to interact with the current project - all the dependencies are immediately available everywhere, including other terminals and editors and other programs.

Ideally the couple commands to activate a project can be wrapped up in a nice script, along with some logic to ensure only one is active at a time, and maybe some nice rofi -based project-switching menu.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

分页:12
转载请注明
本文标题:Transient Global Environments with NixOS
本站链接:http://www.codesec.net/view/481775.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(22)