首页 > 解决方案 > 如何合并两个 shell.nix 文件?

问题描述

我有第一个 shell.nix 文件:

{ pkgs ? import ./nix { }
, useClang ? false
, ae_name ? "ae"
}:

with pkgs;

(if useClang then tvb.aeClangStdenv else tvb.aeGccStdenv).mkDerivation rec {
  name = ae_name;

  nativeBuildInputs = tvb.cppNativeBuildInputs;
  buildInputs = tvb.rustBuildInputs
    ++ tvb.cppBuildInputs
    ++ tvb.rBuildInputs
  ;

  TZDIR = "${tzdata}/share/zoneinfo";
  LOCALE_ARCHIVE = "${glibcLocales}/lib/locale/locale-archive";

  out_dir = (toString ./out);
  cur_dir = (toString ./.);

  shellHook = ''
    export PS1="\[\033[38;5;10m\]\u@\h[${name} nix-shell]\[$(tput sgr0)\]\[\033[38;5;15m\]:\[$(tput sgr0)\]\[\033[38;5;39m\]\w\[$(tput sgr0)\]\\$\[$(tput sgr0)\] \[$(tput sgr0)\]"

    # for tools only bin paths are needed
    for prog in ${toString tvb.shellTools}; do
      export PATH="$prog/bin:$PATH"
    done

    export DEPLOY_CFG=${cur_dir}/.deploy.json

    export LD_LIBRARY_PATH="${out_dir}/lib:${fts5-snowball}/lib"
    export PATH="${cur_dir}/lua/bin:${out_dir}/bin:$PATH"
    export AE_SHARE="${out_dir}/share/ae"
    export AE_LIBEXEC="${out_dir}/libexec/ae"

    ## LuaJIT
    export LUA_PATH="$LUA_PATH;${cur_dir}/lua/lib/?.lua;${out_dir}/share/lua/5.1/?.lua;;"
    export LUA_CPATH="$LUA_CPATH;${out_dir}/lib/lua/5.1/?.so;;"

    ## Lua box
    export LUABOX_UNIT_PATH="${out_dir}/share/ae/box/units/?.lua;"

    ## Python
    export PYTHONPATH="${out_dir}/lib/python2.7:$PYTHONPATH"
  '';
}

我有第二个 shell.nix 文件:

let
  jupyter = import (builtins.fetchGit {
    url = https://github.com/tweag/jupyterWith;
    rev = "37cd8caefd951eaee65d9142544aa4bd9dfac54f";
  }) {};

  iPython = jupyter.kernels.iPythonWith {
    name = "python";
    packages = p: with p; [ numpy ];
  };

  iHaskell = jupyter.kernels.iHaskellWith {
    extraIHaskellFlags = "--codemirror Haskell";  # for jupyterlab syntax highlighting
    name = "haskell";
    packages = p: with p; [ hvega formatting ];
  };

  jupyterEnvironment =
    jupyter.jupyterlabWith {
      kernels = [ iPython iHaskell ];
    };
in
  jupyterEnvironment.env

首先,我尝试将第二个附加到第一个,但随后收到以下错误:

jbezdek@ubuntu:~$ nix-shell
error: syntax error, unexpected ID, expecting '{', at /home/jbezdek/shell.nix:51:3

之后,我尝试了许多其他组合如何将这两者结合在一起,但我从未成功过。你能帮我解决这个问题吗?

标签: shellnixnix-shell

解决方案


完全通用地合并两个 shell.nix 文件很棘手,而且不太可能现成的解决方案。

为了解决这个问题,我认为您只需深入研究 Nix 表达式语言即可编写一个语法有效的 .nix 文件,其中包含两个文件的内容。类似这样的东西可能会起作用:

{ pkgs ? import ./nix { }
, useClang ? false
, ae_name ? "ae"
}:

with pkgs;

let

  jupyter = import (builtins.fetchGit { ... })
  ...
  jupyterEnvironment = ...

in

{
   first_file = (if useClang ...).mkDerivation rec {
     name = ae_name;
     ... 
   };
   
   second_file = jupyterEnvironment.env;
}

推荐阅读