haskell - 在 NixOS 中从 Haskell 运行 shell 命令

标签 haskell nixos

我对 NixOS 还很陌生,我正在尝试使用以下函数从 Haskell 程序中调用 emacs:

ediff :: String -> String -> String -> IO ()
ediff testName a b = do
  a' <- writeSystemTempFile (testName ++ ".expected") a
  b' <- writeSystemTempFile (testName ++ ".received") b
  let quote s = "\"" ++ s ++ "\""
  callCommand $ "emacs --eval \'(ediff-files " ++ quote a' ++ quote b' ++ ")\'"

当我使用 stack test 运行调用此命令的程序时,我得到以下结果(穿插着单元测试结果):
/bin/sh: emacs: command not found
Exception: callCommand: emacs --eval '(ediff-files "/run/user/1000/ast1780695788709393584.expected" "/run/user/1000/ast4917054031918502651.received")'

当我从我的 shell 运行未能在上面运行的命令时,它可以完美运行。如何在 NixOS 中从 Haskell 运行进程,就像我直接调用它们一样,以便它们可以访问与我的用户相同的命令和配置?

最佳答案

您的 shell 和 callCommand使用 PATH环境变量,所以似乎堆栈正在改变它。事实证明,stack 默认使用纯 nix shell,但您还想访问“不纯”的用户环境。

引用 stack documenation

By default, stack will run the build in a pure Nix build environment (or shell), which means the build should fail if you haven't specified all the dependencies in the packages: section of the stack.yaml file, even if these dependencies are installed elsewhere on your system. This behaviour enforces a complete description of the build environment to facilitate reproducibility. To override this behaviour, add pure: false to your stack.yaml or pass the --no-nix-pure option to the command line.



另一种解决方案是将 Emacs 添加到 nix.dependenciesstack.yaml (感谢@chepner)。这样做的好处是,当开发人员运行测试时,某些版本的 Emacs 将始终可用,但 Emacs 可能不是他们想要使用的 Emacs。您可以使用类似 ~/.config/nixpkgs/config.nix 的方法解决这个问题。 ,除非他们在其他地方配置了他们的 Emacs,比如系统配置或家庭管理员。我更喜欢简单但不纯洁的$PATH解决方案。

关于haskell - 在 NixOS 中从 Haskell 运行 shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48182820/

相关文章:

haskell - 由内而外构建管道代理

haskell - IO monad 在 System.Random 中如何工作

haskell - 在 Haskell 和 Yesod 中返回错误状态

haskell - NixOS、Haskell、opengl : problems with building and running openGL programs

haskell - MapM 在 haskell 中的结果?

Gradle — 新创建的守护进程具有与预期不同的上下文

x11 - 使用 LightDM + XMonad 在 NixOS 上进行 DPI 缩放

nix - 从 Nix 表达式语言中的另一个集合继承属性

attributes - 如何使用 nixos 中的属性集的默认值,并扩展它

selenium - 围绕 do block 的 haskell "withSubprocess"构造