numtide / devshell

Per project developer environments
https://numtide.github.io/devshell/
MIT License
1.22k stars 87 forks source link

feat(entrypoint): fixes/improvements to support `nix run` (devshell-as-flake-app) #243

Closed tomeon closed 1 year ago

tomeon commented 1 year ago

I was poking at a devshell derivation in nix repl recently when I noticed its flakeApp attribute. This is a cool feature! I would love to be able to nix run '.#my-devshell' -- <command> and access commands from my devshell without having to first enter a nix develop sesson. It looks like the flakeApp definition needs some updates to account for devshell.nix refactors; this PR is my attempt to apply those updates. It also adds some convenience features, like a CLI option for specifying the project root path.

Have a few questions I'd like to resolve before removing "Draft" status from this PR:

  1. Assuming you'd like to support flakeApp/nix run, where would you like me to document it? I took a look at the files in ./docs and nothing jumped out as an appropriate spot.
  2. Any thoughts on whether it's possible or desirable to automatically infer a value for PRJ_ROOT when running the devshell entrypoint as a flake app? Seems like PRJ_ROOT should refer to the flake's root directory? For instance, maybe a devshell command references files in the flake by expanding them relative to PRJ_ROOT. Using PWD as PRJ_ROOT in this circumstance might work, as long as PWD is the working tree toplevel of the git repository for the flake providing the devshell. But it almost certainly wouldn't work if trying to nix run 'some-other-flake#devshell-flake-app'. On the hand, maybe there are also cases where PRJ_ROOT should refer to (say) the current working directory -- for instance, maybe I want to run a linting command provided by a "foreign" flake, and that command searches for files that live under PRJ_ROOT.
  3. One of the changes in this PR is exposing devshell's own devshell as the flake output apps.<system>.devshell, as a kind of demo of the flakeApp feature. Is this worthwhile? If so, should it be advertised somehow (in the README, or elsewhere)?

Thanks!

zimbatm commented 1 year ago
  1. I don't know
  2. Yes, I think it's the responsibility of nix to set a FLAKE_ROOT environment variable before running anything. Otherwise, we're bound to re-implement the heuristic and it will drift over time.
  3. Sounds good, it will act as a docs placeholder for now
tomeon commented 1 year ago

I've just pushed some updates:

  1. Added some documentation to README.md and docs/flake-app.md covering running devshells as Nix applications.
  2. Added the option devshell.prj_root_fallback for controlling the last-ditch definition of PRJ_ROOT if it isn't already defined and non-empty. The default value is whatever PWD expands to, which may run afoul of the concern that "the heuristic [...] will drift over time" -- devshell.prj_root_fallback also supports being set to null for signifying "no fallback", and I'm happy to switch to that as the default value. I'll just note that I think nix run 'github:somebody/cool-thing' -- do-cool-thing is a nicer experience than nix run 'github:somebody/cool-thing' -- --prj-root "$PWD" -- do-cool-thing (or bailing out if --prj-root is absent and PRJ_ROOT is empty or unset).

Edit: I also broke some stuff, but it's fixed now :)

bors[bot] commented 1 year ago

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here. For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.