Embed nix-shell environment into NixOS configuration

I’m currently using remote deployment for pushing NixOS images to low-end chips. I would now like to compile one package on the machine itself since I have to debug it.

My question:
Is it possible to write a derivation that exposes a binary (probably a shell script) that when executed has the same effect as running nix-shell <nixpkgs> -A pkgs.hello?

This would allow me to add this derivation to my configuration.nix for the low-end machines. Then I could execute this script on the machine and all the build dependencies would already be there, built by the powerful remote machine. Like that I could quickly iterate.

Thanks for any pointers into how to do this

There’s nothing ready-made.

But in fact I’ve been working on pretty much that the other day. The thing I have in mind is essentially a library that will create a derivation where the resulting executable will spawn $SHELL where the environment is based on an attribute set of environment variables. It then should be trivial to pass it { PATH = [ pkgs.hello ]; } and add the result to environment.systemPackages or whatever. I’ll need a couple of weekend afternoons for it to mature though, with no timeline. But it’s really straightforward conceptually, one can hack it out without tests and docs on a whim.

It’s intended as a proposal to drastically simplify nix shell and possibly add it to the still-hypothetical Nixpkgs CLI @Ericson2314 and @RaitoBezarius were discussing.

1 Like

There is also nix print-dev-env, which gives you a shell script to activate the environment, but doesn’t bring the actual dependencies with it. But maybe that can also help.

That’s great. I’m glad that I didn’t just overlook something.

What I would say would be the best is to actually move this entirely to nixpkgs:

  • Add a function like derivationToShell to nixpkgs that takes a derivation and transforms it into a derivation that has one shell script as output (e.g. init-shell) that sets up everything like nix-shell currently does.
  • nix-shell would then just be pretty simple since it would only need to apply this translation to the cli provided input and execute the script.
  • My use case would then also be easily covered by just adding derivationToShell { pkgs.hello } to my system packages.

Do I overlook something here (I don’t know much about nix-shell internals)? What is your opinion on this?

This is the most recent state of discussion.

Here’s the spec for what I’ve outlined: GitHub - fricklerhandwerk/attr-env