Use cd in Nix Expression Language

I’m trying to configure systemd unit for NixOS. My problem is that binary that has to be executed by the unit, should be executed from the directory with that binary, rather then from the root(/) of the environment of the systemd unit. That’s why my ExecStart option firstly uses cd to change directory to the correct one:

 ExecStart =
          "cd ${pkgs.alps}/bin && ./alps";

But unit fails with error that it unable to find cd binary.

As I know, binaries should be referenced in Nix, using the following scheme ${pkgs.pkgName}/bin/pkgName(mostly) and also that package should be added to PATH of the systemd unit.

So I’m wondering, in which package(or path), cd binary can be found?

You are likely looking for the WorkingDirectory config setting for your systemd unit.

https://www.freedesktop.org/software/systemd/man/systemd.exec.html#WorkingDirectory=

cd is a shell built in, so you’d need to execute a shell to interpret it and the &&. Better is to just set the working directory.

1 Like

systemd needs a full path, so while you could do this:

ExecStart = “/bin/sh -c ‘cd /opt/some_place ; ./the_binary’”;

The “proper” way is:

ExecStart = “/opt/some_place/the_binary”;
WorkingDirectory = “/opt/some_place”;

3 Likes