Flakes way of creating a FHS environment

I’m currently using a shell.nix file to produce a FHS environment to run a closed source binary

{ pkgs ? import <nixpkgs> {} }:

(pkgs.buildFHSUserEnv {
  name = "etx-env";
  targetPkgs = pkgs: (with pkgs;
    [ gdk-pixbuf
    ]) ++ (with pkgs.xorg;
    [ libX11
  runScript = "bash";

What’s the flakes way to achieve the same and what’s the command equivalent to “nix-shell” in this case?

1 Like

In the outputs section of your flake.nix. write something like fhs_env = pkgs.buildFHSUserEnv {...};. Then you can run it with nix run .#fhs_env. If your runScript is a shell, you’ll enter the shell where you can explore the environment to determine what to adjust. See the wiki page on Flakes for details on the structure of flake.nix. Remember that flake.nix and flake.lock must be part of a git repo to use the flake.

1 Like

Thanks, that works! The Wiki page only mentions “apps…” attributes in conjunction with nix run which expects a store path. I didn’t understand how to specify it for an environment. I missed that there is a link to the nix run reference page with an example that, together with your explanation, clarifies it.

Thanks again.

1 Like

@fede: could you share your final working code?

I tried apps.fhs_env = { type = "app"; program = pkgs.buildFHSUserEnv {...}; };, but I’m getting error: 'apps.x86_64-linux.fhs_env.program' is not a string but a set.

The program must be a string, that points at the program to run, eg:

program = let drv = pkgs.buildFHSUserEnv {...}; in "${drv}/bin/hello";
1 Like