Running shell commands from Haskell on NixOS

I am new to NixOS and trying to call emacs from a Haskell program using the following function:

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

When I run a program that calls this command using the stack test , I get the following result (briefly with the unit test results):

 /bin/sh: emacs: command not found Exception: callCommand: emacs --eval '(ediff-files "/run/user/1000/ast1780695788709393584.expected" "/run/user/1000/ast4917054031918502651.received")' 

When I run a command that did not run from my shell, it works flawlessly. How can I start processes from Haskell to NixOS, as if I were invoking them directly so that they could access the same commands and configurations as my user?

+5
source share
1 answer

Both your shell and callCommand use the PATH environment variable, so it seems like the stack is changing this. It turns out that the stack uses a clean nix shell by default, but you also want to access your user environment, which is "unclean".

To quote documenation for a stack

By default, the stack will run the assembly in a clean Nix build environment (or shell), which means that the assembly should fail if you did not specify all the dependencies in the packages: the section of the stack.yaml file, even if these dependencies are installed elsewhere on your system . This behavior provides a complete description of the build environment to ensure reproducibility. To override this behavior, add pure: false to your stack.yaml file or pass the -no-nix-pure option on the command line.

Another solution is to add Emacs to nix.dependencies in stack.yaml (thanks @chepner). The advantage is that some version of Emacs will always be available when the developer runs the tests, but Emacs may not be the Emacs they want to use. You might be able to get around this using something like ~/.config/nixpkgs/config.nix , unless they configured their Emacs elsewhere, such as a system configuration or perhaps a home manager. I would prefer a simple but unclean solution to $PATH .

+3
source

Source: https://habr.com/ru/post/1274608/


All Articles