nix macos_在Visual Studio Code中应用Nix-Shell环境

nix macos

A lot of developers faced a problem with packages hell on their workstation. After a couple of months with experiments, including different languages and toolchains, I installed Elixir, Haskell-stack, Node.js/NVM, and other various stuff. Most exciting things happen when you need different versions of the same package for different projects. Humanity already invented a different solution for creating an isolated environment and switch them when needed. We are using NVM to manage Node.js versions, Python Virtual Env for selecting Python stuff versions or Docker for creating OS inside an OS. But none of the solutions satisfy all my requirements for the isolated development environment.

许多开发人员在他们的工作站上遇到软件包问题。 经过几个月的实验,包括不同的语言和工具链,我安装了Elixir,Haskell-stack,Node.js / NVM和其他各种东西。 当您需要不同项目的同一软件包的不同版本时,最令人兴奋的事情发生。 人类已经发明了另一种解决方案,用于创建隔离的环境并在需要时进行切换。 我们正在使用NVM管理Node.js版本,使用Python Virtual Env选择Python填充版本,或者使用Docker在操作系统内部创建操作系统。 但是,没有一种解决方案能够满足我对隔离开发环境的所有要求。

引入管理隔离环境工具的要求 (Inroduction to requirements for managing isolated environment tool)

It was a cold autumn evening. I was sitting in my kitchen room and thought about perfect isolation for project workspace with their dependencies. Actually, I didn’t, but the paragraph should have an introduction.

那是一个寒冷的秋天傍晚。 我坐在我的厨房里,想着项目工作空间及其依赖关系的完美隔离。 实际上,我没有,但是该段应该有一个介绍。

After that, I wrote following criteria for my perfect isolation tool:

之后,我为完美的隔离工具编写了以下标准:

  1. Should cover different languages and packages: not just Node.js, Python or Haskell.

    应该涵盖不同的语言和程序包:不只是Node.js,Python或Haskell。
  2. No virtualization. We are too young for all this shit. Lets save our time.

    没有虚拟化。 我们对于这一切都还太年轻。 让我们节省时间。
  3. All workspace packages should be easy to remove.

    所有工作区软件包都应易于删除。
  4. All workspace environments should be easy to remove.

    所有工作区环境都应易于删除。
  5. My IDE should be working with all this stuff without calling Molfars from the Ukrainian Carpathian mountains.

    我的IDE应该使用所有这些东西,而不必从乌克兰喀尔巴阡山脉叫莫尔法尔斯。

我们需要一杯茶和Google,但首先要喝杯茶 (We need a cup of tea and Google, but cup of tea at first)

I remember that once I read about the declarative package manager that can produce an isolated environment and manage all system dependencies and Haskell project dependencies as well.

我记得我曾经读过声明式包管理器,它可以产生一个隔离的环境并管理所有系统依赖项和Haskell项目依赖项。

Five minutes googling, 30 minutes reading, and 1-hour trying let me find out that the current tool is all I need. All I need is love. But stop, it’s another song. All I need is Nix.

五分钟的搜索时间,30分钟的阅读时间和1个小时的尝试时间让我发现当前所需的工具已足够。 我想要的只是爱。 别说了,这是另一首歌。 我只需要Nix。

Let’s compare the requirements I wrote above from actual Nix features.

让我们比较一下我上面根据实际Nix功能编写的要求。

  1. Compatible with macOS — yes.

    与macOS兼容-是的。
  2. Covers different languages and packages — yes.

    涵盖了不同的语言和软件包-是的。
  3. No virtualization — really no virtualization.

    没有虚拟化-真的没有虚拟化。
  4. All workspace packages should be easy to remove — maybe yes, but I don’t know how.

    所有工作区软件包都应该易于删除-也许可以,但是我不知道如何。
  5. My IDE should be working with all this stuff — no. There no way to integrate Nix Environment to Visual Studio Code.

    我的IDE应该使用所有这些东西-不。 无法将Nix Env​​ironment集成到Visual Studio Code。

扰流板警报 (Spoiler alert)

救救自己 编写Visual Studio代码扩展 (Help yourself. Writing Visual Studio Code extension)

