clhodapp / nix-runner

Tidy shell shebangs with nix flakes
GNU Affero General Public License v3.0
45 stars 3 forks source link

Use inputs from current flake #5

Open TLATER opened 2 years ago

TLATER commented 2 years ago

Hi, I started using this to write a script for some repository mangling that I might need to do without being able to use the devShell in my flake. Obviously I'd like to use the nixpkgs version specified in my flake.lock - any chance --override-from <some command to resolve the current flake's path> could be supported somehow?

clhodapp commented 2 years ago

I frequently write scripts that bind #!registry self . (or even #!registry nixpkgs . if I happen to be re-exporting nixpkgs with overlays on legacyPackages like I often do). There's no conceptual reason why you couldn't do that and then use e.g. #!package self#devShells.x86_64-linux.default to have the rest of your script run in the context of your dev shell. Having to mention x86_64-linux is a little gross but you really only need do that if you aren't exporting the stuff you need under packages or legacyPackages.

clhodapp commented 2 years ago

Oh btw: the above works way better if you are on a new enough version of nix that it has the "search up" functionality to find flake.nix because the scripts will then work anywhere inside of your flake.

clhodapp commented 2 years ago

Another fun pattern is to do the above with a bin directory at the top of your flake and then add the bin to your path with direnv

TLATER commented 2 years ago

Hrm, that does require re-exporting nixpkgs, which I'm not very fond of. It might ultimately be better to set this script up so it can be invoked with nix run, but I liked the idea of not exporting some local repository management scripts.

Your call on whether this is worth thinking through sed again, of course ;) Hopefully nix shell eventually learns to support this use case.

clhodapp commented 2 years ago

I think I'd be willing to do any needed work on the sed side if I had a better notion of how to alter the underlying nix shell command to make the current flake inputs available to reference for installables.

clhodapp commented 1 year ago

Now that nix searches "up" from the current directory to find flake.nix, it might be possible to have a flag which causes us to run nix flake metadata --json and parse the inputs from that.