Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buildFHSUserEnv: use -a "$0" in almost last exec #83296

Closed
wants to merge 1 commit into from

Conversation

doronbehar
Copy link
Contributor

Motivation for this change

In a derivation using buildFHSUserEnv, I'd like to support multiple executables in $out/bin using a script such as:

  runScript = writeScript "${name}-wrapper" ''
    arg0="$(basename "$0")"
    for executable in ${lib.concatStringsSep " " (listOfExecutables)}; do
        if [[ "$arg0" == "$executable" ]]; then
            exec -a "$0" ${drvBeingWrapped}/bin/$executable "$@"
        fi
    done
  '';

While in the derivation coming out of buildFHSUserEnv, I'm putting in extraInstallCommands:

  extraInstallCommands = ''
    for executable in ${lib.concatStringsSep " " (listOfExecutables)}; do
        ln -s $out/bin/${name} $out/bin/$executable
    done
  '';

Where listOfExecutables is some list I'm controlling via:

let
  listOfExecutables = [ ..  ];
in
  buildFHSUserEnv rec {
  ...
};

The idea is that every executable's name in listOfExecutables is linked to the main executable - $out/bin/${name} and that executable is calling in turn runScript. I'd like runScript to know the original $0 in order to know what real executable inside the FHS environment to call.

Right now, it's impossible to implement this because the FHS' environment is calling runScript without -a "$0". This change should fix this (untested).

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.

@kwohlfahrt
Copy link
Contributor

I went back and forth on this design when working on the quartus package, and I'm not sure I like using $0. If a user linked to $out/bin/$executable (where $executable is not the wrapper script, but one of the links) using a different name, this would mangle $0, in a way that it wouldn't if the output was an actual real script that set the command (which is the design I went for in the end).

I would suggest a slightly more invasive change, something along the line of

runScript = {
  'path/inside/out' = myScript;
};

Then, if runScript is a string, buildFHSUserEnv can go on as it does now. If it is an attrset, it can link and wrap each of the individual values. This seems more robust to me, but I'd appreciate comments.

@timokau
Copy link
Member

timokau commented Mar 31, 2020

Regarding #82259 (comment):

Requests for opinion are of course also okay, although they do take a bit more time :)

I've actually seen this one already. I didn't act on it because

  • I'm not very experienced with buildFHSUserEnv and I don't understand the problem you're solving.
  • You added an "untested" disclaimer, so I assumed this is not ready for final review yet.

@doronbehar
Copy link
Contributor Author

I would suggest a slightly more invasive change [...]

@kwohlfahrt I tend to agree now... I'll work on this sometime...

@doronbehar doronbehar closed this Mar 31, 2020
@doronbehar doronbehar deleted the buildFHS-exec-arg0 branch March 31, 2020 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants