gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:30:14 34 4
gpt4 key购买 nike

我对 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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com