Advice on packaging an app with a Nix flake - nix run?

Hi all,

I’m packaging an app which needs to be run reproducably. I’m currently using a flake with a devShell to define the environment, but still have to remember which command to execute in that devShell. I’d like to package this into my flake somehow, ideally so that a novice user can run nix run github:me/example_flake to easily launch the app.

The problem is that the flake doesn’t compile a binary, it just uses binaries from other packages. So what I want to do is e.g.

  1. Set flakes A, B, and C as inputs so that they are available on $PATH
  2. Call a binary from flake A, with specific command-line options

The documentation for nix run seems only to specify a particular binary in the nix store, but doesn’t seem to have a way of defining the environment in which that binary runs.

Am I missing something, or is this just not how nix run is intended to be used? If not, how would you suggest going about what I’m aiming for (i.e. having an easy way for a non-nix-experienced user to launch a defined app in a defined nix environment).

3 Likes

I had something like this (some obvious things left out for breverity):

let
  script = pkgs.writeShellScriptBin "example-script" ''
    export PATH=${lib.makeBinPath [ pkgs.depA pkgs.depB pkgs.depC ]}:$PATH

    exec depA --flag1 --flag2 withArg "$@"
  '';
in { type = "app"; path = "${script}/bin/example-script" };
3 Likes

Brilliant! Just what I was looking for, thank you.