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

Generate a bin folder inside .devenv #167

Closed
shyim opened this issue Dec 1, 2022 · 6 comments · Fixed by #248
Closed

Generate a bin folder inside .devenv #167

shyim opened this issue Dec 1, 2022 · 6 comments · Fixed by #248

Comments

@shyim
Copy link
Contributor

shyim commented Dec 1, 2022

All Jetbrains IDEs needs an absolute path where the executables are living.

It would be nice if we would symlink php to .devenv/bin/php. So you can configure your IDE file path to that directory.
Of course when the package gets updated we update it there too.

A little bit like the /run/current-system/sw

@domenkozar
Copy link
Member

I think we can now just symlink profile to .devenv/profile, I'd be open for such a PR.

@shyim
Copy link
Contributor Author

shyim commented Dec 23, 2022

Should I make this as integration module? Because it's like a "shim" for editors which don't have proper direnv integration

@domenkozar
Copy link
Member

We can do it by default in top-level.nix since it's just a symlink

@szlend
Copy link
Contributor

szlend commented Dec 23, 2022

I don't think symlinking the profile is going to be enough in most cases. It might work for PHP, but it's not going to work for gcc/clang or any language that needs to compile and link using those toolchains. They depend on proper environment setup (e.g. PKG_CONFIG_PATH, etc). One alternative would be to wrap each binary so it first loads the shell environment (source <(nix print-dev-env)). You could then optimize it by caching nix print-dev-env and only loading it once per process tree.

@szlend
Copy link
Contributor

szlend commented Dec 23, 2022

I quickly hacked together a PoC of this yesterday (plain nix flakes, not devenv)

pkgs.writeShellScriptBin "wrap" ''
  rm -rf .wrap
  mkdir -p .wrap/bin

  nix print-dev-env > .wrap/devshell.sh
  path=$(env -i ${pkgs.bash}/bin/bash -c "source .wrap/devshell.sh && echo \$PATH")

  for path in $(${pkgs.findutils}/bin/find ''${path//:/ } -maxdepth 1 -executable 2> /dev/null); do
    if [[ "$path" == *coreutils* ]]; then continue; fi

    binary=$(basename "$path")
    echo '#!${pkgs.bash}/bin/bash' > ".wrap/bin/$binary"
    echo 'source "$(dirname -- "''${BASH_SOURCE[0]}")/../devshell.sh"' >> ".wrap/bin/$binary"
    echo "exec $path" '"$@"' >> ".wrap/bin/$binary"
    chmod +x ".wrap/bin/$binary"
  done
''

This generates wrapper binaries like this:

#!/nix/store/x380x9d7gqh4ig9h50a7wx08p0s6622f-bash-5.1-p16/bin/bash
source "$(dirname -- "${BASH_SOURCE[0]}")/../devshell.sh"
exec /nix/store/rnjmcss5x5mki8qj2wg76fi082jcvb5z-rust-toolchain/bin/cargo "$@"

I tested it with vscode and rust and was a bit disappointed to find out that the rust-analyzer vscode extension has a lot of the toolchain discovery logic in the extension itself, where the shell environment variables are not available. For example it isn't aware of RUST_SRC_PATH and will be unable to find the rust src dir. Something that is not a problem with direnv. So this solution isn't exactly foolproof either. The Rust toolchain is able to compile C dependencies this way though, so it's better than just symlinking the profile.

FWIW, a lot of these issues are avoidable by joining all packages into a single prefix tree (/{bin,lib,include}). The RUST_SRC_PATH env var for example is unnecessary if rust-src is in the same prefix as rustc.

@shyim
Copy link
Contributor Author

shyim commented Dec 23, 2022

@szlend I guess this would definitively make sense to have in some kind of "integration module" as it's much more complex than "just" having a symlink.

Or what do you think @domenkozar :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants