Agree with waffle that understanding your goal/motivation may help.
I wrote this post characterizing the main approaches to supplying dependencies for shell scripts with Nix installed:
Maybe there’s enough there for you to know if those approaches meet your needs, but we’ll need more context if there’s a really specific reason that neither a nix-shell shebang nor packaging the script itself are appropriate for your use-case.
I have created a replacement for xfce4-session-logout so anything that natively refers to it, such as plugins, will use my replacement. However I still need/want to use the built-in method to do things like xfce4-session-logout --halt or xfce4-session-logout --reboot which requires knowing the current installed path as I have replaced the link in /run/current/sw/bin with my custom version.
Long term I will indeed package this inside the configuration.nix and I am also currently doing this. For now though, for testing purposes, being able to access the source path from bash would be very useful and knowing the right way to do this will help me improve my knowledge of Nix and NixOS.
Perhaps you can describe the half a dozen ways you mentioned and the advantages/disadvantages of each to help me figure out the best way to do this. If nothing else, I am certain I will learn something from seeing them.
I suppose the factors that affect what is the perceived ‘right’ way to do this include things like if the solution requires Internet access (wouldn’t be good if you had no Internet access and were relying on it to shut the computer down! ) Another factor would be the use of additional packages or software. If they are required I guess the question is what advantages they bring.
I did find one method as shown above, that seems to work. perhaps the words ‘nix-build’ made me question if this was the correct approach but I couldn’t find a better alternative so I thought I should ask.
Hi, thanks for the shared link. I could see where the nix store path was used in a number of bash scripts, but I could only see this path being resolved like I did above inside configuration.nix or similar. I agree that doing this from configuration.nix is the best overall approach and has been the one I have favoured. I just wanted to know if the same could be done from a shell script. Please see my reply to @waffle8946 above, as I hopefully expanded my reasons more there.
From your post…
Nix-shell seems to require Internet access and didn’t seem to return the path. I found the following example;
… but this seems to return the path for the ‘latest’ version as opposed to the version that is actually installed so it wasn’t quite what I was looking for. It also uses the Internet. Thanks for your feedback.