如何使用 Nix 搭建开发环境?

How to use Nix to setup a development environment?

假设我需要 PostgreSQL 9.6.3 和 Ruby 2.3.1 以及各种其他工具。我找不到解释我需要做什么的教程。

从 Nix 手册来看,我似乎需要写一个 Nix expression 来安装所需的依赖项,但我无法从:

{ stdenv, fetchurl, perl }:

stdenv.mkDerivation {
  name = "hello-2.1.1";
  builder = ./builder.sh;
  src = fetchurl {
    url = ftp://ftp.nluug.nl/pub/gnu/hello/hello-2.1.1.tar.gz;
    md5 = "70c9ccf9fac07f762c24f2df2290784d";
  };
  inherit perl;
}

到将安装正确的 PostgreSQL 和 Ruby 版本的表达式。我完全不清楚将安装 PostgreSQL 和 Ruby 的文件放在哪里,或者如何 运行 给定目录中的单个文件。

有人可以提供此类教程的指针,或者为我指明正确的方向吗?

您可以使用nix-shell for this. It drops you into a shell configured to the given nix expression. Initially that expression could simply be along the lines of buildInputs = [ pkgs.ruby ]; and you can develop it from that. There are a number of helpful articles online written by nix users that give more examples of using nix-shell, like this one from garbas.si

您可能还会发现更好地了解 nix 包的工作原理很有用。有一个单独的nixpkgs manual that covers in greater detail using nix to create package expressions. A quick skim of the 3rd section should be useful to give a bit more understanding. There's also a chapter on using nix with ruby bundler that might be useful for you. Again there are articles that give more examples of its use, such as one from stesie.github.io

如果您实际上 运行 在您的环境中需要 postgresql,nix 不会为您管理;它的功能仅仅是构建和管理包,而不是它们的激活。您可以简单地手动激活 postgres,使用 nix-shell 挂钩,或创建一些其他与 nix 的集成,但我认为最可靠的选择是使用构建在Nix - NixOS. NixOS integrates with nix packages and manages the services provided by the packages. You could create a NixOS configuration with postgres active and your development environment present. This utility from github.com/chrisfarms 也可能有兴趣。