I am looking for `pkgs.runShellScript`

I am looking for a complimentary function to pkgs.writeShellScript, which I should expect to be named pkgs.runShellScript, which takes a derivation suffix (or the name of the script), and the contents of the script.

I would suppose you use it something like

let
  script = name: pkgs.runShellScript "do_tingy_${name}.sh" ''
    mkdir -p $out/${name}
    echo "$name" >> $out/${name}/default
  '';
  scriptOut = name: "${script name}/${name}/default";
in
  xdg.configFile."foo/bar".source = scriptOut "bar";

Where interpolating the return value of the function, it can be coerced to a string containing the path of the store path of the derivation.

My simple version?

      runShellScript = name: args: text:
        pkgs.runCommand name args (
          "${pkgs.getBin pkgs.bash}"
          + pkgs.writeShellScript name text
        );

I believe runCommand on its own does exactly what you want.

runCommand "hello" { ECHO_VAR = "hello"; } "echo $ECHO_VAR > $out"
1 Like

Well my example was multiline, and the script I intend to use is rather long. I could use heredoc to a bash interpreter, but I don’t know how well that would compose.

so just use the multiline Nix syntax? ''.

pkgs.runCommand "hello" { ECHO_VAR = "hello"; } ''
  echo $ECHO_VAR > $out
  ${lib.getExe pkgs.cowsay} "The value of ECHO_VAR is inside $out"
'';

you could write your script in a local file and add it to the nix shell

Being very explicut about what @Solene means in this context:

pkgs.runCommand "hello" { ECHO_VAR = "hello"; } (builtins.readFile ./hello.sh);
1 Like

Does that mean you’re missing out of referencing nix values with nix interpolations? ${pkgs.hello}/bin/hello for example. I guess you could use import and call a nix file starting with ''.

1 Like

Yes, but I don’t think that’s per-se an isse, since you can resolve those paths through runCommand’s env variables if you need them.

For the record, I prefer multiline strings, or proper mkDerivations when scripts are too complex for those, but this seems to be what @spikespaz wants?

Their suggested function is the cleanest alternative if you want to have and eat all your cakes otherwise, and I don’t think there’s a better way to implement it (except maybe using writeShellApplication for nicer package resolution).