The extension helps you integrate VS Code and Nix-shell together.

该扩展可帮助您将VS Code和Nix-shell集成在一起。

I’m too lazy to write this chapter from scratch, so the text bellow is copy/paste from official README.md from my extension.

我懒得从头开始写这章,所以下面的文字是从我的扩展名中的官方README.md复制/粘贴的。

入门 (Getting started)
  • First of all, you should install Nix package manager.

    首先,您应该安装Nix软件包管理器

  • Restart VS Code to be sure path to run nix-shell configured properly

    重新启动VS Code以确保正确配置运行nix-shell的路径
  • Install the extension

    安装扩展

  • Create nix env config, like default.nix in your project workspace root

    在项目工作区根目录中创建nix env配置,例如default.nix
  • Open commands pallet (Cmd/Ctrl + Shift + P) and type Select environment

    打开命令托盘(Cmd / Ctrl + Shift + P)并输入选择环境
  • From the list of nix virtual environments choose the one you’d like to apply

    从nix虚拟环境列表中选择要应用的环境
Haskell Project运行示例 (Haskell Project running example)

To run your Haskell application, you have to install the GHC compiler. To avoid global GHC installation and be able to use different compiler versions in your host lets do this by using nix virtual environment.

要运行Haskell应用程序,您必须安装GHC编译器。 为了避免全局安装GHC并能够在主机中使用不同的编译器版本,请使用nix虚拟环境来执行此操作。

Example of GHC compiler inside the NIX store (shell.nix):

NIX存储库中的GHC编译器示例(shell.nix):

{ nixpkgs ? import <nixpkgs> {} }:
let
  inherit (nixpkgs) pkgs;
  inherit (pkgs) haskellPackages;
  haskellDeps = ps: with ps; [
    base
    lens
    mtl
    random
  ];
  ghc = pkgs.haskell.packages.ghc864.ghcWithPackages haskellDeps;
  nixPackages = [
    ghc
    pkgs.gdb
    haskellPackages.cabal-install
  ];
in
pkgs.stdenv.mkDerivation {
  name = "snadbox-haskell-workspace";
  buildInputs = nixPackages;
}

Now let’s try to open our project in Visual Studio Code.

现在,让我们尝试在Visual Studio Code中打开我们的项目。

image

You can see, IDE can’t find a compiler. Let’s turn on shell.nix env.

可以看到,IDE找不到编译器。 让我们打开shell.nix env。

image

Bingo! Everything is fine now.

答对了! 现在一切都很好。

结论 (Conclusion)

I hope the article brought you some new ideas about how to make your life easier and manage your project without chaos in your file system.

我希望本文能为您带来一些新的想法,使您的生活更轻松,并在不使文件系统混乱的情况下管理项目。

The benefits of this approach are: 这种方法的好处是:
  • All packages in a separate workspace and don’t affect the global scope against you want it implicit

    所有软件包都位于单独的工作空间中,并且不会影响全局范围,因为您希望它隐式
  • Your IDE see all workspace staff and working as you expected

    您的IDE可以看到所有工作区人员并按预期工作
  • Simple project bootstrapping from a config file on different machines

    从不同机器上的配置文件进行简单的项目引导
Nothing is perfect in our world: 在我们的世界中,没有什么是完美的:
  • Need to learn a new language to write config files

    需要学习一种新语言来编写配置文件
  • Nix CLI option has no obvious meaning and all the time when you interact with CLI tools you need read command help

    Nix CLI选项没有明显的含义,并且在与CLI工具进行交互时,始终需要阅读命令帮助
  • All programming languages have their stuff to install ecosystem libraries. Like NPM, Cabal, Cargo, Pip, etc. And when you install this kind of packages via Nix, you have a risk of getting not working from the box some linters, analyzing tools, etc.

    所有编程语言都有自己的东西来安装生态系统库。 像NPM,Cabal,Cargo,Pip等。当您通过Nix安装此类软件包时,可能会有一些短绒,分析工具等无法正常使用的危险。

外部链接 (External links)

翻译自: https://habr.com/en/post/474916/

nix macos

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值