Escaping a shell positional argument into a package

I want to modify the package openresolv such that the RESTARTCMD command is setup correctly.

It needs to be set to something along the lines of

RESTARTCMD="${pkgs.systemd}/bin/systemctl restart $1"

However that does not work because multiple times during the build process the $1 is replaced with whatever positional argument the wrapping script was called with on this string at the time.
I have tried different quotation marks, as well as spamming various amounts of \ backslash characters, but to no avail.

How is one supposed to correctly escape this?

I’m not super sure since we’re not seeing what takes didn’t work and where in the chain it fails, but have you tried this variant?

RESTARTCMD='${pkgs.systemd}/bin/systemctl restart \$1'

(Technically there’s lib.escapeShellArg, but I am not sure if it’s helpful here.)

I already tried that with various amounts of backslashes. Each new backslash brings forth a new parameter deeper into the different scripts handling this file, but at some point the \\\\\\\\\\\\\$1 is somehow turned into \\configurePhase despite the backslashes being present.

Isn’t that the intended outcome?

Yes and no. Ultimately I want to have a $1 in some place that is supposed to be replaced with something.
But along the way there are many other scripts that try to also replace it which I want to prevent.
I.e. the environment variable is written to a config.mk file which gets processed by make during nixpkgs configurePhase.
To get the $1 into the make file already requires it to be escaped like this \''$\''$1 because nixpkgs strips away $ sings in '' blocks unless they are escaped with '', then some script inside nixpkgs replaces single dollar sings with the number 1 unless they are prefixed with backslash and once we are inside of make it tries to parse the single dollar sign unless we escape it with another dollar singn.

But that only pushes the problem further down the line. Right now I am at \''$\''$\''$\''$1 with no end in sight.

Also rebuilding with each change made takes about 30 minutes on my desktop PC only to have some other random parameter appear in the place where I want $1

Nit pick: “Nix strips away”

But… it doesn’t. I just tried in the repl. And why would it? It would, however, strip away ${}.

Does this help?

1 Like

Yea, I also found that I can drop those.
I actually just refactored my build setup to only include the packages that I need to test this which brought turnaround times from 30 minutes to a couple seconds.

I will post the final string here once I have it.

So turns out \$\$1 is correct, but there is a further problem when it is used with NetworkManager.
Maybe it is a bug in the source of resolvconf or NetworkManager.

1 Like